Skip to content

Commit

Permalink
fix: PUID and PGID as 32 bit unsigned integers
Browse files Browse the repository at this point in the history
  • Loading branch information
qdm12 committed May 1, 2022
1 parent b6de603 commit efdf9e7
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 16 deletions.
9 changes: 9 additions & 0 deletions internal/configuration/settings/helpers/copy.go
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions internal/configuration/settings/helpers/default.go
Expand Up @@ -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) {
Expand Down
11 changes: 11 additions & 0 deletions internal/configuration/settings/helpers/merge.go
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions internal/configuration/settings/helpers/override.go
Expand Up @@ -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
Expand Down
20 changes: 10 additions & 10 deletions internal/configuration/settings/system.go
Expand Up @@ -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
}

Expand All @@ -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 {
Expand Down
1 change: 0 additions & 1 deletion internal/configuration/sources/env/helpers.go
Expand Up @@ -135,5 +135,4 @@ func unsetEnvKeys(envKeys []string, err error) (newErr error) {
}

func stringPtr(s string) *string { return &s }
func uint16Ptr(n uint16) *uint16 { return &n }
func boolPtr(b bool) *bool { return &b }
4 changes: 4 additions & 0 deletions internal/configuration/sources/env/helpers_test.go
Expand Up @@ -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)))
}
8 changes: 5 additions & 3 deletions internal/configuration/sources/env/system.go
Expand Up @@ -34,7 +34,7 @@ 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
Expand All @@ -44,10 +44,12 @@ func (r *Reader) readID(key, retroKey string) (
if err != nil {
return nil, fmt.Errorf("environment variable %s: %w: %s: %s",
idEnvKey, ErrSystemIDNotValid, idString, err)
} else if idInt < 0 || idInt > 65535 {
} else if idInt < 0 || idInt > int(^uint32(0)) {
return nil, fmt.Errorf("environment variable %s: %w: %d: must be between 0 and 65535",
idEnvKey, ErrSystemIDNotValid, idInt)
}

return uint16Ptr(uint16(idInt)), nil
return uint32Ptr(uint32(idInt)), nil
}

func uint32Ptr(n uint32) *uint32 { return &n }
4 changes: 2 additions & 2 deletions internal/configuration/sources/env/system_test.go
Expand Up @@ -14,15 +14,15 @@ func Test_Reader_readID(t *testing.T) {
keyValue string
retroKeyPrefix string
retroValue string
id *uint16
id *uint32
errWrapped error
errMessage string
}{
"id 1000": {
keyPrefix: "ID",
keyValue: "1000",
retroKeyPrefix: "RETRO_ID",
id: uint16Ptr(1000),
id: uint32Ptr(1000),
},
}

Expand Down

0 comments on commit efdf9e7

Please sign in to comment.