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

[PATCH v2 1/6] http: simplify update_url_from_redirect

From
Jeff King <peff@peff.net>
Date
Dec 6, 2016, 18:24 UTC
Message-ID
<20161206182429.6jp23tltvlznrh2p@sigill.intra.peff.net>
In-Reply-To
<20161206182414.466uotqfufcimpqb@sigill.intra.peff.net>

This function looks for a common tail between what we asked for and where we were redirected to, but it open-codes the comparison. We can avoid some confusing subtractions by using strip_suffix_mem().

Signed-off-by: Jeff King <peff@peff.net>
---
 http.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
diff --git a/http.c b/http.c
index d8e427b69..d4034a14b 100644
--- a/http.c
+++ b/http.c
@@ -1500,7 +1500,7 @@ static int update_url_from_redirect(struct strbuf *base,
 				    const struct strbuf *got)
 {
 	const char *tail;
-	size_t tail_len;
+	size_t new_len;
 
 	if (!strcmp(asked, got->buf))
 		return 0;
@@ -1509,14 +1509,12 @@ static int update_url_from_redirect(struct strbuf *base,
 		die("BUG: update_url_from_redirect: %s is not a superset of %s",
 		    asked, base->buf);
 
-	tail_len = strlen(tail);
-
-	if (got->len < tail_len ||
-	    strcmp(tail, got->buf + got->len - tail_len))
+	new_len = got->len;
+	if (!strip_suffix_mem(got->buf, &new_len, tail))
 		return 0; /* insane redirect scheme */
 
 	strbuf_reset(base);
-	strbuf_add(base, got->buf, got->len - tail_len);
+	strbuf_add(base, got->buf, new_len);
 	return 1;
 }
 
-- 
2.11.0.191.gdb26c57
Previous: Jeff KingNext: Jeff King
Message 2 of 7 in “jk/http-walker-limit-redirect rebased to maint-2.9”
  1. jk/http-walker-limit-redirect rebased to maint-2.9Jeff King, Dec 6, 2016
  2. 1/6 http: simplify update_url_from_redirectJeff King, Dec 6, 2016
  3. 3/6 remote-curl: rename shadowed options variableJeff King, Dec 6, 2016
  4. 2/6 http: always update the base URL for redirectsJeff King, Dec 6, 2016
  5. 4/6 http: make redirects more obviousJeff King, Dec 6, 2016
  6. 5/6 http: treat http-alternates like redirectsJeff King, Dec 6, 2016
  7. 6/6 http-walker: complain about non-404 loose object errorsJeff King, Dec 6, 2016

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.