Repository: gohugoio/hugo (language: go)

Issue:
Variadic math functions do not validate number of args


&nbsp;|v0.111.2|v0.111.3
:--|:--|:--
`{{ add 1 }}`|wrong number of args for add: want 2 got 1|no error
`{{ sub 1 }}`|wrong number of args for sub: want 2 got 1|no error
`{{ mul 1 }}`|wrong number of args for mul: want 2 got 1|no error
`{{ div 1 }}`|wrong number of args for div: want 2 got 1|no error
`{{ math.Max 1 }}`|wrong number of args for Max: want 2 got 1|no error
`{{ math.Min 1 }}`|wrong number of args for Min: want 2 got 1|no error

cc @septs 


Discussion / hints:

Fix patch:
diff --git a/tpl/math/math.go b/tpl/math/math.go
index e7460e7ec41..a1c12425f49 100644
--- a/tpl/math/math.go
+++ b/tpl/math/math.go
@@ -23,6 +23,10 @@ import (
 	"github.com/spf13/cast"
 )
 
+var (
+	errMustTwoNumbersError = errors.New("must provide at least two numbers")
+)
+
 // New returns a new instance of the math-namespaced template functions.
 func New() *Namespace {
 	return &Namespace{}
@@ -73,6 +77,10 @@ func (ns *Namespace) Log(n any) (float64, error) {
 
 // Max returns the greater of the multivalued numbers n1 and n2 or more values.
 func (ns *Namespace) Max(inputs ...any) (maximum float64, err error) {
+	if len(inputs) < 2 {
+		err = errMustTwoNumbersError
+		return
+	}
 	var value float64
 	for index, input := range inputs {
 		value, err = cast.ToFloat64E(input)
@@ -91,6 +99,10 @@ func (ns *Namespace) Max(inputs ...any) (maximum float64, err error) {
 
 // Min returns the smaller of multivalued numbers n1 and n2 or more values.
 func (ns *Namespace) Min(inputs ...any) (minimum float64, err error) {
+	if len(inputs) < 2 {
+		err = errMustTwoNumbersError
+		return
+	}
 	var value float64
 	for index, input := range inputs {
 		value, err = cast.ToFloat64E(input)
@@ -176,6 +188,9 @@ func (ns *Namespace) Sub(inputs ...any) (any, error) {
 }
 
 func (ns *Namespace) doArithmetic(inputs []any, operation rune) (value any, err error) {
+	if len(inputs) < 2 {
+		return nil, errMustTwoNumbersError
+	}
 	value = inputs[0]
 	for i := 1; i < len(inputs); i++ {
 		value, err = _math.DoArithmetic(value, inputs[i], operation)


Test patch:
diff --git a/tpl/math/math_test.go b/tpl/math/math_test.go
index 076dbbae1d8..3e83405fd60 100644
--- a/tpl/math/math_test.go
+++ b/tpl/math/math_test.go
@@ -36,15 +36,19 @@ func TestBasicNSArithmetic(t *testing.T) {
 		{ns.Add, []any{4, 2}, int64(6)},
 		{ns.Add, []any{4, 2, 5}, int64(11)},
 		{ns.Add, []any{1.0, "foo"}, false},
+		{ns.Add, []any{0}, false},
 		{ns.Sub, []any{4, 2}, int64(2)},
 		{ns.Sub, []any{4, 2, 5}, int64(-3)},
 		{ns.Sub, []any{1.0, "foo"}, false},
+		{ns.Sub, []any{0}, false},
 		{ns.Mul, []any{4, 2}, int64(8)},
 		{ns.Mul, []any{4, 2, 5}, int64(40)},
 		{ns.Mul, []any{1.0, "foo"}, false},
+		{ns.Mul, []any{0}, false},
 		{ns.Div, []any{4, 2}, int64(2)},
 		{ns.Div, []any{4, 2, 5}, int64(0)},
 		{ns.Div, []any{1.0, "foo"}, false},
+		{ns.Div, []any{0}, false},
 	} {
 
 		result, err := test.fn(test.values...)
@@ -390,6 +394,9 @@ func TestMax(t *testing.T) {
 		{[]any{0, "a"}, false},
 		{[]any{"a", 0}, false},
 		{[]any{"a", "b"}, false},
+		// miss values
+		{[]any{}, false},
+		{[]any{0}, false},
 		// multi values
 		{[]any{-1, -2, -3}, -1.0},
 		{[]any{1, 2, 3}, 3.0},
@@ -434,6 +441,9 @@ func TestMin(t *testing.T) {
 		{[]any{0, "a"}, false},
 		{[]any{"a", 0}, false},
 		{[]any{"a", "b"}, false},
+		// miss values
+		{[]any{}, false},
+		{[]any{0}, false},
 		// multi values
 		{[]any{-1, -2, -3}, -3.0},
 		{[]any{1, 2, 3}, 1.0},