lib/util: Move cleanup for unit test in teardown function
authorChristof Schmitt <cs@samba.org>
Tue, 18 Aug 2020 19:48:09 +0000 (12:48 -0700)
committerAndrew Bartlett <abartlet@samba.org>
Mon, 24 Aug 2020 03:10:09 +0000 (03:10 +0000)
Where to call rmdir does not matter, but that should avoid the TOCTOU
warning from CID 1466194 and might be slightly cleaner.

Signed-off-by: Christof Schmitt <cs@samba.org>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
Autobuild-User(master): Andrew Bartlett <abartlet@samba.org>
Autobuild-Date(master): Mon Aug 24 03:10:09 UTC 2020 on sn-devel-184

lib/util/tests/test_util.c

index ad21642357d8b7c72e5dff3583c0b5e401eb70b8..e547668ade3be64aab3d6cca1398a877ea21b4f8 100644 (file)
@@ -130,9 +130,14 @@ static void test_directory_create_or_exists_none(void **state)
        assert_return_code(ret, errno);
        assert_int_equal(sbuf.st_mode & 0777, 0775);
        assert_true(S_ISDIR(sbuf.st_mode));
+}
 
-       ret = rmdir(paths->none);
-       assert_return_code(ret, errno);
+static int teardown_none_directory(void **state)
+{
+       struct test_paths *paths = *state;
+
+       rmdir(paths->none);
+       return 0;
 }
 
 static void test_directory_create_or_exists_dir(void **state)
@@ -218,7 +223,8 @@ static void test_directory_create_or_exists_symlink_file(void **state)
 int main(int argc, char **argv)
 {
        const struct CMUnitTest tests[] = {
-               cmocka_unit_test(test_directory_create_or_exists_none),
+               cmocka_unit_test_teardown(test_directory_create_or_exists_none,
+                                         teardown_none_directory),
                cmocka_unit_test(test_directory_create_or_exists_dir),
                cmocka_unit_test(test_directory_create_or_exists_file),
                cmocka_unit_test(test_directory_create_or_exists_symlink_none),