ast: restore location on unsafe-var errors from rewritten head vars#8776
Open
arpitjain099 wants to merge 1 commit into
Open
ast: restore location on unsafe-var errors from rewritten head vars#8776arpitjain099 wants to merge 1 commit into
arpitjain099 wants to merge 1 commit into
Conversation
checkSafetyRuleHeads built the rego_unsafe_var_error with vars[v].Location, but read that location after v had already been reassigned to its original (rewritten-back) name. vars is keyed by the rewritten head var, so the lookup missed and the error came out with no location. This regressed in v1.14.0: a head variable declared with `some` (and therefore rewritten) lost its file:line in the error. Capture the location from the original key before the reassignment. Fixes open-policy-agent#8719 Signed-off-by: Arpit Jain <arpitjain099@gmail.com>
| // original name below: vars is keyed by the (possibly | ||
| // rewritten) head var, so looking up vars[v] after the | ||
| // reassignment would miss the location. See #8719. | ||
| loc := vars[v].Location |
Contributor
There was a problem hiding this comment.
We could consider the root cause of the issue to be where the rewrite is happening. Could there be a slight risk of a regression here if the location isn't always attached to the rewritten var?
Maybe we could make this handle a wider variety of upstream mishaps by simply using the location from v or w; whichever is non-nil.
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.
What
opa checkon a policy with an unsafe variable that appears in a rule head and is rewritten (for example, declared withsome) reports the error without a location:As noted in #8719, this regressed in v1.14.0 (v1.13 reported
p.rego:3: rego_unsafe_var_error: var x is unsafe).Why
In
checkSafetyRuleHeads, the error is built withvars[v].Location, wherevarscomes fromr.Head.Vars()and is keyed by the head variables. The location is read aftervhas been reassigned to its original name viaRewrittenVars:When the head var is rewritten (e.g. by
some x),varsis keyed by the rewritten name, sovars[v]after the reassignment misses and returns a zero entry with a nil location.Fix
Capture the location from the original key before reassigning
v. With the fix:Added a regression case to
TestCompilerCheckSafetyVarLoc(it nil-panics on the location lookup without the fix). The fullv1/astsuite andgofmtpass.Fixes #8719