Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -1023,12 +1023,25 @@ func (st *Runtime) evalMultiplicativeExpression(node *MultiplicativeExprNode) re
node.Left.errorf("a non numeric value in multiplicative expression")
}
case itemMod:
// the divisor is truncated to an integer, so a non-zero fraction is a zero divisor too
if isInt(kind) {
left = reflect.ValueOf(left.Int() % toInt(right))
divisor := toInt(right)
if divisor == 0 {
node.Left.errorf("modulo by zero")
}
left = reflect.ValueOf(left.Int() % divisor)
} else if isFloat(kind) {
left = reflect.ValueOf(int64(left.Float()) % toInt(right))
divisor := toInt(right)
if divisor == 0 {
node.Left.errorf("modulo by zero")
}
left = reflect.ValueOf(int64(left.Float()) % divisor)
} else if isUint(kind) {
left = reflect.ValueOf(left.Uint() % toUint(right))
divisor := toUint(right)
if divisor == 0 {
node.Left.errorf("modulo by zero")
}
left = reflect.ValueOf(left.Uint() % divisor)
} else {
node.Left.errorf("a non numeric value in multiplicative expression")
}
Expand Down
53 changes: 53 additions & 0 deletions eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -869,6 +869,59 @@ func TestDivisionByZero(t *testing.T) {
}
}

func TestModuloByZero(t *testing.T) {
vars := VarMap{"u": reflect.ValueOf(uint(5))}

for _, tc := range []struct{ name, template string }{
{"int modulo by zero", `{{ 5 % 0 }}`},
{"int modulo by zero float", `{{ 5 % 0.0 }}`},
{"int modulo by fraction", `{{ 5 % 0.5 }}`},
{"float modulo by zero", `{{ 5.0 % 0 }}`},
{"float modulo by zero float", `{{ 5.0 % 0.0 }}`},
{"float modulo by fraction", `{{ 5.0 % 0.5 }}`},
{"uint modulo by zero", `{{ u % 0 }}`},
{"uint modulo by fraction", `{{ u % 0.5 }}`},
} {
t.Run(tc.name, func(t *testing.T) {
var set = NewSet(NewInMemLoader(), WithSafeWriter(nil))
tt, err := set.parse(tc.name, tc.template, false)
if err != nil {
t.Fatal(err)
}
err = tt.Execute(io.Discard, vars, nil)
if err == nil {
t.Fatal("expected modulo by zero to fail with a runtime error, but got nil")
}
if !strings.Contains(err.Error(), "modulo by zero") {
t.Fatalf("expected runtime error to be about modulo by zero, but got %q", err.Error())
}
})
}

for _, tc := range []struct{ name, template, expected string }{
{"int modulo", `{{ 5 % 2 }}`, "1"},
{"int modulo negative divisor", `{{ 5 % -3 }}`, "2"},
{"int modulo truncated divisor", `{{ 5 % 2.5 }}`, "1"},
{"float modulo", `{{ 5.5 % 2 }}`, "1"},
{"uint modulo", `{{ u % 3 }}`, "2"},
} {
t.Run(tc.name, func(t *testing.T) {
var set = NewSet(NewInMemLoader(), WithSafeWriter(nil))
tt, err := set.parse(tc.name, tc.template, false)
if err != nil {
t.Fatal(err)
}
buf := new(bytes.Buffer)
if err := tt.Execute(buf, vars, nil); err != nil {
t.Fatal(err)
}
if buf.String() != tc.expected {
t.Fatalf("expected %q, but got %q", tc.expected, buf.String())
}
})
}
}

func TestRecursiveInclude(t *testing.T) {
l := NewInMemLoader()
l.Set("recursive_incl_1", `{{ include "./recursive_incl_2" }}`)
Expand Down