Allow string section offset to overlap buckets

This commit is contained in:
Milen Dzhumerov 2017-08-22 20:48:17 +01:00
parent f72f8d839e
commit 9783a57cf4
4 changed files with 17 additions and 10 deletions

View File

@ -63,6 +63,12 @@ struct BinaryHeaderMap {
MemoryLayout<UInt32>.size +
MemoryLayout<UInt32>.size;
}
static func headerPlusBucketsSize(
bucketCount: DataHeader.BucketCountType) -> Int {
let bucketsSectionSize = Int(bucketCount) * BinaryHeaderMap.Bucket.packedSize
return packedSize + bucketsSectionSize
}
}
enum Magic: UInt32 {
@ -174,7 +180,9 @@ extension BinaryHeaderMap {
func getString(at offset: StringSectionOffset) -> String? {
let begin = Int(header.stringSectionOffset + offset.offset)
guard begin < data.count else { return nil }
let preambleSize = DataHeader.headerPlusBucketsSize(
bucketCount: header.bucketCount)
guard preambleSize <= begin, begin < data.count else { return nil }
let nullByteIndex = data.withUnsafeBytes {
(bytes: UnsafePointer<UInt8>) -> Int? in
@ -285,16 +293,11 @@ func parseHeaderMap(data: Data) throws -> DataHeaderParseResult {
throw HeaderMapParseError.outOfBoundsStringSectionOffset
}
let bucketsSectionSize = Int(bucketCount) * BinaryHeaderMap.Bucket.packedSize
let headerAndBucketsSectionSize = H.packedSize + bucketsSectionSize
let headerAndBucketsSectionSize = H.headerPlusBucketsSize(bucketCount: bucketCount)
guard headerAndBucketsSectionSize <= data.count else {
throw HeaderMapParseError.bucketsSectionOverflow
}
guard headerAndBucketsSectionSize <= Int(stringSectionOffset) else {
throw HeaderMapParseError.invalidStringSectionOffset
}
return DataHeaderParseResult(
dataHeader: BinaryHeaderMap.DataHeader(
magic: magic,

View File

@ -34,7 +34,6 @@ public enum HeaderMapParseError: LocalizedError {
case outOfBoundsStringSectionOffset
case bucketCountNotPowerOf2(found: UInt32)
case bucketsSectionOverflow
case invalidStringSectionOffset
}
public enum HeaderMapCreateError: LocalizedError {
@ -70,8 +69,6 @@ extension HeaderMapParseError {
return "Bucket count is not a power of 2, found \(buckets) buckets"
case .bucketsSectionOverflow:
return "Bucket section overflows"
case .invalidStringSectionOffset:
return "The string section offset is invalid"
}
}
}

View File

@ -137,5 +137,12 @@ class HeaderMapTests: XCTestCase {
let hmapEntries = Set<HeaderMap.Entry>(hmap.makeEntryList())
XCTAssertEqual(Set(), hmapEntries)
}
func testStringOffsetInBucketSection() throws {
let hmapData = try loadFile(named: "Empty", extension: "hmap").unwrap()
let headerMap = try HeaderMap(data: hmapData)
let entries = Set<HeaderMap.Entry>(headerMap.makeEntryList())
XCTAssertEqual(entries, Set())
}
}

Binary file not shown.