1
0
Fork 0
mirror of https://github.com/Luzifer/rconfig.git synced 2024-11-09 16:30:04 +00:00

Compare commits

..

3 commits

Author SHA1 Message Date
9a13c8c9f1
Lint: Replace deprecated linter
Signed-off-by: Knut Ahlers <knut@ahlers.me>
2024-08-27 15:51:12 +02:00
80dd107159
Fix: Ensure int/uint do not overflow
by utilizing correct parsing for the requested type in strconv

Signed-off-by: Knut Ahlers <knut@ahlers.me>
2024-08-27 15:47:47 +02:00
6f0f872a04
Fix: Add support for int16 flags
Signed-off-by: Knut Ahlers <knut@ahlers.me>
2024-08-27 15:47:03 +02:00
4 changed files with 76 additions and 14 deletions

View file

@ -31,11 +31,11 @@ linters:
- bodyclose # checks whether HTTP response body is closed successfully [fast: true, auto-fix: false] - bodyclose # checks whether HTTP response body is closed successfully [fast: true, auto-fix: false]
- containedctx # containedctx is a linter that detects struct contained context.Context field [fast: true, auto-fix: false] - containedctx # containedctx is a linter that detects struct contained context.Context field [fast: true, auto-fix: false]
- contextcheck # check the function whether use a non-inherited context [fast: false, auto-fix: false] - contextcheck # check the function whether use a non-inherited context [fast: false, auto-fix: false]
- copyloopvar # copyloopvar is a linter detects places where loop variables are copied [fast: true, auto-fix: false]
- dogsled # Checks assignments with too many blank identifiers (e.g. x, _, _, _, := f()) [fast: true, auto-fix: false] - dogsled # Checks assignments with too many blank identifiers (e.g. x, _, _, _, := f()) [fast: true, auto-fix: false]
- durationcheck # check for two durations multiplied together [fast: false, auto-fix: false] - durationcheck # check for two durations multiplied together [fast: false, auto-fix: false]
- errcheck # Errcheck is a program for checking for unchecked errors in go programs. These unchecked errors can be critical bugs in some cases [fast: false, auto-fix: false] - errcheck # Errcheck is a program for checking for unchecked errors in go programs. These unchecked errors can be critical bugs in some cases [fast: false, auto-fix: false]
- errchkjson # Checks types passed to the json encoding functions. Reports unsupported types and optionally reports occations, where the check for the returned error can be omitted. [fast: false, auto-fix: false] - errchkjson # Checks types passed to the json encoding functions. Reports unsupported types and optionally reports occations, where the check for the returned error can be omitted. [fast: false, auto-fix: false]
- exportloopref # checks for pointers to enclosing loop variables [fast: true, auto-fix: false]
- forbidigo # Forbids identifiers [fast: true, auto-fix: false] - forbidigo # Forbids identifiers [fast: true, auto-fix: false]
- funlen # Tool for detection of long functions [fast: true, auto-fix: false] - funlen # Tool for detection of long functions [fast: true, auto-fix: false]
- gocognit # Computes and checks the cognitive complexity of functions [fast: true, auto-fix: false] - gocognit # Computes and checks the cognitive complexity of functions [fast: true, auto-fix: false]

View file

