Skip to content

user: TestGetAdditionalGroups/group_entry_with_out-of-range_gid fails on 32-bit architectures (GOARCH=386 / arm) #254

Description

@siretart

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

  1. In user/user_test.go, the test defines groupContent with:

    toolarge:x:2147483648:
    

    where 2147483648 is math.MaxInt32 + 1.

  2. 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 (_).

  3. 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.

  4. 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:

  1. Parse IDs as int64 (e.g. strconv.ParseInt(string(p), 10, 64)) or validate strconv.ErrRange before truncating/converting to int, or
  2. 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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions