13
13
#include "commit.h"
14
14
#include "tag.h"
15
15
#include "tree.h"
16
+ #include "tree-walk.h"
16
17
#include "refs.h"
17
18
#include "pack-revindex.h"
18
19
#include "sha1-lookup.h"
@@ -2471,8 +2472,37 @@ int has_sha1_file(const unsigned char *sha1)
2471
2472
return has_loose_object (sha1 );
2472
2473
}
2473
2474
2475
+ static void check_tree (const void * buf , size_t size )
2476
+ {
2477
+ struct tree_desc desc ;
2478
+ struct name_entry entry ;
2479
+
2480
+ init_tree_desc (& desc , buf , size );
2481
+ while (tree_entry (& desc , & entry ))
2482
+ /* do nothing
2483
+ * tree_entry() will die() on malformed entries */
2484
+ ;
2485
+ }
2486
+
2487
+ static void check_commit (const void * buf , size_t size )
2488
+ {
2489
+ struct commit c ;
2490
+ memset (& c , 0 , sizeof (c ));
2491
+ if (parse_commit_buffer (& c , buf , size ))
2492
+ die ("corrupt commit" );
2493
+ }
2494
+
2495
+ static void check_tag (const void * buf , size_t size )
2496
+ {
2497
+ struct tag t ;
2498
+ memset (& t , 0 , sizeof (t ));
2499
+ if (parse_tag_buffer (& t , buf , size ))
2500
+ die ("corrupt tag" );
2501
+ }
2502
+
2474
2503
static int index_mem (unsigned char * sha1 , void * buf , size_t size ,
2475
- int write_object , enum object_type type , const char * path )
2504
+ int write_object , enum object_type type ,
2505
+ const char * path , int format_check )
2476
2506
{
2477
2507
int ret , re_allocated = 0 ;
2478
2508
@@ -2490,6 +2520,14 @@ static int index_mem(unsigned char *sha1, void *buf, size_t size,
2490
2520
re_allocated = 1 ;
2491
2521
}
2492
2522
}
2523
+ if (format_check ) {
2524
+ if (type == OBJ_TREE )
2525
+ check_tree (buf , size );
2526
+ if (type == OBJ_COMMIT )
2527
+ check_commit (buf , size );
2528
+ if (type == OBJ_TAG )
2529
+ check_tag (buf , size );
2530
+ }
2493
2531
2494
2532
if (write_object )
2495
2533
ret = write_sha1_file (buf , size , typename (type ), sha1 );
@@ -2503,7 +2541,7 @@ static int index_mem(unsigned char *sha1, void *buf, size_t size,
2503
2541
#define SMALL_FILE_SIZE (32*1024)
2504
2542
2505
2543
int index_fd (unsigned char * sha1 , int fd , struct stat * st , int write_object ,
2506
- enum object_type type , const char * path )
2544
+ enum object_type type , const char * path , int format_check )
2507
2545
{
2508
2546
int ret ;
2509
2547
size_t size = xsize_t (st -> st_size );
@@ -2512,23 +2550,25 @@ int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_object,
2512
2550
struct strbuf sbuf = STRBUF_INIT ;
2513
2551
if (strbuf_read (& sbuf , fd , 4096 ) >= 0 )
2514
2552
ret = index_mem (sha1 , sbuf .buf , sbuf .len , write_object ,
2515
- type , path );
2553
+ type , path , format_check );
2516
2554
else
2517
2555
ret = -1 ;
2518
2556
strbuf_release (& sbuf );
2519
2557
} else if (!size ) {
2520
- ret = index_mem (sha1 , NULL , size , write_object , type , path );
2558
+ ret = index_mem (sha1 , NULL , size , write_object , type , path ,
2559
+ format_check );
2521
2560
} else if (size <= SMALL_FILE_SIZE ) {
2522
2561
char * buf = xmalloc (size );
2523
2562
if (size == read_in_full (fd , buf , size ))
2524
2563
ret = index_mem (sha1 , buf , size , write_object , type ,
2525
- path );
2564
+ path , format_check );
2526
2565
else
2527
2566
ret = error ("short read %s" , strerror (errno ));
2528
2567
free (buf );
2529
2568
} else {
2530
2569
void * buf = xmmap (NULL , size , PROT_READ , MAP_PRIVATE , fd , 0 );
2531
- ret = index_mem (sha1 , buf , size , write_object , type , path );
2570
+ ret = index_mem (sha1 , buf , size , write_object , type , path ,
2571
+ format_check );
2532
2572
munmap (buf , size );
2533
2573
}
2534
2574
close (fd );
@@ -2546,7 +2586,7 @@ int index_path(unsigned char *sha1, const char *path, struct stat *st, int write
2546
2586
if (fd < 0 )
2547
2587
return error ("open(\"%s\"): %s" , path ,
2548
2588
strerror (errno ));
2549
- if (index_fd (sha1 , fd , st , write_object , OBJ_BLOB , path ) < 0 )
2589
+ if (index_fd (sha1 , fd , st , write_object , OBJ_BLOB , path , 0 ) < 0 )
2550
2590
return error ("%s: failed to insert into database" ,
2551
2591
path );
2552
2592
break ;
0 commit comments