diff --git a/compiler.go b/compiler.go index e1167ad..cb1fab7 100644 --- a/compiler.go +++ b/compiler.go @@ -530,6 +530,9 @@ func (c *compiler) intsOperator(l int, r int, op string) (interface{}, error) { case "-": return l - r, nil case "/": + if r == 0 { + return nil, fmt.Errorf("division by zero %v %s %v", l, op, r) + } return l / r, nil case "*": return l * r, nil @@ -556,6 +559,9 @@ func (c *compiler) floatsOperator(l float64, r float64, op string) (interface{}, case "-": return l - r, nil case "/": + if r == 0 { + return nil, fmt.Errorf("division by zero %v %s %v", l, op, r) + } return l / r, nil case "*": return l * r, nil diff --git a/math_test.go b/math_test.go index a9071b3..25a8ca3 100644 --- a/math_test.go +++ b/math_test.go @@ -8,6 +8,24 @@ import ( "github.com/stretchr/testify/require" ) +func Test_Render_Int_Math_Division_By_Zero(t *testing.T) { + r := require.New(t) + input := `<%= 10 / 0 %>` + s, err := plush.Render(input, plush.NewContext()) + r.Error(err) + r.Empty(s) + r.Contains(err.Error(), "division by zero 10 / 0") +} + +func Test_Render_Int_Float_Division_By_Zero(t *testing.T) { + r := require.New(t) + input := `<%= 10.5 / 0.0 %>` + s, err := plush.Render(input, plush.NewContext()) + r.Error(err) + r.Empty(s) + r.Contains(err.Error(), "division by zero 10.5 / 0") +} + func Test_Render_Int_Math(t *testing.T) { r := require.New(t)