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

Re: [PATCH v2] urlmatch.c: recompute ptr after append_normalized_escapes

From
Junio C Hamano <gitster@pobox.com>
Date
Sep 12, 2013, 18:30 UTC
Message-ID
<xmqqioy5q4dd.fsf@gitster.dls.corp.google.com>
In-Reply-To
<c8915eaaf877abe0e69864ffdc6c50f@f74d39fa044aa309eaea14b9f57fe79>
"Kyle J. McKay" <mackyle@gmail.com> writes:
Show 39 quoted lines
> So how about this patch instead...
>
> -- 8< --
> From: Thomas Rast <trast@inf.ethz.ch>
> Subject: urlmatch.c: recompute pointer after append_normalized_escapes
>
> When append_normalized_escapes is called, its internal strbuf_add* calls can
> cause the strbuf's buf to be reallocated changing the value of the buf pointer.
>
> Do not use the strbuf buf pointer from before any append_normalized_escapes
> calls afterwards.  Instead recompute the needed pointer.
>
> Signed-off-by: Thomas Rast <trast@inf.ethz.ch>
> Signed-off-by: Kyle J. McKay <mackyle@gmail.com>
> ---
>  urlmatch.c | 5 ++++-
>  1 file changed, 4 insertions(+), 1 deletion(-)
>
> diff --git a/urlmatch.c b/urlmatch.c
> index 1db76c89..01c67467 100644
> --- a/urlmatch.c
> +++ b/urlmatch.c
> @@ -281,8 +281,9 @@ char *url_normalize(const char *url, struct url_info *out_info)
>  		url_len--;
>  	}
>  	for (;;) {
> -		const char *seg_start = norm.buf + norm.len;
> +		const char *seg_start;
> +		size_t seg_start_off = norm.len;
>  		const char *next_slash = url + strcspn(url, "/?#");
>  		int skip_add_slash = 0;
>  		/*
>  		 * RFC 3689 indicates that any . or .. segments should be
> @@ -297,6 +298,8 @@ char *url_normalize(const char *url, struct url_info *out_info)
>  			strbuf_release(&norm);
>  			return NULL;
>  		}
> +		/* append_normalized_escapes can cause norm.buf to change */
> +		seg_start = norm.buf + seg_start_off;

The change looks good, but I find that this comment is not placed in the right place. It is good if the reader knows about an old bug to put it here, but if the first thing a reader reads is this updated version, the comment is better placed close to the place where the start_ofs variable captures the original value (i.e. "because the next call may relocate the buffer, we cannot grab seg_start upfront; instead we need to record the start_ofs here, and that is what this variable is about").

It is too minor a point for a reroll, so I'll try to tweak it locally. Something like this (but now I think about it, the comment may not even be necessary).

 urlmatch.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
diff --git a/urlmatch.c b/urlmatch.c
index 01c6746..d1600e2 100644
--- a/urlmatch.c
+++ b/urlmatch.c
@@ -282,9 +282,17 @@ char *url_normalize(const char *url, struct url_info *out_info)
 	}
 	for (;;) {
 		const char *seg_start;
-		size_t seg_start_off = norm.len;
+		size_t seg_start_off;
 		const char *next_slash = url + strcspn(url, "/?#");
 		int skip_add_slash = 0;
+
+		/*
+		 * record the starting offset; appending escapes may
+		 * relocate the buffer, so we cannot capture seg_start
+		 * upfront and use it later.
+		 */
+		seg_start_off = norm.len;
+
 		/*
 		 * RFC 3689 indicates that any . or .. segments should be
 		 * unescaped before being checked for.
@@ -298,7 +306,7 @@ char *url_normalize(const char *url, struct url_info *out_info)
 			strbuf_release(&norm);
 			return NULL;
 		}
-		/* append_normalized_escapes can cause norm.buf to change */
+
 		seg_start = norm.buf + seg_start_off;
 		if (!strcmp(seg_start, ".")) {
 			/* ignore a . segment; be careful not to remove initial '/' */
Previous: Junio C HamanoNext: Kyle J. McKay
Message 5 of 8 in “urlmatch: append_normalized_escapes can reallocate norm.buf”
  1. urlmatch: append_normalized_escapes can reallocate norm.bufThomas Rast, Sep 12, 2013
  2. urlmatch.c: recompute ptr after append_normalized_escapesKyle J. McKay, Sep 12, 2013
  3. Thomas RastSep 12, 2013
  4. Junio C HamanoSep 12, 2013
  5. Junio C HamanoSep 12, 2013
  6. Kyle J. McKaySep 12, 2013
  7. Jonathan NiederSep 12, 2013
  8. Junio C HamanoSep 12, 2013

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.