Description
When running tests in github.com/moby/sys/user on 32-bit architectures (e.g. GOARCH=386 or GOARCH=arm), TestGetAdditionalGroups/group_entry_with_out-of-range_gid fails:
=== RUN TestGetAdditionalGroups
=== RUN TestGetAdditionalGroups/group_entry_with_out-of-range_gid
user_test.go:651: Parse(struct { doc string; groups []string; expected []int; hasError bool }{doc:"group entry with out-of-range gid", groups:[]string{"toolarge"}, expected:[]int(nil), hasError:true}) expects error but has none
--- FAIL: TestGetAdditionalGroups (0.01s)
--- FAIL: TestGetAdditionalGroups/group_entry_with_out-of-range_gid (0.00s)
FAIL github.com/moby/sys/user 0.008s
Steps to Reproduce
cd user
GOARCH=386 go test -v . -run "TestGetAdditionalGroups/group_entry_with_out-of-range_gid"
Root Cause Analysis
-
In user/user_test.go, the test defines groupContent with:
where 2147483648 is math.MaxInt32 + 1.
-
In user/user.go, parseParts() parses GID entries into *int via:
case *int:
*e, _ = strconv.Atoi(string(p))
Conversion errors returned by strconv.Atoi are explicitly ignored (_).
-
On 64-bit systems (int is int64), strconv.Atoi("2147483648") succeeds and sets *e = 2147483648. GetAdditionalGroups() then checks if g.Gid < minID || g.Gid > maxID (where maxID is 2147483647), correctly returning ErrRange.
-
On 32-bit systems (int is int32), strconv.Atoi("2147483648") overflows int32 and returns strconv.ErrRange while setting *e = math.MaxInt32 (2147483647). Because the conversion error is discarded, g.Gid becomes 2147483647. Since 2147483647 <= maxID, GetAdditionalGroups() considers the GID valid, does not return an error, and the test fails.
Suggested Fix
parseParts (or Group.Gid / User.Uid / User.Gid parsing) should either:
- Parse IDs as
int64 (e.g. strconv.ParseInt(string(p), 10, 64)) or validate strconv.ErrRange before truncating/converting to int, or
- Check whether the string value exceeds
maxID (or check for strconv.ErrRange) when validating group/user entries so that 32-bit platforms reject out-of-range values consistently with 64-bit platforms.
Description
When running tests in
github.com/moby/sys/useron 32-bit architectures (e.g.GOARCH=386orGOARCH=arm),TestGetAdditionalGroups/group_entry_with_out-of-range_gidfails:Steps to Reproduce
Root Cause Analysis
In
user/user_test.go, the test definesgroupContentwith:where
2147483648ismath.MaxInt32 + 1.In
user/user.go,parseParts()parses GID entries into*intvia:Conversion errors returned by
strconv.Atoiare explicitly ignored (_).On 64-bit systems (
intisint64),strconv.Atoi("2147483648")succeeds and sets*e = 2147483648.GetAdditionalGroups()then checksif g.Gid < minID || g.Gid > maxID(wheremaxIDis2147483647), correctly returningErrRange.On 32-bit systems (
intisint32),strconv.Atoi("2147483648")overflowsint32and returnsstrconv.ErrRangewhile setting*e = math.MaxInt32(2147483647). Because the conversion error is discarded,g.Gidbecomes2147483647. Since2147483647 <= maxID,GetAdditionalGroups()considers the GID valid, does not return an error, and the test fails.Suggested Fix
parseParts(orGroup.Gid/User.Uid/User.Gidparsing) should either:int64(e.g.strconv.ParseInt(string(p), 10, 64)) or validatestrconv.ErrRangebefore truncating/converting toint, ormaxID(or check forstrconv.ErrRange) when validating group/user entries so that 32-bit platforms reject out-of-range values consistently with 64-bit platforms.