@@ -286,50 +286,46 @@ static int print_binary_hunk(diff_print_info *pi, git_blob *old, git_blob *new)
286
286
{
287
287
git_buf deflate = GIT_BUF_INIT , delta = GIT_BUF_INIT , * out = NULL ;
288
288
const void * old_data , * new_data ;
289
- git_off_t off_t_old_data_len , off_t_new_data_len ;
290
- unsigned long old_data_len , new_data_len , delta_data_len , inflated_len ;
291
- size_t remain ;
289
+ git_off_t old_data_len , new_data_len ;
290
+ unsigned long delta_data_len , inflated_len ;
292
291
const char * out_type = "literal" ;
293
- char * ptr ;
292
+ char * scan , * end ;
294
293
int error ;
295
294
296
295
old_data = old ? git_blob_rawcontent (old ) : NULL ;
297
296
new_data = new ? git_blob_rawcontent (new ) : NULL ;
298
297
299
- off_t_old_data_len = old ? git_blob_rawsize (old ) : 0 ;
300
- off_t_new_data_len = new ? git_blob_rawsize (new ) : 0 ;
298
+ old_data_len = old ? git_blob_rawsize (old ) : 0 ;
299
+ new_data_len = new ? git_blob_rawsize (new ) : 0 ;
301
300
302
301
/* The git_delta function accepts unsigned long only */
303
- if (off_t_old_data_len > ULONG_MAX || off_t_new_data_len > ULONG_MAX ) {
304
- error = -1 ;
305
- goto done ;
306
- }
307
-
308
- old_data_len = (unsigned long )off_t_old_data_len ;
309
- new_data_len = (unsigned long )off_t_new_data_len ;
302
+ if (!git__is_ulong (old_data_len ) || !git__is_ulong (new_data_len ))
303
+ return GIT_EBUFS ;
310
304
311
305
out = & deflate ;
312
- inflated_len = new_data_len ;
306
+ inflated_len = ( unsigned long ) new_data_len ;
313
307
314
308
if ((error = git_zstream_deflatebuf (
315
- & deflate , new_data , new_data_len )) < 0 )
309
+ out , new_data , ( size_t ) new_data_len )) < 0 )
316
310
goto done ;
317
311
318
312
/* The git_delta function accepts unsigned long only */
319
- if (deflate .size > ULONG_MAX ) {
320
- error = -1 ;
313
+ if (! git__is_ulong (( git_off_t ) deflate .size ) ) {
314
+ error = GIT_EBUFS ;
321
315
goto done ;
322
316
}
323
317
324
318
if (old && new ) {
325
- void * delta_data ;
326
-
327
- delta_data = git_delta ( old_data , old_data_len , new_data ,
328
- new_data_len , & delta_data_len , (unsigned long )deflate .size );
319
+ void * delta_data = git_delta (
320
+ old_data , ( unsigned long ) old_data_len ,
321
+ new_data , ( unsigned long ) new_data_len ,
322
+ & delta_data_len , (unsigned long )deflate .size );
329
323
330
324
if (delta_data ) {
331
- error = git_zstream_deflatebuf (& delta , delta_data , delta_data_len );
332
- free (delta_data );
325
+ error = git_zstream_deflatebuf (
326
+ & delta , delta_data , (size_t )delta_data_len );
327
+
328
+ git__free (delta_data );
333
329
334
330
if (error < 0 )
335
331
goto done ;
@@ -345,24 +341,25 @@ static int print_binary_hunk(diff_print_info *pi, git_blob *old, git_blob *new)
345
341
git_buf_printf (pi -> buf , "%s %lu\n" , out_type , inflated_len );
346
342
pi -> line .num_lines ++ ;
347
343
348
- for (ptr = out -> ptr , remain = out -> size ; remain > 0 ; ) {
349
- size_t chunk_len = (52 < remain ) ? 52 : remain ;
344
+ for (scan = out -> ptr , end = out -> ptr + out -> size ; scan < end ; ) {
345
+ size_t chunk_len = end - scan ;
346
+ if (chunk_len > 52 )
347
+ chunk_len = 52 ;
350
348
351
349
if (chunk_len <= 26 )
352
350
git_buf_putc (pi -> buf , (char )chunk_len + 'A' - 1 );
353
351
else
354
352
git_buf_putc (pi -> buf , (char )chunk_len - 26 + 'a' - 1 );
355
353
356
- git_buf_put_base85 (pi -> buf , ptr , chunk_len );
354
+ git_buf_put_base85 (pi -> buf , scan , chunk_len );
357
355
git_buf_putc (pi -> buf , '\n' );
358
356
359
357
if (git_buf_oom (pi -> buf )) {
360
358
error = -1 ;
361
359
goto done ;
362
360
}
363
361
364
- ptr += chunk_len ;
365
- remain -= chunk_len ;
362
+ scan += chunk_len ;
366
363
pi -> line .num_lines ++ ;
367
364
}
368
365
@@ -381,26 +378,33 @@ static int diff_print_patch_file_binary(
381
378
git_blob * old = NULL , * new = NULL ;
382
379
const git_oid * old_id , * new_id ;
383
380
int error ;
381
+ size_t pre_binary_size ;
384
382
385
- if ((pi -> flags & GIT_DIFF_SHOW_BINARY ) == 0 ) {
386
- pi -> line .num_lines = 1 ;
387
- return diff_delta_format_with_paths (
388
- pi -> buf , delta , oldpfx , newpfx ,
389
- "Binary files %s%s and %s%s differ\n" );
390
- }
383
+ if ((pi -> flags & GIT_DIFF_SHOW_BINARY ) == 0 )
384
+ goto noshow ;
391
385
386
+ pre_binary_size = pi -> buf -> size ;
392
387
git_buf_printf (pi -> buf , "GIT binary patch\n" );
393
388
pi -> line .num_lines ++ ;
394
389
395
390
old_id = (delta -> status != GIT_DELTA_ADDED ) ? & delta -> old_file .id : NULL ;
396
391
new_id = (delta -> status != GIT_DELTA_DELETED ) ? & delta -> new_file .id : NULL ;
397
392
398
- if ((old_id && (error = git_blob_lookup (& old , pi -> diff -> repo , old_id )) < 0 ) ||
399
- (new_id && (error = git_blob_lookup (& new , pi -> diff -> repo ,new_id )) < 0 ) ||
400
- (error = print_binary_hunk (pi , old , new )) < 0 ||
393
+ if (old_id && (error = git_blob_lookup (& old , pi -> diff -> repo , old_id )) < 0 )
394
+ goto done ;
395
+ if (new_id && (error = git_blob_lookup (& new , pi -> diff -> repo ,new_id )) < 0 )
396
+ goto done ;
397
+
398
+ if ((error = print_binary_hunk (pi , old , new )) < 0 ||
401
399
(error = git_buf_putc (pi -> buf , '\n' )) < 0 ||
402
400
(error = print_binary_hunk (pi , new , old )) < 0 )
403
- goto done ;
401
+ {
402
+ if (error == GIT_EBUFS ) {
403
+ giterr_clear ();
404
+ git_buf_truncate (pi -> buf , pre_binary_size );
405
+ goto noshow ;
406
+ }
407
+ }
404
408
405
409
pi -> line .num_lines ++ ;
406
410
@@ -409,6 +413,12 @@ static int diff_print_patch_file_binary(
409
413
git_blob_free (new );
410
414
411
415
return error ;
416
+
417
+ noshow :
418
+ pi -> line .num_lines = 1 ;
419
+ return diff_delta_format_with_paths (
420
+ pi -> buf , delta , oldpfx , newpfx ,
421
+ "Binary files %s%s and %s%s differ\n" );
412
422
}
413
423
414
424
static int diff_print_patch_file (
0 commit comments