Skip to content

fix: parse leading-zero decimal strings like 08 correctly - #378

Open
abu-tahir-0 wants to merge 1 commit into
spf13:masterfrom
abu-tahir-0:fix/leading-zero-decimal-strings
Open

abu-tahir-0 wants to merge 1 commit into
spf13:masterfrom
abu-tahir-0:fix/leading-zero-decimal-strings

Conversation

@abu-tahir-0

Copy link
Copy Markdown

Problem

cast.ToInt("08") and cast.ToUint("09") return 0 instead of 8 and 9. This affects any zero-padded decimal string containing 8 or 9 (issue #214).

Root Cause

parseInt/parseUint call strconv.ParseInt/ParseUint with base 0, which auto-detects a leading 0 as octal. "08"/"09" are invalid octal, so parsing fails and cast returns the zero value. strconv.Atoi (base 10) handles these correctly.

Solution

Fall back to base 10 when base 0 parsing fails. Inputs that already parse under base 0 (hex 0xff, binary 0b1010, legitimate octal 010) keep their exact current behavior; only previously-erroring strings change. Applied to both parseInt and parseUint.

Testing

  • Added TestToIntLeadingZeroDecimalFallback in number_test.go covering ToInt, ToInt64, and ToUint for "08", "09", "018", "019".
  • Confirmed the test fails on the original source and passes with the fix.
  • Verified hex/binary/octal inputs are unchanged.
  • go test -count=1 ./... and go vet ./... pass.

Impact

Zero-padded decimal strings are converted correctly instead of silently becoming zero.

strconv.ParseInt/ParseUint with base 0 treat a leading zero as octal,
so decimal strings such as "08" or "018" fail to parse and cast
returns 0. Fall back to base 10 when base-0 parsing fails, preserving
hex/binary/octal literals that already parse successfully.
@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

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.

2 participants