Skip to contentSkip to conversation

libsepol/tests: fix -Wsometimes-uninitialized clang warnings

PATCH Applied Nicolas Iooss 2 participants 4 messages Dec 26, 2016 – Jan 6, 2017 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 libsepol/tests: fix -Wsometimes-uninitialized clang warnings

When compiling libsepol tests, clang complains about some uninitialized variables:

    test-common.c:171:14: error: variable 'my_primary' is used
    uninitialized whenever 'if' condition is false
    [-Werror,-Wsometimes-uninitialized]
                    } else if (my_flavor == TYPE_ALIAS) {
                               ^~~~~~~~~~~~~~~~~~~~~~~
    test-common.c:179:30: note: uninitialized use occurs here
                    CU_ASSERT(type->primary == my_primary);
                                               ^~~~~~~~~~
    /usr/include/CUnit/CUnit.h:123:30: note: expanded from macro
    'CU_ASSERT'
      { CU_assertImplementation((value), __LINE__, #value, __FILE__, "", CU_...
                                 ^
    test-common.c:171:10: note: remove the 'if' if its condition is
    always true
                    } else if (my_flavor == TYPE_ALIAS) {
                           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    test-common.c:153:25: note: initialize the variable 'my_primary' to
    silence this warning
            unsigned int my_primary, my_flavor, my_value;
                                   ^
                                    = 0
    test-common.c:171:14: error: variable 'my_value' is used
    uninitialized whenever 'if' condition is false
    [-Werror,-Wsometimes-uninitialized]
                    } else if (my_flavor == TYPE_ALIAS) {
                               ^~~~~~~~~~~~~~~~~~~~~~~
    test-common.c:181:30: note: uninitialized use occurs here
                    CU_ASSERT(type->s.value == my_value);
                                               ^~~~~~~~
    /usr/include/CUnit/CUnit.h:123:30: note: expanded from macro
    'CU_ASSERT'
      { CU_assertImplementation((value), __LINE__, #value, __FILE__, "", CU_...
                                 ^
    test-common.c:171:10: note: remove the 'if' if its condition is
    always true
                    } else if (my_flavor == TYPE_ALIAS) {
                           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    test-common.c:153:46: note: initialize the variable 'my_value' to
    silence this warning
            unsigned int my_primary, my_flavor, my_value;
                                                        ^
                                                         = 0

This is because the call to CU_FAIL("not an alias") is not fatal in test_alias_datum(), and variables my_primary and my_value are indeed used uninitialized in a CU_ASSERT statement later.

Silent the warning by moving the elseif condition to a CU_ASSERT statement which replaces the CU_FAIL.

---
 libsepol/tests/test-common.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
libsepol/tests/test-common.c +2 -3
index 058b7435890e..dae47df3dd01 100644
@@ -168,12 +168,11 @@ void test_alias_datum(policydb_t * p, char *id, char *primary_id, char mode, uns
 		if (my_flavor == TYPE_TYPE) {
 			my_primary = 0;
 			my_value = primary->s.value;
-		} else if (my_flavor == TYPE_ALIAS) {
+		} else {
+			CU_ASSERT(my_flavor == TYPE_ALIAS);
 			my_primary = primary->s.value;
 			CU_ASSERT_NOT_EQUAL(type->s.value, primary->s.value);
 			my_value = type->s.value;
-		} else {
-			CU_FAIL("not an alias");
 		}
 
 		CU_ASSERT(type->primary == my_primary);
Signature · 1 line
2.11.0
#1 lore raw

PATCH 2/2 libsepol/tests: fix some memory leaks

When running "make test" with the Address Sanitizer (by adding -fsanitize=address to compiler flags), a lot of memory leaks are reported from checkpolicy. Anyway some leaks come from the tests and it seems cleaner to start fixing these ones.

---
 libsepol/tests/test-deps.c     |  2 ++
 libsepol/tests/test-expander.c | 17 +++++++++++++++++
 2 files changed, 19 insertions(+)
libsepol/tests/test-deps.c +2 -0
index e7d2bebe0d93..ed23e0992ced 100644
@@ -166,6 +166,7 @@ static void do_deps_modreq_global(int req_met, int b, char *policy, char *decl_t
 	ret = link_modules(h, base, mods, 1, 0);
 	CU_ASSERT_FATAL(ret == link_ret);
 	policydb_destroy(&mod);
+	sepol_handle_destroy(h);
 
 	if (!req_met)
 		return;
@@ -246,6 +247,7 @@ static void do_deps_modreq_opt(int req_met, int ret_val, int b, char *policy, ch
 	ret = link_modules(h, base, mods, 1, 0);
 	CU_ASSERT_FATAL(ret == ret_val);
 	policydb_destroy(&mod);
+	sepol_handle_destroy(h);
 	if (ret_val < 0)
 		return;
 
libsepol/tests/test-expander.c +17 -0
index ded1d9de2003..bc946c778b1d 100644
@@ -122,6 +122,9 @@ int expander_policy_init(policydb_t * mybase, int num_modules, policydb_t ** mym
 		return -1;
 	}
 
+	for (i = 0; i < num_modules + 1; i++) {
+		free(filename[i]);
+	}
 	return 0;
 }
 
@@ -173,6 +176,20 @@ int expander_test_cleanup(void)
 {
 	policydb_destroy(&basemod);
 	policydb_destroy(&base_expanded);
+	policydb_destroy(&basemod2);
+	policydb_destroy(&base_expanded2);
+	policydb_destroy(&mod2);
+	policydb_destroy(&base_only_mod);
+	policydb_destroy(&base_only_expanded);
+	policydb_destroy(&role_basemod);
+	policydb_destroy(&role_expanded);
+	policydb_destroy(&role_mod);
+	policydb_destroy(&user_basemod);
+	policydb_destroy(&user_expanded);
+	policydb_destroy(&user_mod);
+	policydb_destroy(&alias_basemod);
+	policydb_destroy(&alias_expanded);
+	policydb_destroy(&alias_mod);
 	free(typemap);
 
 	return 0;
Signature · 1 line
2.11.0
Applied
#2 lore raw

Re: [PATCH 2/2] libsepol/tests: fix some memory leaks

Quoted 6 lines · Nicolas Iooss

When running "make test" with the Address Sanitizer (by adding -fsanitize=address to compiler flags), a lot of memory leaks are reported from checkpolicy. Anyway some leaks come from the tests and it seems cleaner to start fixing these ones.

Applied.

Thanks,

Quoted 61 lines
---
 libsepol/tests/test-deps.c     |  2 ++
 libsepol/tests/test-expander.c | 17 +++++++++++++++++
 2 files changed, 19 insertions(+)
libsepol/tests/test-deps.c +2 -0
index e7d2bebe0d93..ed23e0992ced 100644
@@ -166,6 +166,7 @@ static void do_deps_modreq_global(int req_met, int b, char *policy, char *decl_t
 	ret = link_modules(h, base, mods, 1, 0);
 	CU_ASSERT_FATAL(ret == link_ret);
 	policydb_destroy(&mod);
+	sepol_handle_destroy(h);
 
 	if (!req_met)
 		return;
@@ -246,6 +247,7 @@ static void do_deps_modreq_opt(int req_met, int ret_val, int b, char *policy, ch
 	ret = link_modules(h, base, mods, 1, 0);
 	CU_ASSERT_FATAL(ret == ret_val);
 	policydb_destroy(&mod);
+	sepol_handle_destroy(h);
 	if (ret_val < 0)
 		return;
 
libsepol/tests/test-expander.c +17 -0
index ded1d9de2003..bc946c778b1d 100644
@@ -122,6 +122,9 @@ int expander_policy_init(policydb_t * mybase, int num_modules, policydb_t ** mym
 		return -1;
 	}
 
+	for (i = 0; i < num_modules + 1; i++) {
+		free(filename[i]);
+	}
 	return 0;
 }
 
@@ -173,6 +176,20 @@ int expander_test_cleanup(void)
 {
 	policydb_destroy(&basemod);
 	policydb_destroy(&base_expanded);
+	policydb_destroy(&basemod2);
+	policydb_destroy(&base_expanded2);
+	policydb_destroy(&mod2);
+	policydb_destroy(&base_only_mod);
+	policydb_destroy(&base_only_expanded);
+	policydb_destroy(&role_basemod);
+	policydb_destroy(&role_expanded);
+	policydb_destroy(&role_mod);
+	policydb_destroy(&user_basemod);
+	policydb_destroy(&user_expanded);
+	policydb_destroy(&user_mod);
+	policydb_destroy(&alias_basemod);
+	policydb_destroy(&alias_expanded);
+	policydb_destroy(&alias_mod);
 	free(typemap);
 
 	return 0;
Signature · 2 lines
James Carter <[email protected]>
National Security Agency
Applied
#3 lore raw
In reply to Nicolas Iooss
Quoted 46 lines · Nicolas Iooss

When compiling libsepol tests, clang complains about some uninitialized variables:

    test-common.c:171:14: error: variable 'my_primary' is used
    uninitialized whenever 'if' condition is false
    [-Werror,-Wsometimes-uninitialized]
                    } else if (my_flavor == TYPE_ALIAS) {
                               ^~~~~~~~~~~~~~~~~~~~~~~
    test-common.c:179:30: note: uninitialized use occurs here
                    CU_ASSERT(type->primary == my_primary);
                                               ^~~~~~~~~~
    /usr/include/CUnit/CUnit.h:123:30: note: expanded from macro
    'CU_ASSERT'
      { CU_assertImplementation((value), __LINE__, #value, __FILE__, "", CU_...
                                 ^
    test-common.c:171:10: note: remove the 'if' if its condition is
    always true
                    } else if (my_flavor == TYPE_ALIAS) {
                           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    test-common.c:153:25: note: initialize the variable 'my_primary' to
    silence this warning
            unsigned int my_primary, my_flavor, my_value;
                                   ^
                                    = 0
    test-common.c:171:14: error: variable 'my_value' is used
    uninitialized whenever 'if' condition is false
    [-Werror,-Wsometimes-uninitialized]
                    } else if (my_flavor == TYPE_ALIAS) {
                               ^~~~~~~~~~~~~~~~~~~~~~~
    test-common.c:181:30: note: uninitialized use occurs here
                    CU_ASSERT(type->s.value == my_value);
                                               ^~~~~~~~
    /usr/include/CUnit/CUnit.h:123:30: note: expanded from macro
    'CU_ASSERT'
      { CU_assertImplementation((value), __LINE__, #value, __FILE__, "", CU_...
                                 ^
    test-common.c:171:10: note: remove the 'if' if its condition is
    always true
                    } else if (my_flavor == TYPE_ALIAS) {
                           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    test-common.c:153:46: note: initialize the variable 'my_value' to
    silence this warning
            unsigned int my_primary, my_flavor, my_value;
                                                        ^
                                                         = 0
Quoted 8 lines

This is because the call to CU_FAIL("not an alias") is not fatal in test_alias_datum(), and variables my_primary and my_value are indeed used uninitialized in a CU_ASSERT statement later.

Silent the warning by moving the elseif condition to a CU_ASSERT statement which replaces the CU_FAIL.

Applied.

Thanks,

Quoted 24 lines
---
 libsepol/tests/test-common.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
libsepol/tests/test-common.c +2 -3
index 058b7435890e..dae47df3dd01 100644
@@ -168,12 +168,11 @@ void test_alias_datum(policydb_t * p, char *id, char *primary_id, char mode, uns
 		if (my_flavor == TYPE_TYPE) {
 			my_primary = 0;
 			my_value = primary->s.value;
-		} else if (my_flavor == TYPE_ALIAS) {
+		} else {
+			CU_ASSERT(my_flavor == TYPE_ALIAS);
 			my_primary = primary->s.value;
 			CU_ASSERT_NOT_EQUAL(type->s.value, primary->s.value);
 			my_value = type->s.value;
-		} else {
-			CU_FAIL("not an alias");
 		}
 
 		CU_ASSERT(type->primary == my_primary);
Signature · 2 lines
James Carter <[email protected]>
National Security Agency