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

Re: [PATCH 1/2] sequencer: stop exporting GIT_REFLOG_ACTION

From
Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Date
Nov 7, 2022, 16:12 UTC
Message-ID
<221107.86iljqvhzf.gmgdl@evledraar.gmail.com>
In-Reply-To
<e9c3f5ac5c6b7a01ee9e43730889d8066a270271.1667575142.git.gitgitgadget@gmail.com>
On Fri, Nov 04 2022, Phillip Wood via GitGitGadget wrote:
> From: Phillip Wood <phillip.wood@dunelm.org.uk>
Show 11 quoted lines
> +static const char *sequencer_reflog_action(struct replay_opts *opts)
> +{
> +	if (!opts->reflog_action) {
> +		opts->reflog_action = getenv(GIT_REFLOG_ACTION);
> +		opts->reflog_action =
> +			xstrdup(opts->reflog_action ? opts->reflog_action
> +						    : action_name(opts));
> +	}
> +
> +	return opts->reflog_action;
> +}

We always return an xstrdup'd here, so this should be a "char *" return value, not a "const char *"., but

Show 10 quoted lines
>  __attribute__((format (printf, 3, 4)))
>  static const char *reflog_message(struct replay_opts *opts,
>  	const char *sub_action, const char *fmt, ...)
>  {
>  	va_list ap;
>  	static struct strbuf buf = STRBUF_INIT;
> -	char *reflog_action = getenv(GIT_REFLOG_ACTION);
>  
>  	va_start(ap, fmt);
>  	strbuf_reset(&buf);
Here we just reset the strbuf...
> -	strbuf_addstr(&buf, reflog_action ? reflog_action : action_name(opts));
> +	strbuf_addstr(&buf, sequencer_reflog_action(opts));
Here we leak the freshly xstrdup'd value, mostly untested, but shouldn't
this instead be:
	
	diff --git a/sequencer.c b/sequencer.c
	index e23f6f0b718..58a97e04c67 100644
	--- a/sequencer.c
	+++ b/sequencer.c
	@@ -3695,10 +3695,11 @@ static const char *reflog_message(struct replay_opts *opts,
	 {
	 	va_list ap;
	 	static struct strbuf buf = STRBUF_INIT;
	+	char *msg = sequencer_reflog_action(opts);
	 
	 	va_start(ap, fmt);
	 	strbuf_reset(&buf);
	-	strbuf_addstr(&buf, sequencer_reflog_action(opts));
	+	strbuf_attach(&buf, msg, strlen(msg), strlen(msg) + 1);
	 	if (sub_action)
	 		strbuf_addf(&buf, " (%s)", sub_action);
	 	if (fmt) {
Of course that requires dropping the "const", per the above...
Show 9 quoted lines
>  	if (sub_action)
>  		strbuf_addf(&buf, " (%s)", sub_action);
>  	if (fmt) {
> @@ -4497,7 +4511,7 @@ static int checkout_onto(struct repository *r, struct replay_opts *opts,
>  				RESET_HEAD_RUN_POST_CHECKOUT_HOOK,
>  		.head_msg = reflog_message(opts, "start", "checkout %s",
>  					   onto_name),
> -		.default_reflog_action = "rebase"
> +		.default_reflog_action = sequencer_reflog_action(opts)

Here we'd before hand a fixed string to reset_head(), but now it's xstrdup()'d, but the corresponding free() on that side is missing.

But aren't we always just returing "rebase" here still?
Show 8 quoted lines
> [...]
> @@ -5116,7 +5121,7 @@ static int single_pick(struct repository *r,
>  			TODO_PICK : TODO_REVERT;
>  	item.commit = cmit;
>  
> -	setenv(GIT_REFLOG_ACTION, action_name(opts), 0);
> +	opts->reflog_message = sequencer_reflog_action(opts);
>  	return do_pick_commit(r, &item, opts, 0, &check_todo);

Here you're adding a new memory leak, which you can see if you run e.g. the 1st test of ./t1013-read-tree-submodule.sh before & after this change.

Previous: Taylor BlauNext: Phillip Wood
Message 4 of 18 in “rebase: stop setting GIT_REFLOG_ACTION”
  1. 0/2 rebase: stop setting GIT_REFLOG_ACTIONPhillip Wood via GitGitGadget, Nov 4, 2022
  2. 1/2 sequencer: stop exporting GIT_REFLOG_ACTIONPhillip Wood via GitGitGadget, Nov 4, 2022
  3. Taylor BlauNov 4, 2022
  4. Ævar Arnfjörð BjarmasonNov 7, 2022
  5. Phillip WoodNov 7, 2022
  6. Phillip WoodNov 8, 2022
  7. Ævar Arnfjörð BjarmasonNov 8, 2022
  8. 2/2 rebase: stop exporting GIT_REFLOG_ACTIONPhillip Wood via GitGitGadget, Nov 4, 2022
  9. Taylor BlauNov 4, 2022
  10. Taylor BlauNov 4, 2022
  11. Ævar Arnfjörð BjarmasonNov 7, 2022
  12. Taylor BlauNov 7, 2022
  13. 0/2 rebase: stop setting GIT_REFLOG_ACTIONPhillip Wood via GitGitGadget, Nov 9, 2022
  14. 1/2 sequencer: stop exporting GIT_REFLOG_ACTIONPhillip Wood via GitGitGadget, Nov 9, 2022
  15. 2/2 rebase: stop exporting GIT_REFLOG_ACTIONPhillip Wood via GitGitGadget, Nov 9, 2022
  16. Ævar Arnfjörð BjarmasonNov 9, 2022
  17. Phillip WoodNov 9, 2022
  18. Taylor BlauNov 9, 2022

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.