]> git.ipfire.org Git - thirdparty/git.git/commitdiff
stash: do not pass strbuf by value
authorDeveshi Dwivedi <deveshigurgaon@gmail.com>
Sun, 15 Mar 2026 09:44:44 +0000 (09:44 +0000)
committerJunio C Hamano <gitster@pobox.com>
Sun, 15 Mar 2026 21:46:51 +0000 (14:46 -0700)
save_untracked_files() takes its 'files' parameter as struct strbuf
by value.  Passing a strbuf by value copies the struct but shares
the underlying buffer between caller and callee, risking a dangling
pointer and double-free if the callee reallocates.

The function needs both the buffer and its length for
pipe_command(), so a plain const char * is not sufficient here.
Switch the parameter to struct strbuf * and update the caller to
pass a pointer.

Signed-off-by: Deveshi Dwivedi <deveshigurgaon@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/stash.c

index e79d612e572e7ca34ec3b92247eebda2431ad664..472eebd6ed656d4628d27e86910c38e8eeefe9dc 100644 (file)
@@ -1232,7 +1232,7 @@ static int check_changes(const struct pathspec *ps, int include_untracked,
 }
 
 static int save_untracked_files(struct stash_info *info, struct strbuf *msg,
-                               struct strbuf files)
+                               struct strbuf *files)
 {
        int ret = 0;
        struct strbuf untracked_msg = STRBUF_INIT;
@@ -1246,7 +1246,7 @@ static int save_untracked_files(struct stash_info *info, struct strbuf *msg,
                         stash_index_path.buf);
 
        strbuf_addf(&untracked_msg, "untracked files on %s\n", msg->buf);
-       if (pipe_command(&cp_upd_index, files.buf, files.len, NULL, 0,
+       if (pipe_command(&cp_upd_index, files->buf, files->len, NULL, 0,
                         NULL, 0)) {
                ret = -1;
                goto done;
@@ -1499,7 +1499,7 @@ static int do_create_stash(const struct pathspec *ps, struct strbuf *stash_msg_b
        parents = NULL;
 
        if (include_untracked) {
-               if (save_untracked_files(info, &msg, untracked_files)) {
+               if (save_untracked_files(info, &msg, &untracked_files)) {
                        if (!quiet)
                                fprintf_ln(stderr, _("Cannot save "
                                                     "the untracked files"));