From f5bf578c16982400f91822d64782a328cea2c760 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Wed, 13 Jul 2022 23:31:32 -0400 Subject: Fix unlikely null dereference in test --- test/test_string.c | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) (limited to 'test') diff --git a/test/test_string.c b/test/test_string.c index 2d610bb..dcfd894 100644 --- a/test/test_string.c +++ b/test/test_string.c @@ -36,19 +36,27 @@ main(void) #ifndef _WIN32 char* s = NULL; + const char* const home = getenv("HOME"); + setenv("LILV_TEST_1", "test", 1); - char* home_foo = lilv_strjoin(getenv("HOME"), "/foo", NULL); + assert(!strcmp((s = lilv_expand("$LILV_TEST_1")), "test")); free(s); - assert(!strcmp((s = lilv_expand("~")), getenv("HOME"))); - free(s); - assert(!strcmp((s = lilv_expand("~foo")), "~foo")); - free(s); - assert(!strcmp((s = lilv_expand("~/foo")), home_foo)); - free(s); + if (home) { + assert(!strcmp((s = lilv_expand("~")), home)); + free(s); + assert(!strcmp((s = lilv_expand("~foo")), "~foo")); + free(s); + + char* const home_foo = lilv_strjoin(home, "/foo", NULL); + assert(!strcmp((s = lilv_expand("~/foo")), home_foo)); + free(s); + free(home_foo); + } + assert(!strcmp((s = lilv_expand("$NOT_A_VAR")), "$NOT_A_VAR")); free(s); - free(home_foo); + unsetenv("LILV_TEST_1"); #endif -- cgit v1.2.1