From 9dd5e7bf1ddf93e82e7ab36ac9e41d266dd23c8b Mon Sep 17 00:00:00 2001 From: Quentin McGaw Date: Sun, 1 May 2022 13:35:14 +0000 Subject: [PATCH] fix: PUID and PGID as 32 bit unsigned integers --- .../configuration/settings/helpers/copy.go | 9 +++++ .../configuration/settings/helpers/default.go | 9 +++++ .../configuration/settings/helpers/merge.go | 11 +++++ .../settings/helpers/override.go | 9 +++++ internal/configuration/settings/system.go | 20 +++++----- internal/configuration/sources/env/helpers.go | 2 +- .../configuration/sources/env/helpers_test.go | 4 ++ internal/configuration/sources/env/system.go | 19 +++++---- .../configuration/sources/env/system_test.go | 40 ++++++++++++++++++- 9 files changed, 102 insertions(+), 21 deletions(-) diff --git a/internal/configuration/settings/helpers/copy.go b/internal/configuration/settings/helpers/copy.go index eb8de6c3..b93e7712 100644 --- a/internal/configuration/settings/helpers/copy.go +++ b/internal/configuration/settings/helpers/copy.go @@ -44,6 +44,15 @@ func CopyUint16Ptr(original *uint16) (copied *uint16) { return copied } +func CopyUint32Ptr(original *uint32) (copied *uint32) { + if original == nil { + return nil + } + copied = new(uint32) + *copied = *original + return copied +} + func CopyIntPtr(original *int) (copied *int) { if original == nil { return nil diff --git a/internal/configuration/settings/helpers/default.go b/internal/configuration/settings/helpers/default.go index 1458fe70..0951a5c1 100644 --- a/internal/configuration/settings/helpers/default.go +++ b/internal/configuration/settings/helpers/default.go @@ -36,6 +36,15 @@ func DefaultUint16(existing *uint16, defaultValue uint16) ( *result = defaultValue return result } +func DefaultUint32(existing *uint32, defaultValue uint32) ( + result *uint32) { + if existing != nil { + return existing + } + result = new(uint32) + *result = defaultValue + return result +} func DefaultBool(existing *bool, defaultValue bool) ( result *bool) { diff --git a/internal/configuration/settings/helpers/merge.go b/internal/configuration/settings/helpers/merge.go index adb24ff2..bb0ca3b8 100644 --- a/internal/configuration/settings/helpers/merge.go +++ b/internal/configuration/settings/helpers/merge.go @@ -78,6 +78,17 @@ func MergeWithUint16(existing, other *uint16) (result *uint16) { return result } +func MergeWithUint32(existing, other *uint32) (result *uint32) { + if existing != nil { + return existing + } else if other == nil { + return nil + } + result = new(uint32) + *result = *other + return result +} + func MergeWithIP(existing, other net.IP) (result net.IP) { if existing != nil { return existing diff --git a/internal/configuration/settings/helpers/override.go b/internal/configuration/settings/helpers/override.go index d60d5143..7e1c77e8 100644 --- a/internal/configuration/settings/helpers/override.go +++ b/internal/configuration/settings/helpers/override.go @@ -68,6 +68,15 @@ func OverrideWithUint16(existing, other *uint16) (result *uint16) { return result } +func OverrideWithUint32(existing, other *uint32) (result *uint32) { + if other == nil { + return existing + } + result = new(uint32) + *result = *other + return result +} + func OverrideWithIP(existing, other net.IP) (result net.IP) { if other == nil { return existing diff --git a/internal/configuration/settings/system.go b/internal/configuration/settings/system.go index 1639cff3..a4ec4a9f 100644 --- a/internal/configuration/settings/system.go +++ b/internal/configuration/settings/system.go @@ -7,8 +7,8 @@ import ( // System contains settings to configure system related elements. type System struct { - PUID *uint16 - PGID *uint16 + PUID *uint32 + PGID *uint32 Timezone string } @@ -19,28 +19,28 @@ func (s System) validate() (err error) { func (s *System) copy() (copied System) { return System{ - PUID: helpers.CopyUint16Ptr(s.PUID), - PGID: helpers.CopyUint16Ptr(s.PGID), + PUID: helpers.CopyUint32Ptr(s.PUID), + PGID: helpers.CopyUint32Ptr(s.PGID), Timezone: s.Timezone, } } func (s *System) mergeWith(other System) { - s.PUID = helpers.MergeWithUint16(s.PUID, other.PUID) - s.PGID = helpers.MergeWithUint16(s.PGID, other.PGID) + s.PUID = helpers.MergeWithUint32(s.PUID, other.PUID) + s.PGID = helpers.MergeWithUint32(s.PGID, other.PGID) s.Timezone = helpers.MergeWithString(s.Timezone, other.Timezone) } func (s *System) overrideWith(other System) { - s.PUID = helpers.OverrideWithUint16(s.PUID, other.PUID) - s.PGID = helpers.OverrideWithUint16(s.PGID, other.PGID) + s.PUID = helpers.OverrideWithUint32(s.PUID, other.PUID) + s.PGID = helpers.OverrideWithUint32(s.PGID, other.PGID) s.Timezone = helpers.OverrideWithString(s.Timezone, other.Timezone) } func (s *System) setDefaults() { const defaultID = 1000 - s.PUID = helpers.DefaultUint16(s.PUID, defaultID) - s.PGID = helpers.DefaultUint16(s.PGID, defaultID) + s.PUID = helpers.DefaultUint32(s.PUID, defaultID) + s.PGID = helpers.DefaultUint32(s.PGID, defaultID) } func (s System) String() string { diff --git a/internal/configuration/sources/env/helpers.go b/internal/configuration/sources/env/helpers.go index f3836379..aa9826da 100644 --- a/internal/configuration/sources/env/helpers.go +++ b/internal/configuration/sources/env/helpers.go @@ -135,5 +135,5 @@ func unsetEnvKeys(envKeys []string, err error) (newErr error) { } func stringPtr(s string) *string { return &s } -func uint16Ptr(n uint16) *uint16 { return &n } +func uint32Ptr(n uint32) *uint32 { return &n } func boolPtr(b bool) *bool { return &b } diff --git a/internal/configuration/sources/env/helpers_test.go b/internal/configuration/sources/env/helpers_test.go index 9c60657f..c5dba032 100644 --- a/internal/configuration/sources/env/helpers_test.go +++ b/internal/configuration/sources/env/helpers_test.go @@ -20,3 +20,7 @@ func setTestEnv(t *testing.T, key, value string) { }) require.NoError(t, err) } + +func TestXxx(t *testing.T) { + t.Log(int(^uint32(0))) +} diff --git a/internal/configuration/sources/env/system.go b/internal/configuration/sources/env/system.go index f2a755d7..faec674a 100644 --- a/internal/configuration/sources/env/system.go +++ b/internal/configuration/sources/env/system.go @@ -34,20 +34,23 @@ func (r *Reader) readSystem() (system settings.System, err error) { var ErrSystemIDNotValid = errors.New("system ID is not valid") func (r *Reader) readID(key, retroKey string) ( - id *uint16, err error) { + id *uint32, err error) { idEnvKey, idString := r.getEnvWithRetro(key, retroKey) if idString == "" { return nil, nil //nolint:nilnil } - idInt, err := strconv.Atoi(idString) + const base = 10 + const bitSize = 64 + const max = uint64(^uint32(0)) + idUint64, err := strconv.ParseUint(idString, base, bitSize) if err != nil { - return nil, fmt.Errorf("environment variable %s: %w: %s: %s", - idEnvKey, ErrSystemIDNotValid, idString, err) - } else if idInt < 0 || idInt > 65535 { - return nil, fmt.Errorf("environment variable %s: %w: %d: must be between 0 and 65535", - idEnvKey, ErrSystemIDNotValid, idInt) + return nil, fmt.Errorf("environment variable %s: %w: %s", + idEnvKey, ErrSystemIDNotValid, err) + } else if idUint64 > max { + return nil, fmt.Errorf("environment variable %s: %w: %d: must be between 0 and %d", + idEnvKey, ErrSystemIDNotValid, idUint64, max) } - return uint16Ptr(uint16(idInt)), nil + return uint32Ptr(uint32(idUint64)), nil } diff --git a/internal/configuration/sources/env/system_test.go b/internal/configuration/sources/env/system_test.go index 7d575110..6ff2e19a 100644 --- a/internal/configuration/sources/env/system_test.go +++ b/internal/configuration/sources/env/system_test.go @@ -14,15 +14,51 @@ func Test_Reader_readID(t *testing.T) { keyValue string retroKeyPrefix string retroValue string - id *uint16 + id *uint32 errWrapped error errMessage string }{ + "empty string": { + keyPrefix: "ID", + retroKeyPrefix: "RETRO_ID", + }, + "invalid string": { + keyPrefix: "ID", + keyValue: "invalid", + retroKeyPrefix: "RETRO_ID", + errWrapped: ErrSystemIDNotValid, + errMessage: `environment variable IDTest_Reader_readID/invalid_string: ` + + `system ID is not valid: ` + + `strconv.ParseUint: parsing "invalid": invalid syntax`, + }, + "negative number": { + keyPrefix: "ID", + keyValue: "-1", + retroKeyPrefix: "RETRO_ID", + errWrapped: ErrSystemIDNotValid, + errMessage: `environment variable IDTest_Reader_readID/negative_number: ` + + `system ID is not valid: ` + + `strconv.ParseUint: parsing "-1": invalid syntax`, + }, "id 1000": { keyPrefix: "ID", keyValue: "1000", retroKeyPrefix: "RETRO_ID", - id: uint16Ptr(1000), + id: uint32Ptr(1000), + }, + "max id": { + keyPrefix: "ID", + keyValue: "4294967295", + retroKeyPrefix: "RETRO_ID", + id: uint32Ptr(4294967295), + }, + "above max id": { + keyPrefix: "ID", + keyValue: "4294967296", + retroKeyPrefix: "RETRO_ID", + errWrapped: ErrSystemIDNotValid, + errMessage: `environment variable IDTest_Reader_readID/above_max_id: ` + + `system ID is not valid: 4294967296: must be between 0 and 4294967295`, }, }