mirror of
https://github.com/btcsuite/btcd.git
synced 2025-03-26 01:29:30 +01:00
Merge d125e1fce4
into 43a870eb9f
This commit is contained in:
commit
9ea20fd99f
3 changed files with 42 additions and 4 deletions
|
@ -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 (
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
||||
|
|
|
@ -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(®ion)
|
||||
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)
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue