Skip to content

Commit 8f8e1b0

Browse files
deveshidwivedigitster
authored andcommitted
stash: do not pass strbuf by value
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>
1 parent 65fec23 commit 8f8e1b0

1 file changed

Lines changed: 3 additions & 3 deletions

File tree

builtin/stash.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1232,7 +1232,7 @@ static int check_changes(const struct pathspec *ps, int include_untracked,
12321232
}
12331233

12341234
static int save_untracked_files(struct stash_info *info, struct strbuf *msg,
1235-
struct strbuf files)
1235+
struct strbuf *files)
12361236
{
12371237
int ret = 0;
12381238
struct strbuf untracked_msg = STRBUF_INIT;
@@ -1246,7 +1246,7 @@ static int save_untracked_files(struct stash_info *info, struct strbuf *msg,
12461246
stash_index_path.buf);
12471247

12481248
strbuf_addf(&untracked_msg, "untracked files on %s\n", msg->buf);
1249-
if (pipe_command(&cp_upd_index, files.buf, files.len, NULL, 0,
1249+
if (pipe_command(&cp_upd_index, files->buf, files->len, NULL, 0,
12501250
NULL, 0)) {
12511251
ret = -1;
12521252
goto done;
@@ -1499,7 +1499,7 @@ static int do_create_stash(const struct pathspec *ps, struct strbuf *stash_msg_b
14991499
parents = NULL;
15001500

15011501
if (include_untracked) {
1502-
if (save_untracked_files(info, &msg, untracked_files)) {
1502+
if (save_untracked_files(info, &msg, &untracked_files)) {
15031503
if (!quiet)
15041504
fprintf_ln(stderr, _("Cannot save "
15051505
"the untracked files"));

0 commit comments

Comments
 (0)