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"
@@ -2479,8 +2480,37 @@ int has_sha1_file(const unsigned char *sha1)
2479
2480
return has_loose_object (sha1 );
2480
2481
}
2481
2482
2483
+ static void check_tree (const void * buf , size_t size )
2484
+ {
2485
+ struct tree_desc desc ;
2486
+ struct name_entry entry ;
2487
+
2488
+ init_tree_desc (& desc , buf , size );
2489
+ while (tree_entry (& desc , & entry ))
2490
+ /* do nothing
2491
+ * tree_entry() will die() on malformed entries */
2492
+ ;
2493
+ }
2494
+
2495
+ static void check_commit (const void * buf , size_t size )
2496
+ {
2497
+ struct commit c ;
2498
+ memset (& c , 0 , sizeof (c ));
2499
+ if (parse_commit_buffer (& c , buf , size ))
2500
+ die ("corrupt commit" );
2501
+ }
2502
+
2503
+ static void check_tag (const void * buf , size_t size )
2504
+ {
2505
+ struct tag t ;
2506
+ memset (& t , 0 , sizeof (t ));
2507
+ if (parse_tag_buffer (& t , buf , size ))
2508
+ die ("corrupt tag" );
2509
+ }
2510
+
2482
2511
static int index_mem (unsigned char * sha1 , void * buf , size_t size ,
2483
- int write_object , enum object_type type , const char * path )
2512
+ int write_object , enum object_type type ,
2513
+ const char * path , int format_check )
2484
2514
{
2485
2515
int ret , re_allocated = 0 ;
2486
2516
@@ -2498,6 +2528,14 @@ static int index_mem(unsigned char *sha1, void *buf, size_t size,
2498
2528
re_allocated = 1 ;
2499
2529
}
2500
2530
}
2531
+ if (format_check ) {
2532
+ if (type == OBJ_TREE )
2533
+ check_tree (buf , size );
2534
+ if (type == OBJ_COMMIT )
2535
+ check_commit (buf , size );
2536
+ if (type == OBJ_TAG )
2537
+ check_tag (buf , size );
2538
+ }
2501
2539
2502
2540
if (write_object )
2503
2541
ret = write_sha1_file (buf , size , typename (type ), sha1 );
@@ -2511,7 +2549,7 @@ static int index_mem(unsigned char *sha1, void *buf, size_t size,
2511
2549
#define SMALL_FILE_SIZE (32*1024)
2512
2550
2513
2551
int index_fd (unsigned char * sha1 , int fd , struct stat * st , int write_object ,
2514
- enum object_type type , const char * path )
2552
+ enum object_type type , const char * path , int format_check )
2515
2553
{
2516
2554
int ret ;
2517
2555
size_t size = xsize_t (st -> st_size );
@@ -2520,23 +2558,25 @@ int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_object,
2520
2558
struct strbuf sbuf = STRBUF_INIT ;
2521
2559
if (strbuf_read (& sbuf , fd , 4096 ) >= 0 )
2522
2560
ret = index_mem (sha1 , sbuf .buf , sbuf .len , write_object ,
2523
- type , path );
2561
+ type , path , format_check );
2524
2562
else
2525
2563
ret = -1 ;
2526
2564
strbuf_release (& sbuf );
2527
2565
} else if (!size ) {
2528
- ret = index_mem (sha1 , NULL , size , write_object , type , path );
2566
+ ret = index_mem (sha1 , NULL , size , write_object , type , path ,
2567
+ format_check );
2529
2568
} else if (size <= SMALL_FILE_SIZE ) {
2530
2569
char * buf = xmalloc (size );
2531
2570
if (size == read_in_full (fd , buf , size ))
2532
2571
ret = index_mem (sha1 , buf , size , write_object , type ,
2533
- path );
2572
+ path , format_check );
2534
2573
else
2535
2574
ret = error ("short read %s" , strerror (errno ));
2536
2575
free (buf );
2537
2576
} else {
2538
2577
void * buf = xmmap (NULL , size , PROT_READ , MAP_PRIVATE , fd , 0 );
2539
- ret = index_mem (sha1 , buf , size , write_object , type , path );
2578
+ ret = index_mem (sha1 , buf , size , write_object , type , path ,
2579
+ format_check );
2540
2580
munmap (buf , size );
2541
2581
}
2542
2582
close (fd );
@@ -2554,7 +2594,7 @@ int index_path(unsigned char *sha1, const char *path, struct stat *st, int write
2554
2594
if (fd < 0 )
2555
2595
return error ("open(\"%s\"): %s" , path ,
2556
2596
strerror (errno ));
2557
- if (index_fd (sha1 , fd , st , write_object , OBJ_BLOB , path ) < 0 )
2597
+ if (index_fd (sha1 , fd , st , write_object , OBJ_BLOB , path , 0 ) < 0 )
2558
2598
return error ("%s: failed to insert into database" ,
2559
2599
path );
2560
2600
break ;
0 commit comments