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 <gitster@pobox.com> Helped-by: Jeff King <peff@peff.net> Signed-off-by: Elia Pinto <gitter.spiros@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Elia Pinto committed
Jan 13, 2017 at 17:58 UTC
8d7aa4ba6a00b3ff69261e88b4842c0df5662125
1 file changed
+10
-11
builtin/commit.c
+10
-11
@@ -960,15 +960,15 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
960
return 0;
961
962
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)) {
967
fprintf(stderr,
968
_("Please supply the message using either -m or -F option.\n"));
969
exit(1);
970
}
971
+ argv_array_clear(&env);
972
}
973
974
if (!no_verify &&
@@ -1557,23 +1557,22 @@ static int run_rewrite_hook(const unsigned char *oldsha1,
1557
1558
int run_commit_hook(int editor_is_used, const char *index_file, const char *name, ...)
1559
{
1560
- const char *hook_env[3] = { NULL };
1561
- char index[PATH_MAX];
1560
+ struct argv_array hook_env = ARGV_ARRAY_INIT;
1561
va_list args;
1562
int ret;
1563
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);
1565
1566
/*
1567
* Let the hook know that no editor will be launched.
1568
*/
1569
if (!editor_is_used)
1572
- hook_env[1] = "GIT_EDITOR=:";
1570
+ argv_array_push(&hook_env, "GIT_EDITOR=:");
1571
1572
va_start(args, name);
1575
- ret = run_hook_ve(hook_env, name, args);
1573
+ ret = run_hook_ve(hook_env.argv,name, args);
1574
va_end(args);
1575
+ argv_array_clear(&hook_env);
1576
1577
return ret;
1578
}