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.

Signed-off-by: Nicolas Iooss <nicolas.iooss@m4x.org>
This commit is contained in:
Nicolas Iooss 2019-09-30 22:22:11 +02:00 committed by Stephen Smalley
parent 7673b97e45
commit 74c5e551ca

View File

@ -145,16 +145,19 @@ void test_semanage_split_on_space(void)
return; return;
} }
temp = semanage_split_on_space(str); temp = semanage_split_on_space(str);
CU_ASSERT_PTR_NOT_NULL_FATAL(temp);
CU_ASSERT_STRING_EQUAL(temp, "bar baz"); CU_ASSERT_STRING_EQUAL(temp, "bar baz");
free(str); free(str);
str = temp; str = temp;
temp = semanage_split_on_space(str); temp = semanage_split_on_space(str);
CU_ASSERT_PTR_NOT_NULL_FATAL(temp);
CU_ASSERT_STRING_EQUAL(temp, "baz"); CU_ASSERT_STRING_EQUAL(temp, "baz");
free(str); free(str);
str = temp; str = temp;
temp = semanage_split_on_space(str); temp = semanage_split_on_space(str);
CU_ASSERT_PTR_NOT_NULL_FATAL(temp);
CU_ASSERT_STRING_EQUAL(temp, ""); CU_ASSERT_STRING_EQUAL(temp, "");
free(str); free(str);
free(temp); free(temp);
@ -171,21 +174,25 @@ void test_semanage_split(void)
return; return;
} }
temp = semanage_split(str, NULL); temp = semanage_split(str, NULL);
CU_ASSERT_PTR_NOT_NULL_FATAL(temp);
CU_ASSERT_STRING_EQUAL(temp, "foo2 foo:bar:"); CU_ASSERT_STRING_EQUAL(temp, "foo2 foo:bar:");
free(str); free(str);
str = temp; str = temp;
temp = semanage_split(str, ""); temp = semanage_split(str, "");
CU_ASSERT_PTR_NOT_NULL_FATAL(temp);
CU_ASSERT_STRING_EQUAL(temp, "foo:bar:"); CU_ASSERT_STRING_EQUAL(temp, "foo:bar:");
free(str); free(str);
str = temp; str = temp;
temp = semanage_split(str, ":"); temp = semanage_split(str, ":");
CU_ASSERT_PTR_NOT_NULL_FATAL(temp);
CU_ASSERT_STRING_EQUAL(temp, "bar:"); CU_ASSERT_STRING_EQUAL(temp, "bar:");
free(str); free(str);
str = temp; str = temp;
temp = semanage_split(str, ":"); temp = semanage_split(str, ":");
CU_ASSERT_PTR_NOT_NULL_FATAL(temp);
CU_ASSERT_STRING_EQUAL(temp, ""); CU_ASSERT_STRING_EQUAL(temp, "");
free(str); free(str);
free(temp); free(temp);
@ -301,14 +308,17 @@ void test_semanage_findval(void)
CU_FAIL_FATAL("Temporary file was not created, aborting test."); CU_FAIL_FATAL("Temporary file was not created, aborting test.");
} }
tok = semanage_findval(fname, "one", NULL); tok = semanage_findval(fname, "one", NULL);
CU_ASSERT_PTR_NOT_NULL_FATAL(tok);
CU_ASSERT_STRING_EQUAL(tok, ""); CU_ASSERT_STRING_EQUAL(tok, "");
free(tok); free(tok);
rewind(fptr); rewind(fptr);
tok = semanage_findval(fname, "one", ""); tok = semanage_findval(fname, "one", "");
CU_ASSERT_PTR_NOT_NULL_FATAL(tok);
CU_ASSERT_STRING_EQUAL(tok, ""); CU_ASSERT_STRING_EQUAL(tok, "");
free(tok); free(tok);
rewind(fptr); rewind(fptr);
tok = semanage_findval(fname, "sigma", "="); tok = semanage_findval(fname, "sigma", "=");
CU_ASSERT_PTR_NOT_NULL_FATAL(tok);
CU_ASSERT_STRING_EQUAL(tok, "foo"); CU_ASSERT_STRING_EQUAL(tok, "foo");
free(tok); free(tok);
} }