Skip to contentSkip to conversation

libsemanage/tests: return when str is NULL

PATCH In review Nicolas Iooss 2 participants 3 messages Sep 30, 2019 – Oct 1, 2019 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 libsemanage/tests: return when str is NULL

CU_FAIL() does not stop the execution flow.

This issue has been found using Infer static analyzer.

---
 libsemanage/tests/test_utilities.c | 1 +
 1 file changed, 1 insertion(+)
libsemanage/tests/test_utilities.c +1 -0
index ba995b5ae094..fa3a077f5e92 100644
@@ -142,6 +142,7 @@ void test_semanage_split_on_space(void)
 	if (!str) {
 		CU_FAIL
 		    ("semanage_split_on_space: unable to perform test, no memory");
+		return;
 	}
 	temp = semanage_split_on_space(str);
 	CU_ASSERT_STRING_EQUAL(temp, "bar    baz");
Signature · 1 line
2.22.0
#1 lore raw

PATCH 2/2 libsemanage/tests: check that string pointers are not NULL before comparing them

This silences many issues reported by Infer static analyzer about possible NULL pointer dereferences.

---
 libsemanage/tests/test_utilities.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
libsemanage/tests/test_utilities.c +10 -0
index fa3a077f5e92..33609401920c 100644
@@ -145,16 +145,19 @@ void test_semanage_split_on_space(void)
 		return;
 	}
 	temp = semanage_split_on_space(str);
+	CU_ASSERT_PTR_NOT_NULL_FATAL(temp);
 	CU_ASSERT_STRING_EQUAL(temp, "bar    baz");
 	free(str);
 	str = temp;
 
 	temp = semanage_split_on_space(str);
+	CU_ASSERT_PTR_NOT_NULL_FATAL(temp);
 	CU_ASSERT_STRING_EQUAL(temp, "baz");
 	free(str);
 	str = temp;
 
 	temp = semanage_split_on_space(str);
+	CU_ASSERT_PTR_NOT_NULL_FATAL(temp);
 	CU_ASSERT_STRING_EQUAL(temp, "");
 	free(str);
 	free(temp);
@@ -171,21 +174,25 @@ void test_semanage_split(void)
 		return;
 	}
 	temp = semanage_split(str, NULL);
+	CU_ASSERT_PTR_NOT_NULL_FATAL(temp);
 	CU_ASSERT_STRING_EQUAL(temp, "foo2   foo:bar:");
 	free(str);
 	str = temp;
 
 	temp = semanage_split(str, "");
+	CU_ASSERT_PTR_NOT_NULL_FATAL(temp);
 	CU_ASSERT_STRING_EQUAL(temp, "foo:bar:");
 	free(str);
 	str = temp;
 
 	temp = semanage_split(str, ":");
+	CU_ASSERT_PTR_NOT_NULL_FATAL(temp);
 	CU_ASSERT_STRING_EQUAL(temp, "bar:");
 	free(str);
 	str = temp;
 
 	temp = semanage_split(str, ":");
+	CU_ASSERT_PTR_NOT_NULL_FATAL(temp);
 	CU_ASSERT_STRING_EQUAL(temp, "");
 	free(str);
 	free(temp);
@@ -301,14 +308,17 @@ void test_semanage_findval(void)
 		CU_FAIL_FATAL("Temporary file was not created, aborting test.");
 	}
 	tok = semanage_findval(fname, "one", NULL);
+	CU_ASSERT_PTR_NOT_NULL_FATAL(tok);
 	CU_ASSERT_STRING_EQUAL(tok, "");
 	free(tok);
 	rewind(fptr);
 	tok = semanage_findval(fname, "one", "");
+	CU_ASSERT_PTR_NOT_NULL_FATAL(tok);
 	CU_ASSERT_STRING_EQUAL(tok, "");
 	free(tok);
 	rewind(fptr);
 	tok = semanage_findval(fname, "sigma", "=");
+	CU_ASSERT_PTR_NOT_NULL_FATAL(tok);
 	CU_ASSERT_STRING_EQUAL(tok, "foo");
 	free(tok);
 }
Signature · 1 line
2.22.0
#2 lore raw

Re: [PATCH 2/2] libsemanage/tests: check that string pointers are not NULL before comparing them

Quoted 4 lines · Nicolas Iooss

This silences many issues reported by Infer static analyzer about possible NULL pointer dereferences.

Thanks, both applied.

Quoted 73 lines
---
  libsemanage/tests/test_utilities.c | 10 ++++++++++
  1 file changed, 10 insertions(+)
libsemanage/tests/test_utilities.c +10 -0
index fa3a077f5e92..33609401920c 100644
@@ -145,16 +145,19 @@ void test_semanage_split_on_space(void)
  		return;
  	}
  	temp = semanage_split_on_space(str);
+	CU_ASSERT_PTR_NOT_NULL_FATAL(temp);
  	CU_ASSERT_STRING_EQUAL(temp, "bar    baz");
  	free(str);
  	str = temp;
 
  	temp = semanage_split_on_space(str);
+	CU_ASSERT_PTR_NOT_NULL_FATAL(temp);
  	CU_ASSERT_STRING_EQUAL(temp, "baz");
  	free(str);
  	str = temp;
 
  	temp = semanage_split_on_space(str);
+	CU_ASSERT_PTR_NOT_NULL_FATAL(temp);
  	CU_ASSERT_STRING_EQUAL(temp, "");
  	free(str);
  	free(temp);
@@ -171,21 +174,25 @@ void test_semanage_split(void)
  		return;
  	}
  	temp = semanage_split(str, NULL);
+	CU_ASSERT_PTR_NOT_NULL_FATAL(temp);
  	CU_ASSERT_STRING_EQUAL(temp, "foo2   foo:bar:");
  	free(str);
  	str = temp;
 
  	temp = semanage_split(str, "");
+	CU_ASSERT_PTR_NOT_NULL_FATAL(temp);
  	CU_ASSERT_STRING_EQUAL(temp, "foo:bar:");
  	free(str);
  	str = temp;
 
  	temp = semanage_split(str, ":");
+	CU_ASSERT_PTR_NOT_NULL_FATAL(temp);
  	CU_ASSERT_STRING_EQUAL(temp, "bar:");
  	free(str);
  	str = temp;
 
  	temp = semanage_split(str, ":");
+	CU_ASSERT_PTR_NOT_NULL_FATAL(temp);
  	CU_ASSERT_STRING_EQUAL(temp, "");
  	free(str);
  	free(temp);
@@ -301,14 +308,17 @@ void test_semanage_findval(void)
  		CU_FAIL_FATAL("Temporary file was not created, aborting test.");
  	}
  	tok = semanage_findval(fname, "one", NULL);
+	CU_ASSERT_PTR_NOT_NULL_FATAL(tok);
  	CU_ASSERT_STRING_EQUAL(tok, "");
  	free(tok);
  	rewind(fptr);
  	tok = semanage_findval(fname, "one", "");
+	CU_ASSERT_PTR_NOT_NULL_FATAL(tok);
  	CU_ASSERT_STRING_EQUAL(tok, "");
  	free(tok);
  	rewind(fptr);
  	tok = semanage_findval(fname, "sigma", "=");
+	CU_ASSERT_PTR_NOT_NULL_FATAL(tok);
  	CU_ASSERT_STRING_EQUAL(tok, "foo");
  	free(tok);
  }