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

Re: [PATCH 2/5] path.c: Don't discard the return value of vsnpath()

From
Ramsay Jones <ramsay@ramsay1.demon.co.uk>
Date
Sep 7, 2012, 19:52 UTC
Message-ID
<504A507F.9030802@ramsay1.demon.co.uk>
In-Reply-To
<7vipbt8q7r.fsf@alter.siamese.dyndns.org>
Junio C Hamano wrote:
Show 13 quoted lines
> cleanup_path() is a quick-and-dirty hack that only deals with
> leading ".///" (e.g. "foo//bar" is not reduced to "foo/bar"), and
> the callers that allocate 4 bytes for buf to hold "foo" may not be
> able to fit it if for some reason there are some bytes that must be
> cleaned, e.g. ".///foo".
> 
> The worst part of its use is that buf and ret could be different,
> which means you cannot safely do:
> 
> 	char *buf = xmalloc(...);
>         buf = git_snpath(buf, n, "%s", ...);
>         ... use buf ...
>         free(buf);

Hmm, this seems very unnatural to me; it would never have occurred to me to use anything other than a stack allocated buffer (or *maybe* a global buffer) when calling git_snpath().

In this situation (ie a dynamically allocated buffer used to hold the result), why would you not use git_pathdup()? (which does not suffer this problem.)

I guess it does not matter what I find unnatural, ... :(
Show 9 quoted lines
> but instead have to do something like:
> 
> 	char *path, *buf = xmalloc(...);
>         path = git_snpath(buf, n, "%s", ...);
>         ... use path ...
>         free(buf);
> 
> And this series goes in a direction of making more callers aware of
> the twisted calling convention, which smells really bad.

Note that, prior to commit aba13e7c ("git_pathdup: returns xstrdup-ed copy of the formatted path", 27-10-2008), git_snpath() was calling cleanup_path() in the non-error return path. I suspect that this commit did not intend to "fix the git_snpath() interface" and only did so by accident. (That's a guess, of course)

However, I much prefer git_snpath(), git_pathdup() and git_path() return the same result string given the same inputs! :D

ATB, Ramsay Jones

Previous: Junio C Hamano
Message 3 of 3 in “path.c: Don't discard the return value of vsnpath()”
  1. 2/5 path.c: Don't discard the return value of vsnpath()Ramsay Jones, Sep 4, 2012
  2. Junio C HamanoSep 4, 2012
  3. Ramsay JonesSep 7, 2012

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.