http-fetch: fix alternates handling.
Fetch over http from a repository that uses alternates to borrow from neighbouring repositories were quite broken, apparently for some time now. We parse input and count bytes to allocate the new buffer, and when we copy into that buffer we know exactly how many bytes we want to copy from where. Using strlcpy for it was simply stupid, and the code forgot to take it into account that strlcpy terminated the string with NUL. Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
Sep 12, 2006 at 23:53 UTC
5df1e0d05cc42aeef9bf2fce243375fb6dac2121
1 file changed
+34
-5
http-fetch.c
+34
-5
@@ -559,9 +559,36 @@ static void process_alternates_response(void *callback_data)
559
char *target = NULL;
560
char *path;
561
if (data[i] == '/') {
562
- serverlen = strchr(base + 8, '/') - base;
563
- okay = 1;
562
+ /* This counts
563
+ * http://git.host/pub/scm/linux.git/
564
+ * -----------here^
565
+ * so memcpy(dst, base, serverlen) will
566
+ * copy up to "...git.host".
567
+ */
568
+ const char *colon_ss = strstr(base,"://");
569
+ if (colon_ss) {
570
+ serverlen = (strchr(colon_ss + 3, '/')
571
+ - base);
572
+ okay = 1;
573
+ }
574
} else if (!memcmp(data + i, "../", 3)) {
575
+ /* Relative URL; chop the corresponding
576
+ * number of subpath from base (and ../
577
+ * from data), and concatenate the result.
578
+ *
579
+ * The code first drops ../ from data, and
580
+ * then drops one ../ from data and one path
581
+ * from base. IOW, one extra ../ is dropped
582
+ * from data than path is dropped from base.
583
+ *
584
+ * This is not wrong. The alternate in
585
+ * http://git.host/pub/scm/linux.git/
586
+ * to borrow from
587
+ * http://git.host/pub/scm/linus.git/
588
+ * is ../../linus.git/objects/. You need
589
+ * two ../../ to borrow from your direct
590
+ * neighbour.
591
+ */
592
i += 3;
593
serverlen = strlen(base);
594
while (i + 2 < posn &&
@@ -583,11 +610,13 @@ static void process_alternates_response(void *callback_data)
610
okay = 1;
611
}
612
}
586
- /* skip 'objects' at end */
613
+ /* skip "objects\n" at end */
614
if (okay) {
615
target = xmalloc(serverlen + posn - i - 6);
589
- strlcpy(target, base, serverlen);
590
- strlcpy(target + serverlen, data + i, posn - i - 6);
616
+ memcpy(target, base, serverlen);
617
+ memcpy(target + serverlen, data + i,
618
+ posn - i - 7);
619
+ target[serverlen + posn - i - 7] = 0;
620
if (get_verbosely)
621
fprintf(stderr,
622
"Also look at %s\n", target);