git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH v10 19/21] stash: convert `stash--helper.c` into `stash.c`

From
Thomas Gummerer <t.gummerer@gmail.com>
Date
Oct 15, 2018, 22:03 UTC
Message-ID
<20181015220338.GB4883@hank.intra.tgummerer.com>
In-Reply-To
<004f2b2e3e8c4d1069a808034f3d6fe16853085c.1539553398.git.ungureanupaulsebastian@gmail.com>
On 10/15, Paul-Sebastian Ungureanu wrote:
Show 92 quoted lines
> 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 <ungureanupaulsebastian@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..
Show 10 quoted lines
>  
>  	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").
Previous: Paul-Sebastian UngureanuNext: Paul-Sebastian Ungureanu
Message 21 of 30 in “Convert "git stash" to C builtin”
  1. 00/21 Convert "git stash" to C builtinPaul-Sebastian Ungureanu, Oct 14, 2018
  2. 01/21 sha1-name.c: add `get_oidf()` which acts like `get_oid()`Paul-Sebastian Ungureanu, Oct 14, 2018
  3. 02/21 strbuf.c: add `strbuf_join_argv()`Paul-Sebastian Ungureanu, Oct 14, 2018
  4. 03/21 stash: improve option parsing test coveragePaul-Sebastian Ungureanu, Oct 14, 2018
  5. 04/21 t3903: modernize stylePaul-Sebastian Ungureanu, Oct 14, 2018
  6. 05/21 stash: rename test cases to be more descriptivePaul-Sebastian Ungureanu, Oct 14, 2018
  7. 06/21 stash: add tests for `git stash show` configPaul-Sebastian Ungureanu, Oct 14, 2018
  8. 09/21 stash: convert drop and clear to builtinPaul-Sebastian Ungureanu, Oct 14, 2018
  9. 07/21 stash: mention options in `show` synopsisPaul-Sebastian Ungureanu, Oct 14, 2018
  10. 10/21 stash: convert branch to builtinPaul-Sebastian Ungureanu, Oct 14, 2018
  11. 11/21 stash: convert pop to builtinPaul-Sebastian Ungureanu, Oct 14, 2018
  12. 12/21 stash: convert list to builtinPaul-Sebastian Ungureanu, Oct 14, 2018
  13. 13/21 stash: convert show to builtinPaul-Sebastian Ungureanu, Oct 14, 2018
  14. 14/21 stash: convert store to builtinPaul-Sebastian Ungureanu, Oct 14, 2018
  15. 15/21 stash: convert create to builtinPaul-Sebastian Ungureanu, Oct 14, 2018
  16. 16/21 stash: convert push to builtinPaul-Sebastian Ungureanu, Oct 14, 2018
  17. 20/21 stash: optimize `get_untracked_files()` and `check_changes()`Paul-Sebastian Ungureanu, Oct 14, 2018
  18. 21/21 stash: replace all `write-tree` child processes with API callsPaul-Sebastian Ungureanu, Oct 14, 2018
  19. 17/21 stash: make push -q quietPaul-Sebastian Ungureanu, Oct 14, 2018
  20. 19/21 stash: convert `stash--helper.c` into `stash.c`Paul-Sebastian Ungureanu, Oct 14, 2018
  21. Thomas GummererOct 15, 2018
  22. 18/21 stash: convert save to builtinPaul-Sebastian Ungureanu, Oct 14, 2018
  23. Thomas GummererOct 15, 2018
  24. 08/21 stash: convert apply to builtinPaul-Sebastian Ungureanu, Oct 14, 2018
  25. Johannes SchindelinOct 15, 2018
  26. Thomas GummererOct 15, 2018
  27. Thomas GummererOct 15, 2018
  28. Junio C HamanoOct 16, 2018
  29. Johannes SchindelinOct 16, 2018
  30. Thomas GummererOct 16, 2018

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.