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

Re: [PATCH 2/3] strbuf: add strbuf_percentquote_buf

From
Junio C Hamano <gitster@pobox.com>
Date
Jan 13, 2010, 06:55 UTC
Message-ID
<7viqb6trwu.fsf@alter.siamese.dyndns.org>
In-Reply-To
<20100112154153.GB24957@coredump.intra.peff.net>
Jeff King <peff@peff.net> writes:
Show 20 quoted lines
> +`strbuf_percentquote_buf`::
> +
> +	Append the contents of one strbuf to another, quoting any
> +	percent signs ("%") into double-percents ("%%") in the
> +	destination. This is useful for literal data to be fed to either
> +	strbuf_expand or to the *printf family of functions.
> +
>  `strbuf_addf`::
>  
>  	Add a formatted string to the buffer.
> diff --git a/strbuf.c b/strbuf.c
> index 6cbc1fc..b5183c6 100644
> --- a/strbuf.c
> +++ b/strbuf.c
> @@ -257,6 +257,16 @@ size_t strbuf_expand_dict_cb(struct strbuf *sb, const char *placeholder,
>  	return 0;
>  }
>  
> +void strbuf_percentquote_buf(struct strbuf *dest, struct strbuf *src)
> +{

Just a style thing, but please call that "dst" to be consistent. You are already dropping vowels from the other side to spell it "src".

I wondered if the function should be just 1-arg that always quotes in-place instead, but your [PATCH 3/3] wants to have an appending semantics from this function, so changing it to be a 1-arg "in-place quoter" will force the caller to run strbuf_addbuf() on the result, which is not nice.

Since tucking a p-quoted version of the same string to its original doesn't make sense at all, perhaps this should:

 (0) be renamed to have "append" somewhere in its name;
 (1) mark the src side as const; and
 (2) perhaps have assert(dst != src).  The loop won't terminate when
     called with src == dst, I think.

There seems to be only one other strbuf function that takes two strbufs in the suite (strbuf_addbuf), and I think it is unsafe in a different way, which is trivial to fix.

-- >8 --
Subject: [PATCH] strbuf_addbuf(): allow passing the same buf to dst and src

If sb and sb2 are the same (i.e. doubling the string), the underlying strbuf_add() will make sb2->buf invalid by calling strbuf_grow(sb) at the beginning and will read from the freed buffer.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 strbuf.h |    8 +++++++-
 1 files changed, 7 insertions(+), 1 deletions(-)
diff --git a/strbuf.h b/strbuf.h
index fa07ecf..e272359 100644
--- a/strbuf.h
+++ b/strbuf.h
@@ -105,7 +105,13 @@ static inline void strbuf_addstr(struct strbuf *sb, const char *s) {
 	strbuf_add(sb, s, strlen(s));
 }
 static inline void strbuf_addbuf(struct strbuf *sb, const struct strbuf *sb2) {
-	strbuf_add(sb, sb2->buf, sb2->len);
+	char *buf = sb2->buf;
+	int len = sb2->len;
+	if (sb->buf == sb2->buf) {
+		strbuf_grow(sb, len);
+		buf = sb->buf;
+	}
+	strbuf_add(sb, buf, len);
 }
 extern void strbuf_adddup(struct strbuf *sb, size_t pos, size_t len);
 
-- 
1.6.6.280.ge295b7.dirty
Previous: Jeff KingNext: Jeff King
Message 25 of 54 in “edit Author/Date metadata as part of 'git commit' $EDITOR invocation?”
  1. Adam MegaczJan 3, 2010
  2. Sverre RabbelierJan 4, 2010
  3. Adam MegaczJan 4, 2010
  4. Sverre RabbelierJan 4, 2010
  5. David AguilarJan 5, 2010
  6. Nanako ShiraishiJan 5, 2010
  7. Junio C HamanoJan 6, 2010
  8. Adam MegaczJan 8, 2010
  9. Junio C HamanoJan 8, 2010
  10. 1/3 ident.c: remove unused variablesJunio C Hamano, Jan 8, 2010
  11. 2/3 ident.c: check explicit identity for name and email separatelyJunio C Hamano, Jan 8, 2010
  12. Santi BéjarJan 8, 2010
  13. 3/3 ident.c: treat $EMAIL as giving user.email identity explicitlyJunio C Hamano, Jan 8, 2010
  14. Display author and committer after "git commit"Adam Megacz, Jan 11, 2010
  15. Adam MegaczJan 11, 2010
  16. Junio C HamanoJan 11, 2010
  17. Adam MegaczJan 12, 2010
  18. Jeff KingJan 12, 2010
  19. Jeff KingJan 12, 2010
  20. Jeff KingJan 12, 2010
  21. 1/3 strbuf_expand: convert "%%" to "%"Jeff King, Jan 12, 2010
  22. 2/3 strbuf: add strbuf_percentquote_bufJeff King, Jan 12, 2010
  23. Johannes SchindelinJan 12, 2010
  24. Jeff KingJan 12, 2010
  25. Junio C HamanoJan 13, 2010
  26. Jeff KingJan 13, 2010
  27. Junio C HamanoJan 13, 2010
  28. Jeff KingJan 13, 2010
  29. 3/3 commit: show interesting ident information in summaryJeff King, Jan 12, 2010
  30. Junio C HamanoJan 13, 2010
  31. Jeff KingJan 13, 2010
  32. Junio C HamanoJan 13, 2010
  33. Jeff KingJan 13, 2010
  34. Jeff KingJan 13, 2010
  35. Junio C HamanoJan 13, 2010
  36. Jeff KingJan 13, 2010
  37. 1/3 strbuf_expand: convert "%%" to "%"Jeff King, Jan 13, 2010
  38. Chris JohnsenJan 14, 2010
  39. Jeff KingJan 14, 2010
  40. 2/3 strbuf: add strbuf_addbuf_percentquoteJeff King, Jan 13, 2010
  41. 3/3 commit: show interesting ident information in summaryJeff King, Jan 13, 2010
  42. Wincent ColaiutaJan 13, 2010
  43. Jeff KingJan 13, 2010
  44. Wincent ColaiutaJan 13, 2010
  45. Thomas RastJan 14, 2010
  46. Felipe ContrerasJan 14, 2010
  47. Junio C HamanoJan 14, 2010
  48. Felipe ContrerasJan 14, 2010
  49. Junio C HamanoJan 14, 2010
  50. Felipe ContrerasJan 15, 2010
  51. Adam MegaczJan 16, 2010
  52. Matthieu MoyJan 17, 2010
  53. Junio C HamanoJan 17, 2010
  54. Jeff KingJan 17, 2010

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.