Skip to content

Commit

Permalink
fix(wal): fix wrong overflow validation calculation (#566)
Browse files Browse the repository at this point in the history
### Motivation


The calculation of the actual buf remain size should not include a
header.

```
actualBufSize := bufSize - startFileOffset
```
  • Loading branch information
mattisonchao authored Nov 12, 2024
1 parent 61f09cf commit 919673c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
2 changes: 1 addition & 1 deletion server/wal/codec/v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func (v *V2) ReadHeaderWithValidation(buf []byte, startFileOffset uint32) (paylo

expectSize := payloadSize + v.HeaderSize
// overflow checking
actualBufSize := bufSize - (startFileOffset + headerOffset)
actualBufSize := bufSize - startFileOffset
if expectSize > actualBufSize {
return payloadSize, previousCrc, payloadCrc,
errors.Wrapf(ErrOffsetOutOfBounds, "expected payload size: %d. actual buf size: %d ", expectSize, bufSize)
Expand Down
11 changes: 11 additions & 0 deletions server/wal/codec/v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package codec

import (
"bytes"
"encoding/binary"
"github.com/google/uuid"
"os"
Expand Down Expand Up @@ -308,3 +309,13 @@ func TestV2_IndexBroken(t *testing.T) {
_, err = v2.ReadIndex(p)
assert.ErrorIs(t, err, ErrDataCorrupted)
}

func TestV2_ReadWithValidation(t *testing.T) {
buf := make([]byte, 15)
payloadSize := uint32(len(buf)) - v2.HeaderSize
payload := bytes.Repeat([]byte("A"), int(payloadSize))
_, wPayloadCrc := v2.WriteRecord(buf, 0, 0, payload)
_, _, rPayloadCrc, err := v2.ReadHeaderWithValidation(buf, 0)
assert.NoError(t, err)
assert.EqualValues(t, wPayloadCrc, rPayloadCrc)
}

0 comments on commit 919673c

Please sign in to comment.