From 592471fd3a27d5208f9959305566343dd262e403 Mon Sep 17 00:00:00 2001 From: Abhishek Singh Date: Sat, 13 Apr 2013 02:18:46 +0530 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. Default value of TEST_DIR (if --with-test-dir was not used) is changed to current directory.Assert to auxiliary function was added and error messages became more verbose. Function tests_set_cwd does not ignore return value of chdir, because TEST_DIR should not be the empty string. relativepath calculation method is changed and is made general based on basedir. --- Makefile.am | 3 ++- src/conf_macros.m4 | 7 ++++--- src/tests/cmocka/test_io.c | 17 +++++++++++++---- src/tests/common.c | 8 ++------ 4 files changed, 21 insertions(+), 14 deletions(-) diff --git a/Makefile.am b/Makefile.am index 332669551a781263e44fd855a920d370b5898c15..27bb8d3cb1e28f63e63c8b8c061d42a41a8b38ec 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1241,7 +1241,8 @@ test_find_uid_LDADD = \ test_io_SOURCES = \ src/tests/cmocka/test_io.c \ - src/util/io.c + src/util/io.c \ + src/tests/common.c test_io_CFLAGS = \ $(AM_CFLAGS) test_io_LDADD = \ 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..68c63ade2bae8f4ba79577e3ecc5f1aaf3fe6e3f 100644 --- a/src/tests/cmocka/test_io.c +++ b/src/tests/cmocka/test_io.c @@ -24,6 +24,7 @@ #include #include +#include #include #include #include @@ -36,6 +37,7 @@ #include "limits.h" #include "util/io.h" #include "util/util.h" +#include "tests/common.h" #define FILE_PATH TEST_DIR"/test_io.XXXXXX" #define NON_EX_PATH "non-existent-path" @@ -49,8 +51,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 +63,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) @@ -113,8 +122,8 @@ void test_sss_openat_cloexec_success(void **state) char path[PATH_MAX] = {'\0'}; const char *relativepath; - relativepath = strchr(get_filepath(path), 't'); dir_fd = dirfd((DIR *)*state); + relativepath = get_filepath(path) + strlen(TEST_DIR) + 1; fd = sss_openat_cloexec(dir_fd, relativepath, flags, &ret); assert_true(fd != -1); diff --git a/src/tests/common.c b/src/tests/common.c index 54c93e281d166e7b55a27a96f5dbc7ce67c57e62..16c051656f86f7b0914893a9349366893429e55f 100644 --- a/src/tests/common.c +++ b/src/tests/common.c @@ -33,12 +33,8 @@ tests_set_cwd(void) ret = chdir(TEST_DIR); if (ret == -1) { - if (strlen(TEST_DIR)) { - fprintf(stderr, - "Could not chdir to [%s].\n" - "Attempting to continue with current dir\n", - TEST_DIR); - } + fprintf(stderr, "Could not chdir to [%s].\n" + "Attempting to continue with current dir\n", TEST_DIR); } } -- 1.8.1.4