@matheusbsbs / matheus-bagliano / commits / 7efbff7531
Fork 0

unpack_sha1_file(): detect corrupt loose object files.

We did not detect broken loose object files, either when underlying inflate() signalled the breakage, nor inflate() finished and we had garbage trailing at the end. We do better now. We also make unpack_sha1_file() a static function to sha1_file.c, since it is not used by anybody outside. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Mar 5, 2007 at 00:21 UTC 7efbff7531af4281487d54c1dc1401308d988e33
2 files changed +19 -9
cache.h
-1
@@ -281,7 +281,6 @@ char *enter_repo(char *path, int strict);
281
282 /* Read and unpack a sha1 file into memory, write memory to a sha1 file */
283 extern int sha1_object_info(const unsigned char *, unsigned long *);
284 -extern void * unpack_sha1_file(void *map, unsigned long mapsize, enum object_type *type, unsigned long *size);
284 extern void * read_sha1_file(const unsigned char *sha1, enum object_type *type, unsigned long *size);
285 extern int hash_sha1_file(void *buf, unsigned long len, const char *type, unsigned char *sha1);
286 extern int write_sha1_file(void *buf, unsigned long len, const char *type, unsigned char *return_sha1);
sha1_file.c
+19 -8
@@ -956,11 +956,12 @@ static int unpack_sha1_header(z_stream *stream, unsigned char *map, unsigned lon
956 return 0;
957 }
958
959 -static void *unpack_sha1_rest(z_stream *stream, void *buffer, unsigned long size)
959 +static void *unpack_sha1_rest(z_stream *stream, void *buffer, unsigned long size, const unsigned char *sha1)
960 {
961 int bytes = strlen(buffer) + 1;
962 unsigned char *buf = xmalloc(1+size);
963 unsigned long n;
964 + int status = Z_OK;
965
966 n = stream->total_out - bytes;
967 if (n > size)
@@ -970,12 +971,22 @@ static void *unpack_sha1_rest(z_stream *stream, void *buffer, unsigned long size
971 if (bytes < size) {
972 stream->next_out = buf + bytes;
973 stream->avail_out = size - bytes;
973 - while (inflate(stream, Z_FINISH) == Z_OK)
974 - /* nothing */;
974 + while (status == Z_OK)
975 + status = inflate(stream, Z_FINISH);
976 }
977 buf[size] = 0;
977 - inflateEnd(stream);
978 - return buf;
978 + if ((status == Z_OK || status == Z_STREAM_END) && !stream->avail_in) {
979 + inflateEnd(stream);
980 + return buf;
981 + }
982 +
983 + if (status < 0)
984 + error("corrupt loose object '%s'", sha1_to_hex(sha1));
985 + else if (stream->avail_in)
986 + error("garbage at end of loose object '%s'",
987 + sha1_to_hex(sha1));
988 + free(buf);
989 + return NULL;
990 }
991
992 /*
@@ -1029,7 +1040,7 @@ static int parse_sha1_header(const char *hdr, unsigned long *sizep)
1040 return *hdr ? -1 : type_from_string(type);
1041 }
1042
1032 -void * unpack_sha1_file(void *map, unsigned long mapsize, enum object_type *type, unsigned long *size)
1043 +static void *unpack_sha1_file(void *map, unsigned long mapsize, enum object_type *type, unsigned long *size, const unsigned char *sha1)
1044 {
1045 int ret;
1046 z_stream stream;
@@ -1039,7 +1050,7 @@ void * unpack_sha1_file(void *map, unsigned long mapsize, enum object_type *type
1050 if (ret < Z_OK || (*type = parse_sha1_header(hdr, size)) < 0)
1051 return NULL;
1052
1042 - return unpack_sha1_rest(&stream, hdr, *size);
1053 + return unpack_sha1_rest(&stream, hdr, *size, sha1);
1054 }
1055
1056 static unsigned long get_delta_base(struct packed_git *p,
@@ -1555,7 +1566,7 @@ void *read_sha1_file(const unsigned char *sha1, enum object_type *type,
1566 return buf;
1567 map = map_sha1_file(sha1, &mapsize);
1568 if (map) {
1558 - buf = unpack_sha1_file(map, mapsize, type, size);
1569 + buf = unpack_sha1_file(map, mapsize, type, size, sha1);
1570 munmap(map, mapsize);
1571 return buf;
1572 }