On 10/15, Paul-Sebastian Ungureanu wrote:
> The old shell script `git-stash.sh`  was removed and replaced
> entirely by `builtin/stash.c`. In order to do that, `create` and
> `push` were adapted to work without `stash.sh`. For example, before
> this commit, `git stash create` called `git stash--helper create
> --message "$*"`. If it called `git stash--helper create "$@"`, then
> some of these changes wouldn't have been necessary.
> 
> This commit also removes the word `helper` since now stash is
> called directly and not by a shell script.
> 
> Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebast...@gmail.com>
> ---
> @@ -1138,7 +1133,6 @@ static int do_create_stash(struct pathspec ps, char 
> **stash_msg,
>                       fprintf_ln(stderr, _("You do not have "
>                                            "the initial commit yet"));
>               ret = -1;
> -             *stash_msg = NULL;
>               goto done;
>       } else {
>               head_commit = lookup_commit(the_repository, &info->b_commit);
> @@ -1146,7 +1140,6 @@ static int do_create_stash(struct pathspec ps, char 
> **stash_msg,
>  
>       if (!check_changes(ps, include_untracked)) {
>               ret = 1;
> -             *stash_msg = NULL;
>               goto done;
>       }
>  
> @@ -1167,7 +1160,6 @@ static int do_create_stash(struct pathspec ps, char 
> **stash_msg,
>                       fprintf_ln(stderr, _("Cannot save the current "
>                                            "index state"));
>               ret = -1;
> -             *stash_msg = NULL;
>               goto done;
>       }
>  
> @@ -1178,14 +1170,12 @@ static int do_create_stash(struct pathspec ps, char 
> **stash_msg,
>                               fprintf_ln(stderr, _("Cannot save "
>                                                    "the untracked files"));
>                       ret = -1;
> -                     *stash_msg = NULL;
>                       goto done;
>               }
>               untracked_commit_option = 1;
>       }
>       if (patch_mode) {
>               ret = stash_patch(info, ps, patch, quiet);
> -             *stash_msg = NULL;
>               if (ret < 0) {
>                       if (!quiet)
>                               fprintf_ln(stderr, _("Cannot save the current "
> @@ -1200,7 +1190,6 @@ static int do_create_stash(struct pathspec ps, char 
> **stash_msg,
>                               fprintf_ln(stderr, _("Cannot save the current "
>                                                    "worktree state"));
>                       ret = -1;
> -                     *stash_msg = NULL;
>                       goto done;
>               }
>       }
> @@ -1210,7 +1199,7 @@ static int do_create_stash(struct pathspec ps, char 
> **stash_msg,
>       else
>               strbuf_addf(&stash_msg_buf, "On %s: %s", branch_name,
>                           *stash_msg);
> -     *stash_msg = strbuf_detach(&stash_msg_buf, NULL);
> +     *stash_msg = xstrdup(stash_msg_buf.buf);
>  
>       /*
>        * `parents` will be empty after calling `commit_tree()`, so there is
> @@ -1244,30 +1233,23 @@ static int do_create_stash(struct pathspec ps, char 
> **stash_msg,
>  
>  static int create_stash(int argc, const char **argv, const char *prefix)
>  {
> -     int include_untracked = 0;
>       int ret = 0;
>       char *stash_msg = NULL;
>       struct stash_info info;
>       struct pathspec ps;
> -     struct option options[] = {
> -             OPT_BOOL('u', "include-untracked", &include_untracked,
> -                      N_("include untracked files in stash")),
> -             OPT_STRING('m', "message", &stash_msg, N_("message"),
> -                      N_("stash message")),
> -             OPT_END()
> -     };
> +     struct strbuf stash_msg_buf = STRBUF_INIT;
>  
> -     argc = parse_options(argc, argv, prefix, options,
> -                          git_stash_helper_create_usage,
> -                          0);
> +     /* Starting with argv[1], since argv[0] is "create" */
> +     strbuf_join_argv(&stash_msg_buf, argc - 1, ++argv, ' ');
> +     stash_msg = stash_msg_buf.buf;

stash_msg is just a pointer to stash_msg_buf.buf here..
>  
>       memset(&ps, 0, sizeof(ps));
> -     ret = do_create_stash(ps, &stash_msg, include_untracked, 0, &info,
> -                           NULL, 0);
> +     ret = do_create_stash(ps, &stash_msg, 0, 0, &info, NULL, 0);
>  
>       if (!ret)
>               printf_ln("%s", oid_to_hex(&info.w_commit));
>  
> +     strbuf_release(&stash_msg_buf);

We release the strbuf here, which means stash_msg_buf.buf is now
'strbuf_slopbuf', which is a global variable and can't be free'd.  If
stash_msg is not changed within do_create_stash, it is now pointing to
'strbuf_slopbuf', and we try to free that below, which makes git
crash in t3903.44, which breaks bisection.

>       free(stash_msg);
>  
>       /*

I think the following diff fixes memory management, by making
do_push_stash responsible for freeing stash_msg when it's done with
it, while the callers of do_create_stash have to free the parameter
they pass in (although it may be pointing to something different than
what was passed in).

diff --git a/builtin/stash.c b/builtin/stash.c
index e945c13c42..3b50f4bd53 100644
--- a/builtin/stash.c
+++ b/builtin/stash.c
@@ -1199,6 +1199,7 @@ static int do_create_stash(struct pathspec ps, char 
**stash_msg,
        else
                strbuf_addf(&stash_msg_buf, "On %s: %s", branch_name,
                            *stash_msg);
+       free(*stash_msg);
        *stash_msg = xstrdup(stash_msg_buf.buf);
 
        /*
@@ -1241,7 +1242,7 @@ static int create_stash(int argc, const char **argv, 
const char *prefix)
 
        /* Starting with argv[1], since argv[0] is "create" */
        strbuf_join_argv(&stash_msg_buf, argc - 1, ++argv, ' ');
-       stash_msg = stash_msg_buf.buf;
+       stash_msg = xstrdup(stash_msg_buf.buf);
 
        memset(&ps, 0, sizeof(ps));
        ret = do_create_stash(ps, &stash_msg, 0, 0, &info, NULL, 0);
@@ -1522,7 +1523,7 @@ static int save_stash(int argc, const char **argv, const 
char *prefix)
        }
 
        memset(&ps, 0, sizeof(ps));
-       ret = do_push_stash(ps, stash_msg, quiet, keep_index, patch_mode,
+       ret = do_push_stash(ps, xstrdup_or_null(stash_msg), quiet, keep_index, 
patch_mode,
                            include_untracked);
 
        strbuf_release(&buf);

The above, as well as the some of the changes I quoted above should
probably be squashed in to the relevant patches, rather than being
part of this patch ("stash: convert create to builtin", and "stash:
convert save to builtin").

Reply via email to