[SV 64124] Avoid use-after-free in expand_variable_buf()

When the expanded value of the variable in buf occupies more space
than available in variable_buffer, function variable_buffer_output
reallocates variable_buffer: return a pointer into the new memory,
not the old memory.

* src/expand.c (expand_variable_buf): Preserve the offset of buf and
return that offset into the (potentially reallocated) buffer.
* tests/scripts/features/expand: Add tests.
This commit is contained in:
Dmitry Goncharov
2023-04-30 09:39:04 -04:00
committed by Paul Smith
parent ebe0a1c9f1
commit 06c75a35b9
2 changed files with 39 additions and 3 deletions

View File

@@ -0,0 +1,26 @@
# -*-perl-*-
$description = "Test variable expansion.";
# sv 64124.
# Expand a variable whose value exceeds 200 bytes.
# 200 is the initial size of variable_buffer.
# Value bigger than 200 bytes causes a realloc of variable_buffer.
# In this test the variable being expanded is MAKEFLAGS and its value occupies
# 11, 550 and 110000 bytes.
my $s = "hello_world";
my @mult = (1, 50, 10000);
for my $m (@mult) {
my $answer = $s x $m;
$ENV{'MAKEFLAGS'} = " -- hello=$answer";
run_make_test(q!
$(info x$(hello)y)
all:
!,
'', "x${answer}y\n#MAKE#: Nothing to be done for 'all'.\n");
}
# This tells the test driver that the perl test script executed properly.
1;