diff-delta: fold two special tests into one plus cleanups
Testing for realloc and size limit can be done with only one test per loop. Make it so and fix a theoretical off-by-one comparison error in the process. The output buffer memory allocation is also bounded by max_size when specified. Finally make some variable unsigned to allow the handling of files up to 4GB in size instead of 2GB. Signed-off-by: Nicolas Pitre <nico@cam.org> Signed-off-by: Junio C Hamano <junkio@cox.net>
Nicolas Pitre committed
Feb 21, 2006 at 20:41 UTC
fe474b588b3cb1c23c987a3d0f9e869a160d82d2
1 file changed
+14
-10
diff-delta.c
+14
-10
@@ -148,16 +148,20 @@ static void delta_cleanup(bdfile_t *bdf)
148
cha_free(&bdf->cha);
149
}
150
151
+/* provide the size of the copy opcode given the block offset and size */
152
#define COPYOP_SIZE(o, s) \
153
(!!(o & 0xff) + !!(o & 0xff00) + !!(o & 0xff0000) + !!(o & 0xff000000) + \
154
!!(s & 0xff) + !!(s & 0xff00) + 1)
155
156
+/* the maximum size for any opcode */
157
+#define MAX_OP_SIZE COPYOP_SIZE(0xffffffff, 0xffffffff)
158
+
159
void *diff_delta(void *from_buf, unsigned long from_size,
160
void *to_buf, unsigned long to_size,
161
unsigned long *delta_size,
162
unsigned long max_size)
163
{
160
- int i, outpos, outsize, inscnt, csize, msize, moff;
164
+ unsigned int i, outpos, outsize, inscnt, csize, msize, moff;
165
unsigned int fp;
166
const unsigned char *ref_data, *ref_top, *data, *top, *ptr1, *ptr2;
167
unsigned char *out, *orig;
@@ -169,6 +173,8 @@ void *diff_delta(void *from_buf, unsigned long from_size,
173
174
outpos = 0;
175
outsize = 8192;
176
+ if (max_size && outsize >= max_size)
177
+ outsize = max_size + MAX_OP_SIZE + 1;
178
out = malloc(outsize);
179
if (!out) {
180
delta_cleanup(&bdf);
@@ -259,17 +265,15 @@ void *diff_delta(void *from_buf, unsigned long from_size,
265
*orig = i;
266
}
267
262
- if (max_size && outpos > max_size) {
263
- free(out);
264
- delta_cleanup(&bdf);
265
- return NULL;
266
- }
267
-
268
- /* next time around the largest possible output is 1 + 4 + 3 */
269
- if (outpos > outsize - 8) {
268
+ if (outpos >= outsize - MAX_OP_SIZE) {
269
void *tmp = out;
270
outsize = outsize * 3 / 2;
272
- out = realloc(out, outsize);
271
+ if (max_size && outsize >= max_size)
272
+ outsize = max_size + MAX_OP_SIZE + 1;
273
+ if (max_size && outpos > max_size)
274
+ out = NULL;
275
+ else
276
+ out = realloc(out, outsize);
277
if (!out) {
278
free(tmp);
279
delta_cleanup(&bdf);