Skip to content

fix: validate 32-bit declared lengths in the buffer decoder - #143

Merged
shamaton merged 1 commit into
mainfrom
fix/buffer-length-32bit
Oct 20, 2026
Merged

shamaton merged 1 commit into
mainfrom
fix/buffer-length-32bit

Conversation

@shamaton

Copy link
Copy Markdown
Owner

Summary

  • Reject 32-bit declared lengths that don't fit in int instead of panicking
  • Remove offset/length arithmetic that could overflow on 32-bit platforms
  • Affects Unmarshal only; the streaming decoder already had this check

Background / Motivation

On 32-bit platforms (GOARCH=386, arm), Array32/Map32/Str32/Bin32/Ext32 lengths with the high bit set became negative ints. Malformed input could panic (makeslice: len out of range, slice bounds out of range) or be silently accepted as an empty value.

Changes

  • LengthFromUint32 moved to a shared helper and used by both buffer and stream decoders
  • All 32-bit length reads in the buffer decoder, including unknown struct field skipping, go through it
  • Bounds checks rewritten to avoid index+n / length*2 overflow

Test Plan

  • go vet ./... / go test -race ./... pass
  • New regression test for huge declared lengths across Unmarshal / UnmarshalRead
  • Ran the new test on linux/386 (Docker): fails/panics before the fix, passes after
  • Decode benchmarks: no significant regression; B/op and allocs/op unchanged

Notes

  • The existing test suite does not compile on 32-bit due to untyped constant overflows, so 386 verification used the new test only

On 32-bit platforms, Array32/Map32/Str32/Bin32 lengths with the high bit
set became negative ints, causing panics or silently accepting malformed
input. Declared lengths are now validated with LengthFromUint32 (shared
with the stream decoder), and offset arithmetic no longer overflows.

Ext32 lengths are validated as well.
@github-actions github-actions Bot added the type: bug Confirmed or likely defect label Oct 20, 2026
@codecov

codecov Bot commented Oct 20, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 57.77778% with 19 lines in your changes missing coverage. Please review.
✅ Project coverage is 92.56%. Comparing base (b07bb9b) to head (9a1deee).

Files with missing lines Patch % Lines
internal/decoding/struct.go 64.70% 3 Missing and 3 partials ⚠️
internal/common/decodingutil/range.go 50.00% 1 Missing and 1 partial ⚠️
internal/decoding/bin.go 50.00% 1 Missing and 1 partial ⚠️
internal/decoding/ext.go 50.00% 1 Missing and 1 partial ⚠️
internal/decoding/map.go 60.00% 1 Missing and 1 partial ⚠️
internal/decoding/slice.go 50.00% 1 Missing and 1 partial ⚠️
internal/decoding/string.go 50.00% 1 Missing and 1 partial ⚠️
internal/stream/decoding/struct.go 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #143      +/-   ##
==========================================
- Coverage   92.79%   92.56%   -0.23%     
==========================================
  Files          75       75              
  Lines        7131     7157      +26     
==========================================
+ Hits         6617     6625       +8     
- Misses        321      330       +9     
- Partials      193      202       +9     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@shamaton
shamaton merged commit 5e13862 into main Oct 20, 2026
27 checks passed
@shamaton
shamaton deleted the fix/buffer-length-32bit branch October 20, 2026 12:51
@github-actions github-actions Bot mentioned this pull request Oct 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type: bug Confirmed or likely defect

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant