Skip to content

Commit afe96a7

Browse files
committed
sstable: add redaction to bit flip check
Added safe details to bit flip check.
1 parent 918c6dd commit afe96a7

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

sstable/block/block.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ package block
77
import (
88
"context"
99
"encoding/binary"
10-
"fmt"
1110
"path/filepath"
1211
"runtime"
1312
"slices"
@@ -176,14 +175,14 @@ func ValidateChecksum(checksumType ChecksumType, b []byte, bh Handle) error {
176175
// Check if the checksum was due to a singular bit flip and report it.
177176
data := slices.Clone(b[:bh.Length+1])
178177
found, indexFound, bitFound := checkSliceForBitFlip(data, checksumType, expectedChecksum)
179-
bitFlipExtraMsg := ""
178+
err := base.CorruptionErrorf("block %d/%d: %s checksum mismatch %x != %x",
179+
errors.Safe(bh.Offset), errors.Safe(bh.Length), checksumType,
180+
expectedChecksum, computedChecksum)
180181
if found {
181-
bitFlipExtraMsg = fmt.Sprintf(". bit flip found: byte index %d. got: %x. want: %x.",
182+
err = errors.WithSafeDetails(err, ". bit flip found: byte index %d. got: %x. want: %x.",
182183
indexFound, data[indexFound], data[indexFound]^(1<<bitFound))
183184
}
184-
return base.CorruptionErrorf("block %d/%d: %s checksum mismatch %x != %x%s",
185-
errors.Safe(bh.Offset), errors.Safe(bh.Length), checksumType,
186-
expectedChecksum, computedChecksum, bitFlipExtraMsg)
185+
return err
187186
}
188187
return nil
189188
}

sstable/reader_test.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"os"
1616
"path"
1717
"path/filepath"
18+
"regexp"
1819
"slices"
1920
"strconv"
2021
"strings"
@@ -1474,7 +1475,6 @@ func TestReaderChecksumErrors(t *testing.T) {
14741475
// Corrupt a random bit in the block.
14751476
r := rand.New(rand.NewPCG(uint64(time.Now().UnixNano()), 0))
14761477
randOffset := r.Uint64N(bh.Length) + bh.Offset
1477-
println(bh.Offset, bh.Length)
14781478
randBit := uint(r.IntN(8))
14791479
data[randOffset] ^= (1 << randBit)
14801480
}
@@ -1513,8 +1513,16 @@ func TestReaderChecksumErrors(t *testing.T) {
15131513
// Check that the error message has the bit flip message if there was an error.
15141514
checkBitFlipErr := func(err error) bool {
15151515
if err != nil {
1516-
require.Regexp(t, `checksum mismatch.+bit flip found:.+`, err)
1517-
return true
1516+
details := errors.GetAllSafeDetails(err)
1517+
re := regexp.MustCompile(`bit flip found`)
1518+
for _, d := range details {
1519+
for _, s := range d.SafeDetails {
1520+
if re.MatchString(s) {
1521+
return true
1522+
}
1523+
}
1524+
}
1525+
require.Fail(t, "expected at least one detail to match bit flip found", err)
15181526
}
15191527
return false
15201528
}

0 commit comments

Comments
 (0)