From 67f18d1760634c42d2b85d8ddb31d3915cd56d78 Mon Sep 17 00:00:00 2001 From: Mikko Johannes Koivunalho Date: Thu, 6 Feb 2020 22:48:12 +0100 Subject: [PATCH 01/11] Remove unused variables from tests --- tests/check_check_master.c | 4 ---- tests/check_check_tags.c | 2 +- tests/check_set_max_msg_size.c | 1 - 3 files changed, 1 insertion(+), 6 deletions(-) diff --git a/tests/check_check_master.c b/tests/check_check_master.c index a606708a..c25d3aea 100644 --- a/tests/check_check_master.c +++ b/tests/check_check_master.c @@ -652,10 +652,6 @@ END_TEST START_TEST(test_check_test_names) { int i; - int line_no; - int passed = 0; - int number_failed; - TestResult *tr; rewind(test_names_file); diff --git a/tests/check_check_tags.c b/tests/check_check_tags.c index e4f485ab..7c77e302 100644 --- a/tests/check_check_tags.c +++ b/tests/check_check_tags.c @@ -658,7 +658,7 @@ START_TEST(include_w_spaces) Suite *make_tag_suite(void) { - TCase *set_get_tags, *no_filters; + TCase *no_filters; TCase *include_filters, *exclude_filters; #if HAVE_DECL_SETENV TCase *include_filters_env, *exclude_filters_env; diff --git a/tests/check_set_max_msg_size.c b/tests/check_set_max_msg_size.c index 92a3bab5..55280d65 100644 --- a/tests/check_set_max_msg_size.c +++ b/tests/check_set_max_msg_size.c @@ -52,7 +52,6 @@ static Suite *make_set_max_msg_size_suite(void) int main (int argc, char *argv[]) { - int n; SRunner *sr; if (argc != 2) { From 98680507006f62ecd0e26792a4d665a3f6f7e4db Mon Sep 17 00:00:00 2001 From: Mikko Johannes Koivunalho Date: Thu, 6 Feb 2020 23:18:20 +0100 Subject: [PATCH 02/11] Mark float literals properly with suffix 'F' --- tests/check_check_sub.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/check_check_sub.c b/tests/check_check_sub.c index ddfea1d2..184a3bf7 100644 --- a/tests/check_check_sub.c +++ b/tests/check_check_sub.c @@ -1092,7 +1092,7 @@ END_TEST START_TEST(test_ck_assert_double_eq_with_promotion) { - float x = 0.1; + float x = 0.1F; double y = x; record_test_name(tcase_name()); @@ -1103,7 +1103,7 @@ END_TEST START_TEST(test_ck_assert_double_eq_with_conv) { - float x = 0.1; + float x = 0.1F; record_test_name(tcase_name()); @@ -1608,7 +1608,7 @@ END_TEST START_TEST(test_ck_assert_ldouble_eq_with_promotion) { - float x = 1.1; + float x = 1.1F; long double y = x; record_test_name(tcase_name()); @@ -1619,7 +1619,7 @@ END_TEST START_TEST(test_ck_assert_ldouble_eq_with_conv) { - float x = 1.1; + float x = 1.1F; long double y = x; record_test_name(tcase_name()); From 1add8571cb78455bad0e94a5f4b1a7dff506c727 Mon Sep 17 00:00:00 2001 From: Mikko Johannes Koivunalho Date: Thu, 6 Feb 2020 23:19:26 +0100 Subject: [PATCH 03/11] Use strtoul instead of atoi --- tests/check_set_max_msg_size.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/check_set_max_msg_size.c b/tests/check_set_max_msg_size.c index 55280d65..d7c91bd6 100644 --- a/tests/check_set_max_msg_size.c +++ b/tests/check_set_max_msg_size.c @@ -63,8 +63,8 @@ int main (int argc, char *argv[]) * Run the test suite. This is intended to trigger the "Message is too long" error. * Actual success/failure is determined by examining the output. */ - check_set_max_msg_size(32); /* 1st call has no effect since */ - check_set_max_msg_size(atoi(argv[1])); /* the 2nd call will override it. */ + check_set_max_msg_size(32); /* 1st call has no effect since */ + check_set_max_msg_size(strtoul(argv[1], NULL, 10)); /* the 2nd call will override it. */ sr = srunner_create(make_set_max_msg_size_suite()); srunner_run_all(sr, CK_NORMAL); srunner_free(sr); From 57fc493a518c1e1304fd4ccb775e6385415636fa Mon Sep 17 00:00:00 2001 From: Mikko Koivunalho Date: Tue, 11 Feb 2020 08:48:51 +0100 Subject: [PATCH 04/11] Change signature of internal test func function int get_next_failure_line_num(FILE * file) calls long strtol(..) and returns its return value. Signed-off-by: Mikko Koivunalho --- tests/check_check.h | 2 +- tests/check_check_master.c | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/check_check.h b/tests/check_check.h index 32e49bc6..d828ee01 100644 --- a/tests/check_check.h +++ b/tests/check_check.h @@ -96,6 +96,6 @@ void record_failure_line_num(const int line); * * If there are no more lines to read, -1 is returned. */ -int get_next_failure_line_num(FILE * file); +long get_next_failure_line_num(FILE * file); #endif /* CHECK_CHECK_H */ diff --git a/tests/check_check_master.c b/tests/check_check_master.c index c25d3aea..35c5eb49 100644 --- a/tests/check_check_master.c +++ b/tests/check_check_master.c @@ -561,7 +561,7 @@ END_TEST START_TEST(test_check_failure_lnos) { int i; - int line_no; + long line_no; int passed = 0; int number_failed; TestResult *tr; @@ -977,13 +977,13 @@ void record_failure_line_num(int linenum) } } -int get_next_failure_line_num(FILE * file) +long get_next_failure_line_num(FILE * file) { char * line = NULL; char * end = NULL; size_t length; ssize_t written; - int value = -1; + long value = -1; written = getline(&line, &length, file); From 70d28f9437eb4325de83ca158618bab23fbaaf0d Mon Sep 17 00:00:00 2001 From: Mikko Koivunalho Date: Tue, 11 Feb 2020 09:08:49 +0100 Subject: [PATCH 05/11] Change variable from int to size_t --- tests/check_check_master.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/check_check_master.c b/tests/check_check_master.c index 35c5eb49..92e635fa 100644 --- a/tests/check_check_master.c +++ b/tests/check_check_master.c @@ -932,7 +932,7 @@ char* get_next_test_name(FILE * file) void record_failure_line_num(int linenum) { - int to_write; + size_t to_write; ssize_t written; int result; char string[16]; @@ -944,7 +944,7 @@ void record_failure_line_num(int linenum) linenum += 1; to_write = snprintf(string, sizeof(string), "%d\n", linenum); - if(to_write <= 0) + if(to_write == 0) { fprintf(stderr, "%s:%d: Error in call to snprintf:", __FILE__, __LINE__); exit(1); @@ -965,7 +965,7 @@ void record_failure_line_num(int linenum) written = fwrite(string, 1, to_write, line_num_failures); if(written != to_write) { - fprintf(stderr, "%s:%d: Error in call to fwrite, wrote %zd instead of %d:", __FILE__, __LINE__, written, to_write); + fprintf(stderr, "%s:%d: Error in call to fwrite, wrote %zd instead of %zu:", __FILE__, __LINE__, written, to_write); exit(1); } From c176543369bd86e6fb89a33602b9e095b70fef20 Mon Sep 17 00:00:00 2001 From: Mikko Koivunalho Date: Tue, 11 Feb 2020 09:10:16 +0100 Subject: [PATCH 06/11] Change variable from ssize_t to size_t --- tests/check_check_master.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/check_check_master.c b/tests/check_check_master.c index 92e635fa..776a6920 100644 --- a/tests/check_check_master.c +++ b/tests/check_check_master.c @@ -933,7 +933,7 @@ char* get_next_test_name(FILE * file) void record_failure_line_num(int linenum) { size_t to_write; - ssize_t written; + size_t written; int result; char string[16]; From b7d52310c7bc205c1e836a2c60f051c786dfc3be Mon Sep 17 00:00:00 2001 From: Mikko Koivunalho Date: Wed, 12 Feb 2020 10:00:53 +0100 Subject: [PATCH 07/11] Change sig of internal func non_pass() --- src/check.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/check.c b/src/check.c index 2d4ebb68..f74b3849 100644 --- a/src/check.c +++ b/src/check.c @@ -54,7 +54,7 @@ int check_micro_version = CHECK_MICRO_VERSION; const char* current_test_name = NULL; -static int non_pass(int val); +static int non_pass(enum test_result); static Fixture *fixture_create(SFun fun, int ischecked); static void tcase_add_fixture(TCase * tc, SFun setup, SFun teardown, int ischecked); @@ -512,7 +512,7 @@ TestResult **srunner_results(SRunner * sr) return trarray; } -static int non_pass(int val) +static int non_pass(enum test_result val) { return val != CK_PASS; } From 5e2b32fccc8c4c2440fdf8eccd0eb73fc5a3dcd9 Mon Sep 17 00:00:00 2001 From: Mikko Johannes Koivunalho Date: Thu, 13 Feb 2020 21:19:14 +0100 Subject: [PATCH 08/11] Add macro CK_ATTRIBUTE_FORMAT MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Using GNU C's format attribute: https://gcc.gnu.org/onlinedocs/gcc-2.95.3/gcc_4.html#SEC84 Fixing following warnings: /src/check.c:383:9: warning: function ‘_ck_assert_failed’ might be a candidate for ‘gnu_printf’ format attribute [-Wsuggest-attribute=format] /src/check_error.c:47:5: warning: function ‘eprintf’ might be a candidate for ‘gnu_printf’ format attribute [-Wsuggest-attribute=format] /src/check_str.c:91:9: warning: function ‘ck_strdup_printf’ might be a candidate for ‘gnu_printf’ format attribute [-Wsuggest-attribute=format] Signed-off-by: Mikko Johannes Koivunalho --- lib/libcompat.h | 2 ++ src/check.h.in | 6 ++++-- src/check_error.h | 2 +- src/check_str.h | 4 +++- 4 files changed, 10 insertions(+), 4 deletions(-) diff --git a/lib/libcompat.h b/lib/libcompat.h index 73c2c7ed..a5d26503 100644 --- a/lib/libcompat.h +++ b/lib/libcompat.h @@ -48,8 +48,10 @@ #if GCC_VERSION_AT_LEAST(2,95,3) #define CK_ATTRIBUTE_UNUSED __attribute__ ((unused)) +#define CK_ATTRIBUTE_FORMAT(a, b, c) __attribute__ ((format (a, b, c))) #else #define CK_ATTRIBUTE_UNUSED +#define CK_ATTRIBUTE_FORMAT(a, b, c) #endif /* GCC 2.95 */ #if GCC_VERSION_AT_LEAST(2,5,0) diff --git a/src/check.h.in b/src/check.h.in index 2e131d26..fdbc8b05 100644 --- a/src/check.h.in +++ b/src/check.h.in @@ -64,8 +64,10 @@ CK_CPPSTART #if GCC_VERSION_AT_LEAST(2,95,3) #define CK_ATTRIBUTE_UNUSED __attribute__ ((unused)) +#define CK_ATTRIBUTE_FORMAT(a, b, c) __attribute__ ((format (a, b, c))) #else #define CK_ATTRIBUTE_UNUSED +#define CK_ATTRIBUTE_FORMAT(a, b, c) #endif /* GCC 2.95 */ #if GCC_VERSION_AT_LEAST(2,5,0) @@ -499,10 +501,10 @@ static void __testname ## _fn (int _i CK_ATTRIBUTE_UNUSED) #if @HAVE_FORK@ CK_DLL_EXP void CK_EXPORT _ck_assert_failed(const char *file, int line, const char *expr, - ...) CK_ATTRIBUTE_NORETURN; + ...) CK_ATTRIBUTE_NORETURN CK_ATTRIBUTE_FORMAT(gnu_printf, 3, 4); #else CK_DLL_EXP void CK_EXPORT _ck_assert_failed(const char *file, int line, - const char *expr, ...); + const char *expr, ...) CK_ATTRIBUTE_FORMAT(gnu_printf, 3, 4); #endif /** diff --git a/src/check_error.h b/src/check_error.h index c2c8d34e..19580880 100644 --- a/src/check_error.h +++ b/src/check_error.h @@ -31,7 +31,7 @@ extern jmp_buf error_jmp_buffer; /* Print error message and die If fmt ends in colon, include system error information */ void eprintf(const char *fmt, const char *file, int line, - ...) CK_ATTRIBUTE_NORETURN; + ...) CK_ATTRIBUTE_NORETURN CK_ATTRIBUTE_FORMAT(gnu_printf, 1, 4); /* malloc or die */ void *emalloc(size_t n); void *erealloc(void *, size_t n); diff --git a/src/check_str.h b/src/check_str.h index b26eae2b..92697f28 100644 --- a/src/check_str.h +++ b/src/check_str.h @@ -21,6 +21,8 @@ #ifndef CHECK_STR_H #define CHECK_STR_H +#include "../lib/libcompat.h" + /* Return a string representation of the given TestResult. Return value has been malloc'd, and must be freed by the caller */ char *tr_str(TestResult * tr); @@ -37,6 +39,6 @@ char *tr_short_str(TestResult * tr); */ char *sr_stat_str(SRunner * sr); -char *ck_strdup_printf(const char *fmt, ...); +char *ck_strdup_printf(const char *fmt, ...) CK_ATTRIBUTE_FORMAT(gnu_printf, 1, 2); #endif /* CHECK_STR_H */ From 0015a1b6038ca400a1cfbd94fc1e090297b99387 Mon Sep 17 00:00:00 2001 From: Mikko Koivunalho Date: Sat, 15 Feb 2020 15:18:05 +0200 Subject: [PATCH 09/11] Correct function type of snprintf man snprintf: Upon successful return, these functions return the number of characters printed (excluding the null byte used to end output to strings). The functions snprintf() and vsnprintf() do not write more than size bytes (including the terminating null byte ('\0')). If the output was truncated due to this limit, then the return value is the number of characters (excluding the terminating null byte) which would have been written to the final string if enough space had been available. Thus, a return value of size or more means that the output was truncated. (See also below under NOTES.) If an output error is encountered, a negative value is returned. Signed-off-by: Mikko Koivunalho --- tests/check_check_master.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tests/check_check_master.c b/tests/check_check_master.c index 776a6920..aaf45f3f 100644 --- a/tests/check_check_master.c +++ b/tests/check_check_master.c @@ -934,7 +934,7 @@ void record_failure_line_num(int linenum) { size_t to_write; size_t written; - int result; + int result, chars_printed; char string[16]; /* @@ -943,12 +943,13 @@ void record_failure_line_num(int linenum) */ linenum += 1; - to_write = snprintf(string, sizeof(string), "%d\n", linenum); - if(to_write == 0) + chars_printed = snprintf(string, sizeof(string), "%d\n", linenum); + if(chars_printed <= 0 || (size_t) chars_printed >= sizeof(string)) { fprintf(stderr, "%s:%d: Error in call to snprintf:", __FILE__, __LINE__); exit(1); } + to_write = (size_t) chars_printed; if(line_num_failures == NULL) { From b6f8806df5c61cf0882eb276996c45793a4f8ff0 Mon Sep 17 00:00:00 2001 From: Mikko Johannes Koivunalho Date: Mon, 9 Mar 2020 11:29:39 +0100 Subject: [PATCH 10/11] Document changes to NEWS Signed-off-by: Mikko Johannes Koivunalho --- NEWS | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/NEWS b/NEWS index 4f3fc479..c704a45e 100644 --- a/NEWS +++ b/NEWS @@ -1,6 +1,11 @@ In Development: # Mentioning Check 0.14.0 for now, to fix distcheck target until next release +* Define CK_ATTRIBUTE_FORMAT for GCC >= 2.95.3, to make use of + ‘gnu_printf’ format attribute + +* Refactor tests to fix signed - unsigned conversions + Sun Jan 26, 2020: Released Check 0.14.0 based on hash 0076ec62f71d33b5b54530f8471b4c99f50638d7 From 28fd45a09c57768ab7c62ba627b5dc9b6bf4705f Mon Sep 17 00:00:00 2001 From: Mikko Johannes Koivunalho Date: Mon, 9 Mar 2020 23:11:12 +0100 Subject: [PATCH 11/11] Add to TODO: does compiler support __attribute__ Signed-off-by: Mikko Johannes Koivunalho --- TODO | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/TODO b/TODO index 7e53b95b..3045d3f6 100644 --- a/TODO +++ b/TODO @@ -59,6 +59,10 @@ Build issues: [ ] * Add option BUILD_DOCUMENTATION to CMake (Github #217). [ ] * Missing function check in CMake creates def HAVE_FOO=0. There should be no HAVE_FOO (Github #195). +[ ] * CMake and Autotools should check if compiler supports __attribute__ ((format (a, b, c))) + to decide if CK_ATTRIBUTE_FORMAT can be defined + https://www.gnu.org/software/autoconf-archive/ax_gcc_func_attribute.html + Check source code: ============