Skip to content

Commit ce2c4c2

Browse files
peffdscho
authored andcommitted
commit: copy saved getenv() result
We save the result of $GIT_INDEX_FILE so that we can restore it after setting it to a new value and running add--interactive. However, the pointer returned by getenv() is not guaranteed to be valid after calling setenv(). This _usually_ works fine, but can fail if libc needs to reallocate the environment block during the setenv(). Let's just duplicate the string, so we know that it remains valid. In the long run it may be more robust to teach interactive_add() to take a set of environment variables to pass along to run-command when it execs add--interactive. And then we would not have to do this save/restore dance at all. But this is an easy fix in the meantime. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0bf3c96 commit ce2c4c2

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

builtin/commit.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ static const char *prepare_index(int argc, const char **argv, const char *prefix
351351
if (write_locked_index(&the_index, &index_lock, 0))
352352
die(_("unable to create temporary index"));
353353

354-
old_index_env = getenv(INDEX_ENVIRONMENT);
354+
old_index_env = xstrdup_or_null(getenv(INDEX_ENVIRONMENT));
355355
setenv(INDEX_ENVIRONMENT, get_lock_file_path(&index_lock), 1);
356356

357357
if (interactive_add(argc, argv, prefix, patch_interactive) != 0)
@@ -361,6 +361,7 @@ static const char *prepare_index(int argc, const char **argv, const char *prefix
361361
setenv(INDEX_ENVIRONMENT, old_index_env, 1);
362362
else
363363
unsetenv(INDEX_ENVIRONMENT);
364+
FREE_AND_NULL(old_index_env);
364365

365366
discard_cache();
366367
read_cache_from(get_lock_file_path(&index_lock));

0 commit comments

Comments
 (0)