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

Re: [PATCH 2/4] Use strbuf in http code

From
Junio C Hamano <gitster@pobox.com>
Date
Dec 9, 2007, 18:15 UTC
Message-ID
<7vy7c3pwek.fsf@gitster.siamese.dyndns.org>
In-Reply-To
<1197219900-19334-2-git-send-email-mh@glandium.org>
Mike Hommey <mh@glandium.org> writes:
Show 9 quoted lines
> Also, replace whitespaces with tabs in some places
>
> Signed-off-by: Mike Hommey <mh@glandium.org>
> ---
>
>  While testing this, I noticed 3 things:
>  - CURL_MULTI makes the code very racy
>  - a lot of the code doesn't do anything useful without CURL_MULTI
>  - the code is redundant

Yeah, there does seem to be a lot of duplicated code that does common setup with slightly different request string.

Show 30 quoted lines
> @@ -1115,16 +1109,11 @@ static char *quote_ref_url(const char *base, const char *ref)
>  
>  int fetch_ref(char *ref, unsigned char *sha1)
>  {
> -        char *url;
> -        char hex[42];
> -        struct buffer buffer;
> +	char *url;
> +	struct strbuf buffer = STRBUF_INIT;
>  	char *base = remote->url;
>  	struct active_request_slot *slot;
>  	struct slot_results results;
> -        buffer.size = 41;
> -        buffer.posn = 0;
> -        buffer.buffer = hex;
> -        hex[41] = '\0';
>  
>  	url = quote_ref_url(base, ref);
>  	slot = get_active_slot();
> @@ -1142,9 +1131,9 @@ int fetch_ref(char *ref, unsigned char *sha1)
>  		return error("Unable to start request");
>  	}
>  
> -        hex[40] = '\0';
> -        get_sha1_hex(hex, sha1);
> -        return 0;
> +	buffer.buf[40] = '\0';
> +	get_sha1_hex(buffer.buf, sha1);
> +	return 0;
>  }
A conversion like this is worrysome and needs to be rethought I think.

At least with the old code, we knew hex[40] was a safe location to make assignment to, even though we did not check if what it contained made sense --- the other end might have had a garbage in that URL (but the caller hopefully would be responsible for noticing that). But with your change, I do not think you have that guarantee. fwrite_buffer() may have extended the buffer using strbuf API, but it may have received less than what you are expecting, in which case you may not have buf[40] touchable for you, no?

I at the same time think the original code is buggy. It initializes buffer.buffer to the on-stack storage hex[], but lets fwrite_buffer() to call xrealloc() on it.

Previous: Junio C HamanoNext: Mike Hommey
Message 7 of 23 in “Cleanup variables in http.[ch]”
  1. 1/4 Cleanup variables in http.[ch]Mike Hommey, Dec 9, 2007
  2. 2/4 Use strbuf in http codeMike Hommey, Dec 9, 2007
  3. 3/4 Move the file read logic to read_patch_file() in builtin-apply.cMike Hommey, Dec 9, 2007
  4. 4/4 Add support for URLs to git-applyMike Hommey, Dec 9, 2007
  5. 4/4 Add support for URLs to git-applyMike Hommey, Dec 10, 2007
  6. Junio C HamanoDec 9, 2007
  7. Junio C HamanoDec 9, 2007
  8. Mike HommeyDec 9, 2007
  9. Junio C HamanoDec 9, 2007
  10. 2/4 Use strbuf in http codeMike Hommey, Dec 9, 2007
  11. Junio C HamanoDec 11, 2007
  12. Mike HommeyDec 11, 2007
  13. 2/4 Use strbuf in http codeMike Hommey, Dec 11, 2007
  14. git-send-email.perl: Really add angle brackets to In-Reply-To if necessaryMike Hommey, Dec 9, 2007
  15. Mike HommeyDec 9, 2007
  16. Junio C HamanoDec 9, 2007
  17. Mike HommeyDec 9, 2007
  18. David KastrupDec 9, 2007
  19. Randal L. SchwartzDec 9, 2007
  20. David KastrupDec 9, 2007
  21. Randal L. SchwartzDec 9, 2007
  22. git-send-email.perl: Really add angle brackets to In-Reply-To if necessaryMike Hommey, Dec 9, 2007
  23. Junio C HamanoDec 9, 2007

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.