Fix mmap leak caused by reading bad indexes.
If an index is corrupt, or is simply too new for us to understand, we were leaking the mmap that held the entire content of the index. This could be a considerable size on large projects, given that the index is at least 24 bytes * nr_objects. Signed-off-by: Shawn O. Pearce <spearce@spearce.org> Signed-off-by: Junio C Hamano <junkio@cox.net>
Shawn O. Pearce committed
Mar 6, 2007 at 20:44 UTC
2d88451b7a8cdd8bc233c7910c775bbb0ac76457
1 file changed
+16
-9
sha1_file.c
+16
-9
@@ -430,8 +430,9 @@ void pack_report()
430
pack_mapped, peak_pack_mapped);
431
}
432
433
-static int check_packed_git_idx(const char *path, unsigned long *idx_size_,
434
- void **idx_map_)
433
+static int check_packed_git_idx(const char *path,
434
+ unsigned long *idx_size_,
435
+ void **idx_map_)
436
{
437
void *idx_map;
438
uint32_t *index;
@@ -446,6 +447,10 @@ static int check_packed_git_idx(const char *path, unsigned long *idx_size_,
447
return -1;
448
}
449
idx_size = st.st_size;
450
+ if (idx_size < 4 * 256 + 20 + 20) {
451
+ close(fd);
452
+ return error("index file %s is too small", path);
453
+ }
454
idx_map = xmmap(NULL, idx_size, PROT_READ, MAP_PRIVATE, fd, 0);
455
close(fd);
456
@@ -453,25 +458,25 @@ static int check_packed_git_idx(const char *path, unsigned long *idx_size_,
458
*idx_map_ = idx_map;
459
*idx_size_ = idx_size;
460
456
- /* check index map */
457
- if (idx_size < 4*256 + 20 + 20)
458
- return error("index file %s is too small", path);
459
-
461
/* a future index format would start with this, as older git
462
* binaries would fail the non-monotonic index check below.
463
* give a nicer warning to the user if we can.
464
*/
464
- if (index[0] == htonl(PACK_IDX_SIGNATURE))
465
+ if (index[0] == htonl(PACK_IDX_SIGNATURE)) {
466
+ munmap(idx_map, idx_size);
467
return error("index file %s is a newer version"
468
" and is not supported by this binary"
469
" (try upgrading GIT to a newer version)",
470
path);
471
+ }
472
473
nr = 0;
474
for (i = 0; i < 256; i++) {
475
unsigned int n = ntohl(index[i]);
473
- if (n < nr)
476
+ if (n < nr) {
477
+ munmap(idx_map, idx_size);
478
return error("non-monotonic index %s", path);
479
+ }
480
nr = n;
481
}
482
@@ -482,8 +487,10 @@ static int check_packed_git_idx(const char *path, unsigned long *idx_size_,
487
* - 20-byte SHA1 of the packfile
488
* - 20-byte SHA1 file checksum
489
*/
485
- if (idx_size != 4*256 + nr * 24 + 20 + 20)
490
+ if (idx_size != 4*256 + nr * 24 + 20 + 20) {
491
+ munmap(idx_map, idx_size);
492
return error("wrong index file size in %s", path);
493
+ }
494
495
return 0;
496
}