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

Re: [PATCH 3/6] config: drop useless length variable in write_pair()

From
Junio C Hamano <gitster@pobox.com>
Date
Apr 10, 2020, 21:51 UTC
Message-ID
<xmqq4ktrhuw7.fsf@gitster.c.googlers.com>
In-Reply-To
<20200410194445.GC1363756@coredump.intra.peff.net>
Jeff King <peff@peff.net> writes:
> We compute the length of a subset of a string, but then use that length
> only to feed a "%.*s" printf placeholder for the same string. We can
> just use "%s" to achieve the same thing.

Heh, makes readers wonder why the original author wrote such a convoluted code.

> The variable became useless in cb891a5989 (Use a strbuf for building up
> section header and key/value pair strings., 2007-12-14), which swapped
> out a write() which _did_ use the length for a strbuf_addf() call.

And that history, i.e. the %.*s formatter being a direct translation from write(2), explains it.

Thanks.
Show 27 quoted lines
> Signed-off-by: Jeff King <peff@peff.net>
> ---
>  config.c | 4 +---
>  1 file changed, 1 insertion(+), 3 deletions(-)
>
> diff --git a/config.c b/config.c
> index ff7998df46..7ea588a7e0 100644
> --- a/config.c
> +++ b/config.c
> @@ -2545,7 +2545,6 @@ static ssize_t write_pair(int fd, const char *key, const char *value,
>  {
>  	int i;
>  	ssize_t ret;
> -	int length = strlen(key + store->baselen + 1);
>  	const char *quote = "";
>  	struct strbuf sb = STRBUF_INIT;
>  
> @@ -2564,8 +2563,7 @@ static ssize_t write_pair(int fd, const char *key, const char *value,
>  	if (i && value[i - 1] == ' ')
>  		quote = "\"";
>  
> -	strbuf_addf(&sb, "\t%.*s = %s",
> -		    length, key + store->baselen + 1, quote);
> +	strbuf_addf(&sb, "\t%s = %s", key + store->baselen + 1, quote);
>  
>  	for (i = 0; value[i]; i++)
>  		switch (value[i]) {
Previous: Jeff KingNext: Jeff King
Message 6 of 16 in “better handling of gigantic config files”
  1. 0/6 better handling of gigantic config filesJeff King, Apr 10, 2020
  2. 1/6 remote: drop auto-strlen behavior of make_branch() and make_rewrite()Jeff King, Apr 10, 2020
  3. Junio C HamanoApr 10, 2020
  4. 2/6 parse_config_key(): return subsection len as size_tJeff King, Apr 10, 2020
  5. 3/6 config: drop useless length variable in write_pair()Jeff King, Apr 10, 2020
  6. Junio C HamanoApr 10, 2020
  7. 4/6 git_config_parse_key(): return baselen as size_tJeff King, Apr 10, 2020
  8. 5/6 config: use size_t to store parsed variable baselenJeff King, Apr 10, 2020
  9. 6/6 config: reject parsing of files over INT_MAXJeff King, Apr 10, 2020
  10. Junio C HamanoApr 10, 2020
  11. Jeff KingApr 10, 2020
  12. Taylor BlauApr 13, 2020
  13. Junio C HamanoApr 13, 2020
  14. Taylor BlauApr 13, 2020
  15. Jeff KingApr 13, 2020
  16. Taylor BlauApr 13, 2020

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.