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

Re: [PATCH v2 1/2] apply: avoid fixed-size buffer in create_one_file()

From
Junio C Hamano <gitster@pobox.com>
Date
Apr 5, 2024, 16:51 UTC
Message-ID
<xmqqfrvzwy5w.fsf@gitster.g>
In-Reply-To
<1b5f3b1d-60e2-4fe7-9ac8-a63ad861cd16@web.de>
René Scharfe <l.s.r@web.de> writes:
> Changes since v1:
> - Split out removal of mksnpath() into a separate patch.
> - Use errno only before calling free(3).

Relative to the previous round, the body of the loop has become much easier to follow. Nice clean-up.

Thanks, both.
Show 50 quoted lines
>  apply.c | 15 +++++++++------
>  1 file changed, 9 insertions(+), 6 deletions(-)
>
> diff --git a/apply.c b/apply.c
> index 432837a674..e171b42904 100644
> --- a/apply.c
> +++ b/apply.c
> @@ -4441,6 +4441,7 @@ static int create_one_file(struct apply_state *state,
>  			   const char *buf,
>  			   unsigned long size)
>  {
> +	char *newpath = NULL;
>  	int res;
>
>  	if (state->cached)
> @@ -4502,24 +4503,26 @@ static int create_one_file(struct apply_state *state,
>  		unsigned int nr = getpid();
>
>  		for (;;) {
> -			char newpath[PATH_MAX];
> -			mksnpath(newpath, sizeof(newpath), "%s~%u", path, nr);
> +			newpath = mkpathdup("%s~%u", path, nr);
>  			res = try_create_file(state, newpath, mode, buf, size);
>  			if (res < 0)
> -				return -1;
> +				goto out;
>  			if (!res) {
>  				if (!rename(newpath, path))
> -					return 0;
> +					goto out;
>  				unlink_or_warn(newpath);
>  				break;
>  			}
>  			if (errno != EEXIST)
>  				break;
>  			++nr;
> +			FREE_AND_NULL(newpath);
>  		}
>  	}
> -	return error_errno(_("unable to write file '%s' mode %o"),
> -			   path, mode);
> +	res = error_errno(_("unable to write file '%s' mode %o"), path, mode);
> +out:
> +	free(newpath);
> +	return res;
>  }
>
>  static int add_conflicted_stages_file(struct apply_state *state,
> --
> 2.44.0
Previous: Jeff King
Message 15 of 15 in “apply: replace mksnpath() with a mkpathdup() call”
  1. apply: replace mksnpath() with a mkpathdup() callRené Scharfe, Apr 4, 2024
  2. Junio C HamanoApr 4, 2024
  3. free and errno, was Re: [PATCH] apply: replace mksnpath() with a mkpathdup() callJeff King, Apr 4, 2024
  4. Junio C HamanoApr 4, 2024
  5. René ScharfeApr 5, 2024
  6. Jeff KingApr 5, 2024
  7. Jeff KingApr 5, 2024
  8. René ScharfeApr 6, 2024
  9. Jeff KingApr 7, 2024
  10. René ScharfeApr 14, 2024
  11. Jeff KingApr 24, 2024
  12. 1/2 apply: avoid fixed-size buffer in create_one_file()René Scharfe, Apr 5, 2024
  13. 2/2 path: remove mksnpath()René Scharfe, Apr 5, 2024
  14. Jeff KingApr 5, 2024
  15. Junio C HamanoApr 5, 2024

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.