diff options
author | David Robillard <d@drobilla.net> | 2025-06-07 11:57:39 -0400 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2025-06-07 11:57:39 -0400 |
commit | bb785b3daeba8d27116aeeb12be6e5b98539398d (patch) | |
tree | dfc84d79529e5f7e64c4add213d241de59e47d15 | |
parent | 028b861412cee8787b5dfc2051c50a0d1c85dbcb (diff) | |
download | zix-bb785b3daeba8d27116aeeb12be6e5b98539398d.tar.gz zix-bb785b3daeba8d27116aeeb12be6e5b98539398d.tar.bz2 zix-bb785b3daeba8d27116aeeb12be6e5b98539398d.zip |
Fix potential memory leak on failed allocation in POSIX environment
Adds a test for a new potentially failing allocation added in
8348512a60399d172fc83cd7bdf121d4c0b1015e "Use getenv() instead of environ to
avoid issues on FreeBSD", and fixes the memory leak it exposes.
Avoiding this temporary allocation (which is only for adding null termination)
is the main reason getenv() was initially avoided.
-rw-r--r-- | src/posix/environment_posix.c | 1 | ||||
-rw-r--r-- | test/test_environment.c | 3 |
2 files changed, 4 insertions, 0 deletions
diff --git a/src/posix/environment_posix.c b/src/posix/environment_posix.c index 32a1c8d..8447753 100644 --- a/src/posix/environment_posix.c +++ b/src/posix/environment_posix.c @@ -100,6 +100,7 @@ zix_expand_environment_strings(ZixAllocator* const allocator, !(ref = set_ref(allocator, &ref, t, string + s)) || !(out = append_var(allocator, &len, out, t, ref))) { zix_free(allocator, ref); + zix_free(allocator, out); return NULL; } start = s = t; diff --git a/test/test_environment.c b/test/test_environment.c index dba7b90..8756441 100644 --- a/test/test_environment.c +++ b/test/test_environment.c @@ -111,6 +111,9 @@ test_failed_alloc(void) zix_failing_allocator_reset(&allocator, 2U); assert(!zix_expand_environment_strings(&allocator.base, "/one:$HOME/two")); + + zix_failing_allocator_reset(&allocator, 1U); + assert(!zix_expand_environment_strings(&allocator.base, "/one:$UNSET/two")); #endif } |