Conversation
`{{ a % b }}` with a zero divisor panicked with Go's `runtime error:
integer divide by zero`, and the panic escaped Template.Execute: recover()
in eval.go re-panics anything that is a runtime.Error, so an operator that
can produce one has to guard before it runs. itemDiv does that already;
itemMod did not. Modulo by zero now results in a runtime error instead of
a panic, like division by zero since f1947cd.
`%` has no float form, so the divisor is truncated by toInt/toUint before
it is used. `{{ 10 % 0.5 }}` therefore divides by zero as well, and the
guard sits on the converted divisor rather than on the raw right operand.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
{{ a % b }}with a zero divisor panics with Go'sruntime error: integer divide by zero, and the panic escapesTemplate.Executeand kills the caller's process. The sibling/in the same switch already returns a clean jet runtime error.eval.go:1025-1034(pristine) reaches the%operator with no guard:This is not merely uncaught, it is deliberately not caught.
(*Runtime).recoverateval.go:237-241re-panics anything that is aruntime.Error:so an operator that can produce one has to guard before it runs. That is what
case itemDiv:does ateval.go:1004-1007.Reachability
Run against
607e931through the publicTemplate.Execute, with the caller's ownrecover()around the call so an escaped panic is visible:n,zero,uandhalfare ordinaryVarMapvariables, so this is reachable from template data, not just from a literal in the template source. After the fix each of those returnsJet Runtime Error ("/t":N): modulo by zero, and every in-range result is unchanged.Why this is a bug and not a design choice
Your own
case itemDiv:ateval.go:1005-1007already does this - it rejects a zero divisor withnode.Left.errorf("division by zero")so the caller gets a catchable jet runtime error - whilecase itemMod:twelve lines below does not.That guard was added in
f1947cd("fix division by zero panic"), whose message says "division by zero now results in a runtime error instead of a panic".%divides too, and was left as it was.Fix
The one design point worth stating: copying
itemDiv's guard literally asif right.IsZero()is not enough.%has no float form, so the divisor goes throughtoInt/toUintand is truncated -{{ 10 % 0.5 }}still reaches% 0and still panics. The guard has to sit on the converted divisor, which is why it is inside the three branches rather than above them.Test
TestModuloByZeroineval_test.go, next to the existingTestDivisionByZeroand asserting the same way: 8 zero-divisor templates that must return an error containingmodulo by zero, plus 5 in-range templates that must keep their current output. It uses a slice with subtests rather than the map the neighbour uses, because on the unfixed code the first case aborts the test binary and the ordering has to be deterministic.Red / green / mutation,
go test -count=1 -run TestModuloByZero .each time:int_modulo_by_zero-panic: runtime error: integer divide by zero [recovered, repanicked]ateval.go:1029, repanicked by(*Runtime).recoverateval.go:240. Unrecoverable by the caller, so no later subtest runsright.IsZero()onlyint_modulo_by_fraction-{{ 5 % 0.5 }}still panicsdivisor <= 0in all three branchesint_modulo_negative_divisor-{{ 5 % -3 }}wrongly rejectedVerification
CI scripts copied out of the config files:
.travis.ymlscriptenv GO111MODULE=on go test -v ./...appveyor.ymltest_script[0]go test -v ./...go vet ./...gofmt -l eval.go eval_test.gogo test -raceon both zero-divisor testsGOARCH=386 go build ./...,GOARCH=arm64 go build ./...appveyor.ymltest_script[1..4] (theexamples/asset_packagingsteps) need network forgithub.com/shurcooL/vfsgen, which is in neithergo.modnorgo.sum, so I could not run them; they fail the same way on an unmodified checkout here and never compileeval.go. The Travis matrix does not run them at all.Out of scope
The
isFloatbranch also saturates on conversion -{{ 1e19 % 10 }}prints-8, because the float-to-int64 conversion saturates. That is a separate and architecture-dependent question, this PR does not change it, and no test here asserts on it.Disclosure: this patch was prepared with AI assistance. I ran the tests, the red-green, both mutation directions and the reachability probe above myself, and the numbers are from those runs.