Skip to content

fix: prevent panic when reading empty WAL index file (#737)#912

Merged
mattisonchao merged 1 commit intomainfrom
fix/737-empty-idx-panic
Feb 26, 2026
Merged

fix: prevent panic when reading empty WAL index file (#737)#912
mattisonchao merged 1 commit intomainfrom
fix/737-empty-idx-panic

Conversation

@coderzc
Copy link
Copy Markdown
Member

@coderzc coderzc commented Feb 26, 2026

Problem

When the .idxx index file is empty (0 bytes), ReadIndex() would panic with slice bounds out of range [4:0] when trying to read the CRC at offset 0.

Stack trace from #737:

github.com/streamnative/oxia/server/wal/codec.(*V2).ReadIndex
    /src/oxia/server/wal/codec/v2.go:189 +0x47d

Solution

Added a length check before reading the index. If the file is shorter than the header size (4 bytes), it returns ErrDataCorrupted instead of panicking, allowing the caller (readonly_segment.go) to rebuild the index from the txn file.

Changes

  • oxiad/dataserver/wal/codec/v2.go: Added length check in ReadIndex()
  • oxiad/dataserver/wal/codec/v2_test.go: Added tests for empty and short index files

Test Plan

go test -v ./oxiad/dataserver/wal/codec/... -run "TestV2_ReadEmptyIndex|TestV2_ReadShortIndex"

Both tests pass:

  • TestV2_ReadEmptyIndex: verifies empty file (0 bytes) returns ErrDataCorrupted
  • TestV2_ReadShortIndex: verifies short file (<4 bytes) returns ErrDataCorrupted

Fixes #737

When the .idxx index file is empty (0 bytes), ReadIndex() would panic
with "slice bounds out of range [4:0]" when trying to read the CRC
at offset 0.

This fix adds a length check before reading the index. If the file
is shorter than the header size (4 bytes), it returns ErrDataCorrupted
instead of panicking, allowing the caller to rebuild the index from
the txn file.

Added tests:
- TestV2_ReadEmptyIndex: verifies empty file handling
- TestV2_ReadShortIndex: verifies short file (<4 bytes) handling

Fixes #737
@mattisonchao mattisonchao merged commit 4893217 into main Feb 26, 2026
9 checks passed
@mattisonchao mattisonchao deleted the fix/737-empty-idx-panic branch February 26, 2026 03:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

panic: runtime error: slice bounds out of range [4:0]

2 participants