@@ -1175,6 +1175,7 @@ int git_packfile_alloc(struct git_pack_file **pack_out, const char *path)
1175
1175
static git_off_t nth_packed_object_offset (const struct git_pack_file * p , uint32_t n )
1176
1176
{
1177
1177
const unsigned char * index = p -> index_map .data ;
1178
+ const unsigned char * end = index + p -> index_map .len ;
1178
1179
index += 4 * 256 ;
1179
1180
if (p -> index_version == 1 ) {
1180
1181
return ntohl (* ((uint32_t * )(index + 24 * n )));
@@ -1185,6 +1186,11 @@ static git_off_t nth_packed_object_offset(const struct git_pack_file *p, uint32_
1185
1186
if (!(off & 0x80000000 ))
1186
1187
return off ;
1187
1188
index += p -> num_objects * 4 + (off & 0x7fffffff ) * 8 ;
1189
+
1190
+ /* Make sure we're not being sent out of bounds */
1191
+ if (index >= end - 8 )
1192
+ return -1 ;
1193
+
1188
1194
return (((uint64_t )ntohl (* ((uint32_t * )(index + 0 )))) << 32 ) |
1189
1195
ntohl (* ((uint32_t * )(index + 4 )));
1190
1196
}
@@ -1264,6 +1270,7 @@ static int pack_entry_find_offset(
1264
1270
const unsigned char * index = p -> index_map .data ;
1265
1271
unsigned hi , lo , stride ;
1266
1272
int pos , found = 0 ;
1273
+ git_off_t offset ;
1267
1274
const unsigned char * current = 0 ;
1268
1275
1269
1276
* offset_out = 0 ;
@@ -1336,7 +1343,12 @@ static int pack_entry_find_offset(
1336
1343
if (found > 1 )
1337
1344
return git_odb__error_ambiguous ("found multiple offsets for pack entry" );
1338
1345
1339
- * offset_out = nth_packed_object_offset (p , pos );
1346
+ if ((offset = nth_packed_object_offset (p , pos )) < 0 ) {
1347
+ giterr_set (GITERR_ODB , "packfile index is corrupt" );
1348
+ return -1 ;
1349
+ }
1350
+
1351
+ * offset_out = offset ;
1340
1352
git_oid_fromraw (found_oid , current );
1341
1353
1342
1354
#ifdef INDEX_DEBUG_LOOKUP
0 commit comments