This commit is contained in:
h4x3rotab 2025-03-22 05:53:31 +00:00 committed by GitHub
commit 9ea20fd99f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 42 additions and 4 deletions

View file

@ -63,6 +63,11 @@ const (
// [4:8] File offset (4 bytes)
// [8:12] Block length (4 bytes)
blockLocSize = 12
// blockMetadataSize is the number of bytes added as the metadata to
// a serialized block (4 bytes network version + 4 bytes block size +
// 4 bytes checksum).
blockMetadataSize = 12
)
var (

View file

@ -1459,12 +1459,15 @@ func (tx *transaction) FetchBlockRegion(region *database.BlockRegion) ([]byte, e
}
location := deserializeBlockLoc(blockRow)
// Calculate the actual block size by removing the metadata.
blockLen := location.blockLen - blockMetadataSize
// Ensure the region is within the bounds of the block.
endOffset := region.Offset + region.Len
if endOffset < region.Offset || endOffset > location.blockLen {
if endOffset < region.Offset || endOffset > blockLen {
str := fmt.Sprintf("block %s region offset %d, length %d "+
"exceeds block length of %d", region.Hash,
region.Offset, region.Len, location.blockLen)
region.Offset, region.Len, blockLen)
return nil, makeDbErr(database.ErrBlockRegionInvalid, str, nil)
}
@ -1557,12 +1560,15 @@ func (tx *transaction) FetchBlockRegions(regions []database.BlockRegion) ([][]by
}
location := deserializeBlockLoc(blockRow)
// Calculate the actual block size by removing the metadata.
blockLen := location.blockLen - blockMetadataSize
// Ensure the region is within the bounds of the block.
endOffset := region.Offset + region.Len
if endOffset < region.Offset || endOffset > location.blockLen {
if endOffset < region.Offset || endOffset > blockLen {
str := fmt.Sprintf("block %s region offset %d, length "+
"%d exceeds block length of %d", region.Hash,
region.Offset, region.Len, location.blockLen)
region.Offset, region.Len, blockLen)
return nil, makeDbErr(database.ErrBlockRegionInvalid, str, nil)
}

View file

@ -1359,6 +1359,19 @@ func testFetchBlockIO(tc *testContext, tx database.Tx) bool {
if !checkDbError(tc.t, testName, err, wantErrCode) {
return false
}
// Ensure fetching a block region larger than the block returns
// the expected error.
testName = fmt.Sprintf("FetchBlockRegion(%s) out of block size",
blockHash)
wantErrCode = database.ErrBlockRegionInvalid
region.Hash = blockHash
region.Offset = 0
region.Len = uint32(len(blockBytes) + 1)
_, err = tx.FetchBlockRegion(&region)
if !checkDbError(tc.t, testName, err, wantErrCode) {
return false
}
}
// -----------------
@ -1499,6 +1512,20 @@ func testFetchBlockIO(tc *testContext, tx database.Tx) bool {
}
wantErrCode = database.ErrBlockRegionInvalid
_, err = tx.FetchBlockRegions(badBlockRegions)
if !checkDbError(tc.t, testName, err, wantErrCode) {
return false
}
// Ensure fetching block regions larger than the block returns the
// expected error.
testName = "FetchBlockRegions out of bunds"
badBlockRegions = badBlockRegions[:len(badBlockRegions)-1]
for i := range badBlockRegions {
badBlockRegions[i].Offset = 0
badBlockRegions[i].Len = uint32(len(allBlockBytes[i]) + 1)
}
wantErrCode = database.ErrBlockRegionInvalid
_, err = tx.FetchBlockRegions(badBlockRegions)
return checkDbError(tc.t, testName, err, wantErrCode)
}