From b77a48c628b5cd6a2c1c939e2f813542c7255030 Mon Sep 17 00:00:00 2001 From: Till Hofmann Date: Fri, 14 Dec 2018 16:05:07 +0000 Subject: [PATCH] tests: initialize char* in mode test (#875) * tests: initialize char* in mode test Currently, test_mode_result relies on undefined behavior. The test calls mode_result, which checks whether the pointer is NULL. However, the pointer was never initialized, so it may or may not be NULL, depending on the compiler. This caused a test failure on ppc64 and Fedora 28, apparently because in this setting, gcc sets uninitialized pointers to NULL. By initializing the pointer to the empty string, the behavior is defined and the test passes on all architectures. * mode: fix input pointer check in mode_result Do not check whether *input (i.e., the char* the input points to) is NULL, as this is valid. Instead, check whether the input itself is NULL. * tests: make char* input arg in test_mode_result modifiable The function mode_result expects a modifiable char*, initialize the argument properly so it can be modified. --- source/mode.c | 2 +- test/mode-test.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/source/mode.c b/source/mode.c index 28552e11..45d651a2 100644 --- a/source/mode.c +++ b/source/mode.c @@ -98,7 +98,7 @@ ModeMode mode_result ( Mode *mode, int menu_retv, char **input, unsigned int sel { g_assert ( mode != NULL ); g_assert ( mode->_result != NULL ); - g_assert ( ( *input ) != NULL ); + g_assert ( input != NULL ); return mode->_result ( mode, menu_retv, input, selected_line ); } diff --git a/test/mode-test.c b/test/mode-test.c index 201f0b4a..e68f76d7 100644 --- a/test/mode-test.c +++ b/test/mode-test.c @@ -135,7 +135,7 @@ END_TEST START_TEST(test_mode_result) { - char *res; + char res[] = ""; ck_assert_int_eq ( mode_result ( &help_keys_mode, MENU_NEXT, &res,0), NEXT_DIALOG); ck_assert_int_eq ( mode_result ( &help_keys_mode, MENU_PREVIOUS, &res,0), PREVIOUS_DIALOG); ck_assert_int_eq ( mode_result ( &help_keys_mode, MENU_QUICK_SWITCH|1, &res,0), 1);