Skip to content

Commit 43192a0

Browse files
wtarreaua-denoyelle
authored andcommitted
BUG/MINOR: tools: fix possible null-deref in env_expand() on out-of-memory
In GH issue #2586 @Bbulatov reported a theoretical null-deref in env_expand() in case there's no memory anymore to expand an environment variable. The function should return NULL in this case so that the only caller (str2sa_range) sees it. In practice it may only happen during boot thus is harmless but better fix it since it's easy. This can be backported to all versions where this applies. (cherry picked from commit ba958fb) Signed-off-by: Amaury Denoyelle <[email protected]>
1 parent 995bba7 commit 43192a0

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

src/tools.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4627,8 +4627,9 @@ int my_unsetenv(const char *name)
46274627
* corresponding value. A variable is identified as a series of alphanumeric
46284628
* characters or underscores following a '$' sign. The <in> string must be
46294629
* free()able. NULL returns NULL. The resulting string might be reallocated if
4630-
* some expansion is made. Variable names may also be enclosed into braces if
4631-
* needed (eg: to concatenate alphanum characters).
4630+
* some expansion is made (an NULL will be returned on failure). Variable names
4631+
* may also be enclosed into braces if needed (eg: to concatenate alphanum
4632+
* characters).
46324633
*/
46334634
char *env_expand(char *in)
46344635
{
@@ -4683,6 +4684,9 @@ char *env_expand(char *in)
46834684
}
46844685

46854686
out = my_realloc2(out, out_len + (txt_end - txt_beg) + val_len + 1);
4687+
if (!out)
4688+
goto leave;
4689+
46864690
if (txt_end > txt_beg) {
46874691
memcpy(out + out_len, txt_beg, txt_end - txt_beg);
46884692
out_len += txt_end - txt_beg;
@@ -4697,6 +4701,7 @@ char *env_expand(char *in)
46974701

46984702
/* here we know that <out> was allocated and that we don't need <in> anymore */
46994703
free(in);
4704+
leave:
47004705
return out;
47014706
}
47024707

0 commit comments

Comments
 (0)