Skip to content

Validate key lengths and avoid overflowing decoded offsets - #445

Open
zhechen-cs wants to merge 1 commit into
allegro:mainfrom
zhechen-cs:codex/validate-large-cache-keys
Open

zhechen-cs wants to merge 1 commit into
allegro:mainfrom
zhechen-cs:codex/validate-large-cache-keys

Conversation

@zhechen-cs

Copy link
Copy Markdown

A 65518-byte key can be written successfully but makes Get panic with slice bounds out of range [18:0]. The decoded key length is a uint16, so adding the 18-byte entry header wraps before it is used as a slice offset. Longer keys also affect value decoding and the key comparison used by Append.

Convert decoded lengths to int before computing offsets. Reject keys longer than the two-byte length field can represent in Set and Append, before changing cache state, and document that limit.

Tests cover the last length before the overflow, the first overflowing length, and the maximum representable key across Get, GetWithInfo, Append, and iteration. Both write methods reject 65536-byte keys without replacing an existing colliding entry.

Validation on macOS arm64 with Go 1.27.2:

  • On unchanged main, the round-trip regression panics and both rejection cases fail.
  • go test -race -count=1 ./... passes.
  • go vet ./..., golangci-lint for new issues, and git diff --check pass.

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.

1 participant