vcs-svn: guard against overflow when computing preimage length

Signed integer overflow produces undefined behavior in C and off_t is a signed type. For predictable behavior, add some checks to protect in advance against overflow. On 32-bit systems ftell as called by buffer_tmpfile_prepare_to_read is likely to fail with EOVERFLOW when reading the corresponding postimage, and this patch does not fix that. So it's more of a futureproofing measure than a complete fix. Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>

Jonathan Nieder committed May 27, 2011 at 05:18 UTC abe27c0cbd97bf6a693004ddb411392ed596a853
1 file changed +14 -1
vcs-svn/fast_export.c
+14 -1
@@ -166,6 +166,7 @@ static int ends_with(const char *s, size_t len, const char *suffix)
166 static int parse_cat_response_line(const char *header, off_t *len)
167 {
168 size_t headerlen = strlen(header);
169 + uintmax_t n;
170 const char *type;
171 const char *end;
172
@@ -174,14 +175,25 @@ static int parse_cat_response_line(const char *header, off_t *len)
175 type = memmem(header, headerlen, " blob ", strlen(" blob "));
176 if (!type)
177 return error("cat-blob header has wrong object type: %s", header);
177 - *len = strtoumax(type + strlen(" blob "), (char **) &end, 10);
178 + n = strtoumax(type + strlen(" blob "), (char **) &end, 10);
179 if (end == type + strlen(" blob "))
180 return error("cat-blob header does not contain length: %s", header);
181 + if (memchr(type + strlen(" blob "), '-', end - type - strlen(" blob ")))
182 + return error("cat-blob header contains negative length: %s", header);
183 + if (n == UINTMAX_MAX || n > maximum_signed_value_of_type(off_t))
184 + return error("blob too large for current definition of off_t");
185 + *len = n;
186 if (*end)
187 return error("cat-blob header contains garbage after length: %s", header);
188 return 0;
189 }
190
191 +static void check_preimage_overflow(off_t a, off_t b)
192 +{
193 + if (signed_add_overflows(a, b))
194 + die("blob too large for current definition of off_t");
195 +}
196 +
197 static long apply_delta(off_t len, struct line_buffer *input,
198 const char *old_data, uint32_t old_mode)
199 {
@@ -204,6 +216,7 @@ static long apply_delta(off_t len, struct line_buffer *input,
216 }
217 if (old_mode == REPO_MODE_LNK) {
218 strbuf_addstr(&preimage.buf, "link ");
219 + check_preimage_overflow(preimage_len, strlen("link "));
220 preimage_len += strlen("link ");
221 }
222 if (svndiff0_apply(input, len, &preimage, out))