Skip to content

number: derive ParseInt/ParseUint bitSize from the target type#348

Open
c-tonneslan wants to merge 1 commit into
spf13:masterfrom
c-tonneslan:fix/parseint-parseuint-bitsize-from-T
Open

number: derive ParseInt/ParseUint bitSize from the target type#348
c-tonneslan wants to merge 1 commit into
spf13:masterfrom
c-tonneslan:fix/parseint-parseuint-bitsize-from-T

Conversation

@c-tonneslan

Copy link
Copy Markdown

`parseInt`/`parseUint` passed `bitSize=0` to `strconv`, which means native int width. On a 32-bit platform that's 32 even when `T` is `int64`, so strings like `"9223372036854775807"` or `"18446744073709551615"` fail to round-trip through cast. That's the armhf `TestNumber` failure from the issue.

Switched to `unsafe.Sizeof(t)*8`, which matches `T`'s actual width at each generic instantiation (8/16/32/64 for the fixed-size variants, `strconv.IntSize` for `int`/`uint`). Overflow detection for the smaller types is preserved, and the `cast.go` call sites that pass `parseInt[T]` as a function reference don't change.

fixes #310

parseInt and parseUint passed bitSize=0 to strconv, which means native
int width. On a 32-bit platform that's 32 even when T is int64, so
strings like "9223372036854775807" or "18446744073709551615" fail to
round-trip through Cast (see TestNumber failures reported on armhf).

Use unsafe.Sizeof(t)*8, which matches T's actual width at each generic
instantiation: 8/16/32/64 for the fixed-size variants, strconv.IntSize
for int/uint. Overflow detection for the smaller types is preserved.

Closes spf13#310

Signed-off-by: Charlie Tonneslan <cst0520@gmail.com>
@CLAassistant

CLAassistant commented May 31, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

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.

Test failures on 32bit systems

2 participants