vcs-svn: allow import of > 4GiB files

There is no reason in principle that an svn-format dump would not be able to represent a file whose length does not fit in a 32-bit integer. Use off_t consistently to represent file lengths (in place of using uint32_t in some contexts) so we can handle that. Most svn-fe code is already ready to do that without this patch and passes values of type off_t around. The type mismatch from stragglers was noticed with gcc -Wtype-limits. While at it, tighten the parsing of the Text-content-length field to make sure it is a number and does not overflow, and tighten other overflow checks as that value is passed around and manipulated. Inspired-by: Ramsay Jones <ramsay@ramsay1.demon.co.uk> Signed-off-by: Jonathan Nieder <jrnieder@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jonathan Nieder committed Feb 2, 2012 at 05:03 UTC 150f75467cdd6eaf581d22175bb377399c62893a
3 files changed +26 -14
vcs-svn/fast_export.c
+9 -6
@@ -227,15 +227,18 @@ static long apply_delta(off_t len, struct line_buffer *input,
227 return ret;
228 }
229
230 -void fast_export_data(uint32_t mode, uint32_t len, struct line_buffer *input)
230 +void fast_export_data(uint32_t mode, off_t len, struct line_buffer *input)
231 {
232 + assert(len >= 0);
233 if (mode == REPO_MODE_LNK) {
234 /* svn symlink blobs start with "link " */
235 + if (len < 5)
236 + die("invalid dump: symlink too short for \"link\" prefix");
237 len -= 5;
238 if (buffer_skip_bytes(input, 5) != 5)
239 die_short_read(input);
240 }
238 - printf("data %"PRIu32"\n", len);
241 + printf("data %"PRIuMAX"\n", (uintmax_t) len);
242 if (buffer_copy_bytes(input, len) != len)
243 die_short_read(input);
244 fputc('\n', stdout);
@@ -297,12 +300,12 @@ int fast_export_ls(const char *path, uint32_t *mode, struct strbuf *dataref)
300
301 void fast_export_blob_delta(uint32_t mode,
302 uint32_t old_mode, const char *old_data,
300 - uint32_t len, struct line_buffer *input)
303 + off_t len, struct line_buffer *input)
304 {
305 long postimage_len;
303 - if (len > maximum_signed_value_of_type(off_t))
304 - die("enormous delta");
305 - postimage_len = apply_delta((off_t) len, input, old_data, old_mode);
306 +
307 + assert(len >= 0);
308 + postimage_len = apply_delta(len, input, old_data, old_mode);
309 if (mode == REPO_MODE_LNK) {
310 buffer_skip_bytes(&postimage, strlen("link "));
311 postimage_len -= strlen("link ");
vcs-svn/fast_export.h
+2 -2
@@ -14,10 +14,10 @@ void fast_export_begin_commit(uint32_t revision, const char *author,
14 const struct strbuf *log, const char *uuid,
15 const char *url, unsigned long timestamp);
16 void fast_export_end_commit(uint32_t revision);
17 -void fast_export_data(uint32_t mode, uint32_t len, struct line_buffer *input);
17 +void fast_export_data(uint32_t mode, off_t len, struct line_buffer *input);
18 void fast_export_blob_delta(uint32_t mode,
19 uint32_t old_mode, const char *old_data,
20 - uint32_t len, struct line_buffer *input);
20 + off_t len, struct line_buffer *input);
21
22 /* If there is no such file at that rev, returns -1, errno == ENOENT. */
23 int fast_export_ls_rev(uint32_t rev, const char *path,
vcs-svn/svndump.c
+15 -6
@@ -40,7 +40,8 @@
40 static struct line_buffer input = LINE_BUFFER_INIT;
41
42 static struct {
43 - uint32_t action, propLength, textLength, srcRev, type;
43 + uint32_t action, propLength, srcRev, type;
44 + off_t text_length;
45 struct strbuf src, dst;
46 uint32_t text_delta, prop_delta;
47 } node_ctx;
@@ -61,7 +62,7 @@ static void reset_node_ctx(char *fname)
62 node_ctx.type = 0;
63 node_ctx.action = NODEACT_UNKNOWN;
64 node_ctx.propLength = LENGTH_UNKNOWN;
64 - node_ctx.textLength = LENGTH_UNKNOWN;
65 + node_ctx.text_length = -1;
66 strbuf_reset(&node_ctx.src);
67 node_ctx.srcRev = 0;
68 strbuf_reset(&node_ctx.dst);
@@ -209,7 +210,7 @@ static void handle_node(void)
210 {
211 const uint32_t type = node_ctx.type;
212 const int have_props = node_ctx.propLength != LENGTH_UNKNOWN;
212 - const int have_text = node_ctx.textLength != LENGTH_UNKNOWN;
213 + const int have_text = node_ctx.text_length != -1;
214 /*
215 * Old text for this node:
216 * NULL - directory or bug
@@ -291,12 +292,12 @@ static void handle_node(void)
292 }
293 if (!node_ctx.text_delta) {
294 fast_export_modify(node_ctx.dst.buf, node_ctx.type, "inline");
294 - fast_export_data(node_ctx.type, node_ctx.textLength, &input);
295 + fast_export_data(node_ctx.type, node_ctx.text_length, &input);
296 return;
297 }
298 fast_export_modify(node_ctx.dst.buf, node_ctx.type, "inline");
299 fast_export_blob_delta(node_ctx.type, old_mode, old_data,
299 - node_ctx.textLength, &input);
300 + node_ctx.text_length, &input);
301 }
302
303 static void begin_revision(void)
@@ -409,7 +410,15 @@ void svndump_read(const char *url)
410 break;
411 case sizeof("Text-content-length"):
412 if (!constcmp(t, "Text-content-length")) {
412 - node_ctx.textLength = atoi(val);
413 + char *end;
414 + uintmax_t textlen;
415 +
416 + textlen = strtoumax(val, &end, 10);
417 + if (!isdigit(*val) || *end)
418 + die("invalid dump: non-numeric length %s", val);
419 + if (textlen > maximum_signed_value_of_type(off_t))
420 + die("unrepresentable length in dump: %s", val);
421 + node_ctx.text_length = (off_t) textlen;
422 break;
423 }
424 if (constcmp(t, "Prop-content-length"))