>From b0fc09ca0ccc93b6af774d00f4c23311e7fdf59c Mon Sep 17 00:00:00 2001 From: Lukas Slebodnik Date: Thu, 11 Apr 2013 10:00:14 +0200 Subject: [PATCH] Fix segmentation fault in test_io. If configure isn't being run with argument --with-test-dir, then variable TEST_DIR will be defined, but its value will be empty (""). In this case opendir will fail with uncatched error "Directory does not exist, or name is an empty string". Finally function call dirfd will segfault because its argument is NULL. I changed default value of TEST_DIR (if --with-test-dir was not used). Assert to auxiliary function was added and error messages became more verbose. --- src/conf_macros.m4 | 7 ++++--- src/tests/cmocka/test_io.c | 13 ++++++++++--- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/conf_macros.m4 b/src/conf_macros.m4 index 4ea4e93679563b7c3fdc9af751b1cafd728f859b..26eb4acccc4895e961ecc50d490a3cdd5716d085 100644 --- a/src/conf_macros.m4 +++ b/src/conf_macros.m4 @@ -329,11 +329,12 @@ AC_DEFUN([WITH_TEST_DIR], [AC_HELP_STRING([--with-test-dir=PATH], [Directory used for make check temporary files [$builddir]] ) - ] + ], + [TEST_DIR=$withval], + [TEST_DIR="."] ) - TEST_DIR=$with_test_dir AC_SUBST(TEST_DIR) - AC_DEFINE_UNQUOTED(TEST_DIR, "$with_test_dir", [Directory used for 'make check' temporary files]) + AC_DEFINE_UNQUOTED(TEST_DIR, "$TEST_DIR", [Directory used for 'make check' temporary files]) ]) AC_DEFUN([WITH_NSCD], diff --git a/src/tests/cmocka/test_io.c b/src/tests/cmocka/test_io.c index 4d21b2e25d3f35cc155b6410bcb4ce8963a59c15..5418ac7935e1145ea66e8396d164ea6e01635959 100644 --- a/src/tests/cmocka/test_io.c +++ b/src/tests/cmocka/test_io.c @@ -49,8 +49,11 @@ static char *get_filepath(char path[]) ret = mkstemp(path); if (ret == -1) { - fprintf(stderr, "mkstemp failed\n"); + int err = errno; + fprintf(stderr, "mkstemp failed with path:'%s' [%s]\n", + path, strerror(err)); } + assert_false(ret == -1); return path; } @@ -58,9 +61,13 @@ static char *get_filepath(char path[]) void setup_dirp(void **state) { DIR *dirp = opendir(TEST_DIR); - if (dirp != NULL){ - *state = (void *)dirp; + if (dirp == NULL) { + int err = errno; + fprintf(stderr, "Could not open directory:'%s' [%s]\n", + TEST_DIR, strerror(err)); } + assert_non_null(dirp); + *state = (void *)dirp; } void teardown_dirp(void **state) -- 1.8.1.4