Skip to content

Commit 8d7aa4b

Browse files
devzero2000gitster
authored andcommitted
builtin/commit.c: remove the PATH_MAX limitation via dynamic allocation
Remove the PATH_MAX limitation from the environment setting that points to a filename by switching to dynamic allocation. As a side effect of this change, we also reduce the snprintf() calls, that may silently truncate results if the programmer is not careful. Helped-by: Junio C Hamano <[email protected]> Helped-by: Jeff King <[email protected]> Signed-off-by: Elia Pinto <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d7dffce commit 8d7aa4b

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

builtin/commit.c

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -960,15 +960,15 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
960960
return 0;
961961

962962
if (use_editor) {
963-
char index[PATH_MAX];
964-
const char *env[2] = { NULL };
965-
env[0] = index;
966-
snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file);
967-
if (launch_editor(git_path_commit_editmsg(), NULL, env)) {
963+
struct argv_array env = ARGV_ARRAY_INIT;
964+
965+
argv_array_pushf(&env, "GIT_INDEX_FILE=%s", index_file);
966+
if (launch_editor(git_path_commit_editmsg(), NULL, env.argv)) {
968967
fprintf(stderr,
969968
_("Please supply the message using either -m or -F option.\n"));
970969
exit(1);
971970
}
971+
argv_array_clear(&env);
972972
}
973973

974974
if (!no_verify &&
@@ -1557,23 +1557,22 @@ static int run_rewrite_hook(const unsigned char *oldsha1,
15571557

15581558
int run_commit_hook(int editor_is_used, const char *index_file, const char *name, ...)
15591559
{
1560-
const char *hook_env[3] = { NULL };
1561-
char index[PATH_MAX];
1560+
struct argv_array hook_env = ARGV_ARRAY_INIT;
15621561
va_list args;
15631562
int ret;
15641563

1565-
snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file);
1566-
hook_env[0] = index;
1564+
argv_array_pushf(&hook_env, "GIT_INDEX_FILE=%s", index_file);
15671565

15681566
/*
15691567
* Let the hook know that no editor will be launched.
15701568
*/
15711569
if (!editor_is_used)
1572-
hook_env[1] = "GIT_EDITOR=:";
1570+
argv_array_push(&hook_env, "GIT_EDITOR=:");
15731571

15741572
va_start(args, name);
1575-
ret = run_hook_ve(hook_env, name, args);
1573+
ret = run_hook_ve(hook_env.argv,name, args);
15761574
va_end(args);
1575+
argv_array_clear(&hook_env);
15771576

15781577
return ret;
15791578
}

0 commit comments

Comments
 (0)