git-http-fetch: Remove size limit for objects/info/{packs,alternates}
git-http-fetch received objects/info/packs into a fixed-size buffer and started to fail when this file became larger than the buffer. Change it to grow the buffer dynamically, and do the same thing for objects/info/alternates. Also add missing free() calls for these buffers. Signed-off-by: Sergey Vlasov <vsu@altlinux.ru> Signed-off-by: Junio C Hamano <junkio@cox.net>
Sergey Vlasov committed
Oct 13, 2005 at 10:49 UTC
bc8f26527064e8bd9123c293741a0a3762b8a64e
1 file changed
+36
-7
http-fetch.c
+36
-7
@@ -110,6 +110,22 @@ static size_t fwrite_buffer(void *ptr, size_t eltsize, size_t nmemb,
110
return size;
111
}
112
113
+static size_t fwrite_buffer_dynamic(const void *ptr, size_t eltsize,
114
+ size_t nmemb, struct buffer *buffer)
115
+{
116
+ size_t size = eltsize * nmemb;
117
+ if (size > buffer->size - buffer->posn) {
118
+ buffer->size = buffer->size * 3 / 2;
119
+ if (buffer->size < buffer->posn + size)
120
+ buffer->size = buffer->posn + size;
121
+ buffer->buffer = xrealloc(buffer->buffer, buffer->size);
122
+ }
123
+ memcpy(buffer->buffer + buffer->posn, ptr, size);
124
+ buffer->posn += size;
125
+ data_received++;
126
+ return size;
127
+}
128
+
129
static size_t fwrite_sha1_file(void *ptr, size_t eltsize, size_t nmemb,
130
void *data)
131
{
@@ -618,11 +634,12 @@ static int fetch_alternates(char *base)
634
int i = 0;
635
int http_specific = 1;
636
struct alt_base *tail = alt;
637
+ static const char null_byte = '\0';
638
639
struct active_request_slot *slot;
640
641
data = xmalloc(4096);
625
- buffer.size = 4095;
642
+ buffer.size = 4096;
643
buffer.posn = 0;
644
buffer.buffer = data;
645
@@ -634,7 +651,8 @@ static int fetch_alternates(char *base)
651
652
slot = get_active_slot();
653
curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
637
- curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
654
+ curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION,
655
+ fwrite_buffer_dynamic);
656
curl_easy_setopt(slot->curl, CURLOPT_URL, url);
657
if (start_active_slot(slot)) {
658
run_active_slot(slot);
@@ -646,20 +664,24 @@ static int fetch_alternates(char *base)
664
slot = get_active_slot();
665
curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
666
curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION,
649
- fwrite_buffer);
667
+ fwrite_buffer_dynamic);
668
curl_easy_setopt(slot->curl, CURLOPT_URL, url);
669
if (start_active_slot(slot)) {
670
run_active_slot(slot);
671
if (slot->curl_result != CURLE_OK) {
672
+ free(buffer.buffer);
673
return 0;
674
}
675
}
676
}
677
} else {
678
+ free(buffer.buffer);
679
return 0;
680
}
681
662
- data[buffer.posn] = '\0';
682
+ fwrite_buffer_dynamic(&null_byte, 1, 1, &buffer);
683
+ buffer.posn--;
684
+ data = buffer.buffer;
685
686
while (i < buffer.posn) {
687
int posn = i;
@@ -718,7 +740,8 @@ static int fetch_alternates(char *base)
740
}
741
i = posn + 1;
742
}
721
-
743
+
744
+ free(buffer.buffer);
745
return ret;
746
}
747
@@ -748,17 +771,22 @@ static int fetch_indices(struct alt_base *repo)
771
772
slot = get_active_slot();
773
curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
751
- curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
774
+ curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION,
775
+ fwrite_buffer_dynamic);
776
curl_easy_setopt(slot->curl, CURLOPT_URL, url);
777
curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, NULL);
778
if (start_active_slot(slot)) {
779
run_active_slot(slot);
756
- if (slot->curl_result != CURLE_OK)
780
+ if (slot->curl_result != CURLE_OK) {
781
+ free(buffer.buffer);
782
return error("%s", curl_errorstr);
783
+ }
784
} else {
785
+ free(buffer.buffer);
786
return error("Unable to start request");
787
}
788
789
+ data = buffer.buffer;
790
while (i < buffer.posn) {
791
switch (data[i]) {
792
case 'P':
@@ -778,6 +806,7 @@ static int fetch_indices(struct alt_base *repo)
806
i++;
807
}
808
809
+ free(buffer.buffer);
810
repo->got_indices = 1;
811
return 0;
812
}