Skip to content

fix(bytes): parse byte counts exactly and reject overflow - #69

Open
Twiink wants to merge 1 commit into
labstack:masterfrom
Twiink:fix/byte-count-precision-20261121
Open

Twiink wants to merge 1 commit into
labstack:masterfrom
Twiink:fix/byte-count-precision-20261121

Conversation

@Twiink

@Twiink Twiink commented Nov 21, 2026

Copy link
Copy Markdown

Byte parsing converts the decimal input to float64 before applying its unit. This loses bytes even at small sizes (Parse("1.001KB") returns 1000 instead of 1001), rounds large integer inputs, and can silently turn positive values into negative byte counts (Parse("9223372036854775807B")). Out-of-range inputs also return a negative count without an error.

Use exact rational arithmetic while applying the binary/decimal multiplier, truncate fractional bytes toward zero, and check the final integer's range before returning it. The accepted input syntax, case handling, whitespace behavior, and fractional-byte truncation remain consistent.

Validation:

  • Unchanged master: 11 failing regression subcases and 5 passing controls, covering decimal units, binary units, signed values, integer boundaries, leading zeroes, and fractional truncation.
  • go test -race -count=1 ./... passed for the full repository.
  • go vet ./..., gofmt, and git diff --check passed.

AI assistance: Codex helped implement and verify this change.

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