@ -258,8 +258,8 @@ func execTags(in interface{}, fs *pflag.FlagSet) ([]afterFunc, error) {
valField.SetBool(v) valField.SetBool(v)
} }
case reflect.Int, reflect.Int8, reflect.Int32, reflect.Int64: case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
vt, err := strconv.ParseInt(value, 10, 64) vt, err := parseIntForType(value, 10, typeField.Type.Kind()) //nolint:mnd
if err != nil { if err != nil {
if value != "" { if value != "" {
return nil, fmt.Errorf("parsing int: %w", err) return nil, fmt.Errorf("parsing int: %w", err)
@ -273,7 +273,7 @@ func execTags(in interface{}, fs *pflag.FlagSet) ([]afterFunc, error) {
} }
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
vt, err := strconv.ParseUint(value, 10, 64) vt, err := parseUintForType(value, 10, typeField.Type.Kind()) //nolint:mnd
if err != nil { if err != nil {
if value != "" { if value != "" {
return nil, fmt.Errorf("parsing uint: %w", err) return nil, fmt.Errorf("parsing uint: %w", err)
@ -371,15 +371,21 @@ func registerFlagInt(t reflect.Kind, fs *pflag.FlagSet, field interface{}, parts
} }
case reflect.Int8: case reflect.Int8:
if len(parts) == 1 { if len(parts) == 1 {
fs.Int8Var(field.(*int8), parts[0], int8(vt), desc) fs.Int8Var(field.(*int8), parts[0], int8(vt), desc) //#nosec:G115 - Variable is guaranteed to match by strconv
} else { } else {
fs.Int8VarP(field.(*int8), parts[0], parts[1], int8(vt), desc) fs.Int8VarP(field.(*int8), parts[0], parts[1], int8(vt), desc) //#nosec:G115 - Variable is guaranteed to match by strconv
}
case reflect.Int16:
if len(parts) == 1 {
fs.Int16Var(field.(*int16), parts[0], int16(vt), desc) //#nosec:G115 - Variable is guaranteed to match by strconv
} else {
fs.Int16VarP(field.(*int16), parts[0], parts[1], int16(vt), desc) //#nosec:G115 - Variable is guaranteed to match by strconv
} }
case reflect.Int32: case reflect.Int32:
if len(parts) == 1 { if len(parts) == 1 {
fs.Int32Var(field.(*int32), parts[0], int32(vt), desc) fs.Int32Var(field.(*int32), parts[0], int32(vt), desc) //#nosec:G115 - Variable is guaranteed to match by strconv
} else { } else {
fs.Int32VarP(field.(*int32), parts[0], parts[1], int32(vt), desc) fs.Int32VarP(field.(*int32), parts[0], parts[1], int32(vt), desc) //#nosec:G115 - Variable is guaranteed to match by strconv
} }
case reflect.Int64: case reflect.Int64:
if len(parts) == 1 { if len(parts) == 1 {
@ -400,21 +406,21 @@ func registerFlagUint(t reflect.Kind, fs *pflag.FlagSet, field interface{}, part
} }
case reflect.Uint8: case reflect.Uint8:
if len(parts) == 1 { if len(parts) == 1 {
fs.Uint8Var(field.(*uint8), parts[0], uint8(vt), desc) fs.Uint8Var(field.(*uint8), parts[0], uint8(vt), desc) //#nosec:G115 - Variable is guaranteed to match by strconv
} else { } else {
fs.Uint8VarP(field.(*uint8), parts[0], parts[1], uint8(vt), desc) fs.Uint8VarP(field.(*uint8), parts[0], parts[1], uint8(vt), desc) //#nosec:G115 - Variable is guaranteed to match by strconv
} }
case reflect.Uint16: case reflect.Uint16:
if len(parts) == 1 { if len(parts) == 1 {
fs.Uint16Var(field.(*uint16), parts[0], uint16(vt), desc) fs.Uint16Var(field.(*uint16), parts[0], uint16(vt), desc) //#nosec:G115 - Variable is guaranteed to match by strconv
} else { } else {
fs.Uint16VarP(field.(*uint16), parts[0], parts[1], uint16(vt), desc) fs.Uint16VarP(field.(*uint16), parts[0], parts[1], uint16(vt), desc) //#nosec:G115 - Variable is guaranteed to match by strconv
} }
case reflect.Uint32: case reflect.Uint32:
if len(parts) == 1 { if len(parts) == 1 {
fs.Uint32Var(field.(*uint32), parts[0], uint32(vt), desc) fs.Uint32Var(field.(*uint32), parts[0], uint32(vt), desc) //#nosec:G115 - Variable is guaranteed to match by strconv
} else { } else {
fs.Uint32VarP(field.(*uint32), parts[0], parts[1], uint32(vt), desc) fs.Uint32VarP(field.(*uint32), parts[0], parts[1], uint32(vt), desc) //#nosec:G115 - Variable is guaranteed to match by strconv
} }
case reflect.Uint64: case reflect.Uint64:
if len(parts) == 1 { if len(parts) == 1 {

View file

@ -9,6 +9,7 @@ func TestIntParsing(t *testing.T) {
args = []string{ args = []string{
"--int=1", "-i", "2", "--int=1", "-i", "2",
"--int8=3", "-8", "4", "--int8=3", "-8", "4",
"--int16=2", "-1", "3",
"--int32=5", "-3", "6", "--int32=5", "-3", "6",
"--int64=7", "-6", "8", "--int64=7", "-6", "8",
} }
@ -17,6 +18,8 @@ func TestIntParsing(t *testing.T) {
TestP int `flag:"intp,i"` TestP int `flag:"intp,i"`
Test8 int8 `flag:"int8"` Test8 int8 `flag:"int8"`
Test8P int8 `flag:"int8p,8"` Test8P int8 `flag:"int8p,8"`
Test16 int16 `flag:"int16"`
Test16P int16 `flag:"int16p,1"`
Test32 int32 `flag:"int32"` Test32 int32 `flag:"int32"`
Test32P int32 `flag:"int32p,3"` Test32P int32 `flag:"int32p,3"`
Test64 int64 `flag:"int64"` Test64 int64 `flag:"int64"`
@ -34,6 +37,8 @@ func TestIntParsing(t *testing.T) {
{cfg.TestP, 2}, {cfg.TestP, 2},
{cfg.Test8, int8(3)}, {cfg.Test8, int8(3)},
{cfg.Test8P, int8(4)}, {cfg.Test8P, int8(4)},
{cfg.Test16, int16(2)},
{cfg.Test16P, int16(3)},
{cfg.Test32, int32(5)}, {cfg.Test32, int32(5)},
{cfg.Test32P, int32(6)}, {cfg.Test32P, int32(6)},
{cfg.Test64, int64(7)}, {cfg.Test64, int64(7)},

51
strconv.go Normal file
View file

@ -0,0 +1,51 @@
package rconfig
import (
"fmt"
"reflect"
"strconv"
)
func parseIntForType(s string, base int, fieldType reflect.Kind) (i int64, err error) {
switch fieldType {
case reflect.Int:
return strconv.ParseInt(s, base, strconv.IntSize) //nolint:wrapcheck
case reflect.Int8:
return strconv.ParseInt(s, base, 8) //nolint:wrapcheck
case reflect.Int16:
return strconv.ParseInt(s, base, 16) //nolint:wrapcheck
case reflect.Int32:
return strconv.ParseInt(s, base, 32) //nolint:wrapcheck
case reflect.Int64:
return strconv.ParseInt(s, base, 64) //nolint:wrapcheck
default:
return 0, fmt.Errorf("unsupported type: %v", fieldType)
}
}
func parseUintForType(s string, base int, fieldType reflect.Kind) (uint64, error) {
switch fieldType {
case reflect.Uint:
return strconv.ParseUint(s, base, strconv.IntSize) //nolint:wrapcheck
case reflect.Uint8:
return strconv.ParseUint(s, base, 8) //nolint:wrapcheck
case reflect.Uint16:
return strconv.ParseUint(s, base, 16) //nolint:wrapcheck
case reflect.Uint32:
return strconv.ParseUint(s, base, 32) //nolint:wrapcheck
case reflect.Uint64:
return strconv.ParseUint(s, base, 64) //nolint:wrapcheck
default:
return 0, fmt.Errorf("unsupported type: %v", fieldType)
}
}