fix: PUID and PGID as 32 bit unsigned integers

This commit is contained in:
Quentin McGaw
2022-05-01 13:35:14 +00:00
parent b6de6035f6
commit 9dd5e7bf1d
9 changed files with 102 additions and 21 deletions

View File

@@ -44,6 +44,15 @@ func CopyUint16Ptr(original *uint16) (copied *uint16) {
return copied 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) { func CopyIntPtr(original *int) (copied *int) {
if original == nil { if original == nil {
return nil return nil

View File

@@ -36,6 +36,15 @@ func DefaultUint16(existing *uint16, defaultValue uint16) (
*result = defaultValue *result = defaultValue
return result 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) ( func DefaultBool(existing *bool, defaultValue bool) (
result *bool) { result *bool) {

View File

@@ -78,6 +78,17 @@ func MergeWithUint16(existing, other *uint16) (result *uint16) {
return result 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) { func MergeWithIP(existing, other net.IP) (result net.IP) {
if existing != nil { if existing != nil {
return existing return existing

View File

@@ -68,6 +68,15 @@ func OverrideWithUint16(existing, other *uint16) (result *uint16) {
return result 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) { func OverrideWithIP(existing, other net.IP) (result net.IP) {
if other == nil { if other == nil {
return existing return existing

View File

@@ -7,8 +7,8 @@ import (
// System contains settings to configure system related elements. // System contains settings to configure system related elements.
type System struct { type System struct {
PUID *uint16 PUID *uint32
PGID *uint16 PGID *uint32
Timezone string Timezone string
} }
@@ -19,28 +19,28 @@ func (s System) validate() (err error) {
func (s *System) copy() (copied System) { func (s *System) copy() (copied System) {
return System{ return System{
PUID: helpers.CopyUint16Ptr(s.PUID), PUID: helpers.CopyUint32Ptr(s.PUID),
PGID: helpers.CopyUint16Ptr(s.PGID), PGID: helpers.CopyUint32Ptr(s.PGID),
Timezone: s.Timezone, Timezone: s.Timezone,
} }
} }
func (s *System) mergeWith(other System) { func (s *System) mergeWith(other System) {
s.PUID = helpers.MergeWithUint16(s.PUID, other.PUID) s.PUID = helpers.MergeWithUint32(s.PUID, other.PUID)
s.PGID = helpers.MergeWithUint16(s.PGID, other.PGID) s.PGID = helpers.MergeWithUint32(s.PGID, other.PGID)
s.Timezone = helpers.MergeWithString(s.Timezone, other.Timezone) s.Timezone = helpers.MergeWithString(s.Timezone, other.Timezone)
} }
func (s *System) overrideWith(other System) { func (s *System) overrideWith(other System) {
s.PUID = helpers.OverrideWithUint16(s.PUID, other.PUID) s.PUID = helpers.OverrideWithUint32(s.PUID, other.PUID)
s.PGID = helpers.OverrideWithUint16(s.PGID, other.PGID) s.PGID = helpers.OverrideWithUint32(s.PGID, other.PGID)
s.Timezone = helpers.OverrideWithString(s.Timezone, other.Timezone) s.Timezone = helpers.OverrideWithString(s.Timezone, other.Timezone)
} }
func (s *System) setDefaults() { func (s *System) setDefaults() {
const defaultID = 1000 const defaultID = 1000
s.PUID = helpers.DefaultUint16(s.PUID, defaultID) s.PUID = helpers.DefaultUint32(s.PUID, defaultID)
s.PGID = helpers.DefaultUint16(s.PGID, defaultID) s.PGID = helpers.DefaultUint32(s.PGID, defaultID)
} }
func (s System) String() string { func (s System) String() string {

View File

@@ -135,5 +135,5 @@ func unsetEnvKeys(envKeys []string, err error) (newErr error) {
} }
func stringPtr(s string) *string { return &s } 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 } func boolPtr(b bool) *bool { return &b }

View File

@@ -20,3 +20,7 @@ func setTestEnv(t *testing.T, key, value string) {
}) })
require.NoError(t, err) require.NoError(t, err)
} }
func TestXxx(t *testing.T) {
t.Log(int(^uint32(0)))
}

View File

@@ -34,20 +34,23 @@ func (r *Reader) readSystem() (system settings.System, err error) {
var ErrSystemIDNotValid = errors.New("system ID is not valid") var ErrSystemIDNotValid = errors.New("system ID is not valid")
func (r *Reader) readID(key, retroKey string) ( func (r *Reader) readID(key, retroKey string) (
id *uint16, err error) { id *uint32, err error) {
idEnvKey, idString := r.getEnvWithRetro(key, retroKey) idEnvKey, idString := r.getEnvWithRetro(key, retroKey)
if idString == "" { if idString == "" {
return nil, nil //nolint:nilnil 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 { if err != nil {
return nil, fmt.Errorf("environment variable %s: %w: %s: %s", return nil, fmt.Errorf("environment variable %s: %w: %s",
idEnvKey, ErrSystemIDNotValid, idString, err) idEnvKey, ErrSystemIDNotValid, err)
} else if idInt < 0 || idInt > 65535 { } else if idUint64 > max {
return nil, fmt.Errorf("environment variable %s: %w: %d: must be between 0 and 65535", return nil, fmt.Errorf("environment variable %s: %w: %d: must be between 0 and %d",
idEnvKey, ErrSystemIDNotValid, idInt) idEnvKey, ErrSystemIDNotValid, idUint64, max)
} }
return uint16Ptr(uint16(idInt)), nil return uint32Ptr(uint32(idUint64)), nil
} }

View File

@@ -14,15 +14,51 @@ func Test_Reader_readID(t *testing.T) {
keyValue string keyValue string
retroKeyPrefix string retroKeyPrefix string
retroValue string retroValue string
id *uint16 id *uint32
errWrapped error errWrapped error
errMessage string 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": { "id 1000": {
keyPrefix: "ID", keyPrefix: "ID",
keyValue: "1000", keyValue: "1000",
retroKeyPrefix: "RETRO_ID", 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`,
}, },
} }