mirror of
https://github.com/k3s-io/kubernetes.git
synced 2026-07-18 04:54:54 +00:00
Fail to generate unsigned min and max for negative values
This commit is contained in:
@@ -34,3 +34,12 @@ func rootTypeString(src, dst *types.Type) string {
|
||||
}
|
||||
return src.String() + " -> " + dst.String()
|
||||
}
|
||||
|
||||
// isInt returns true if t is a uint type.
|
||||
func isUnsignedInt(t *types.Type) bool {
|
||||
switch t {
|
||||
case types.Uint, types.Uint64, types.Uint32, types.Uint16, types.Byte:
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -226,7 +226,8 @@ func (minimumTagValidator) GetValidations(context Context, tag codetags.Tag) (Va
|
||||
|
||||
// This tag can apply to value and pointer fields, as well as typedefs
|
||||
// (which should never be pointers). We need to check the concrete type.
|
||||
if t := util.NonPointer(util.NativeType(context.Type)); !types.IsInteger(t) {
|
||||
t := util.NonPointer(util.NativeType(context.Type))
|
||||
if !types.IsInteger(t) {
|
||||
return result, fmt.Errorf("can only be used on integer types (%s)", rootTypeString(context.Type, t))
|
||||
}
|
||||
|
||||
@@ -234,6 +235,9 @@ func (minimumTagValidator) GetValidations(context Context, tag codetags.Tag) (Va
|
||||
if err != nil {
|
||||
return result, fmt.Errorf("failed to parse tag payload as int: %w", err)
|
||||
}
|
||||
if isUnsignedInt(t) && intVal < 0 {
|
||||
return result, fmt.Errorf("must be greater than or equal to zero for unsigned types (%s)", rootTypeString(context.Type, t))
|
||||
}
|
||||
result.AddFunction(Function(minimumTagName, DefaultFlags, minimumValidator, intVal))
|
||||
return result, nil
|
||||
}
|
||||
@@ -274,7 +278,8 @@ func (maximumTagValidator) GetValidations(context Context, tag codetags.Tag) (Va
|
||||
|
||||
// This tag can apply to value and pointer fields, as well as typedefs
|
||||
// (which should never be pointers). We need to check the concrete type.
|
||||
if t := util.NonPointer(util.NativeType(context.Type)); !types.IsInteger(t) {
|
||||
t := util.NonPointer(util.NativeType(context.Type))
|
||||
if !types.IsInteger(t) {
|
||||
return result, fmt.Errorf("can only be used on integer types (%s)", rootTypeString(context.Type, t))
|
||||
}
|
||||
|
||||
@@ -282,6 +287,9 @@ func (maximumTagValidator) GetValidations(context Context, tag codetags.Tag) (Va
|
||||
if err != nil {
|
||||
return result, fmt.Errorf("failed to parse tag payload as int: %w", err)
|
||||
}
|
||||
if isUnsignedInt(t) && intVal < 0 {
|
||||
return result, fmt.Errorf("must be greater than or equal to zero for unsigned types (%s)", rootTypeString(context.Type, t))
|
||||
}
|
||||
result.AddFunction(Function(maximumTagName, DefaultFlags, maximumValidator, intVal))
|
||||
return result, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user