Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

builtin strings.replace_n is nondetermistic when patterns overlap #2822

Closed
srenatus opened this issue Oct 26, 2020 · 0 comments · Fixed by #2825
Closed

builtin strings.replace_n is nondetermistic when patterns overlap #2822

srenatus opened this issue Oct 26, 2020 · 0 comments · Fixed by #2825

Comments

@srenatus
Copy link
Contributor

I believe this has been noted in #1825, but got lost along the way: since map iteration is nondetermistic, so is the ordering in the replacement array built here. Since in strings.Replacer, "old string comparisons are done in argument order", this has implications on what gets replaced.

AFAICT it only affects overlapping matches.

Expected Behavior

No matter how often I run this,

strings.replace_n({"f": "1", "foo":"2"}, "foo")

I always get the same result.

Actual Behavior

> strings.replace_n({"f": "1", "foo":"2"}, "foo")
"1oo"
> strings.replace_n({"f": "1", "foo":"2"}, "foo")
"1oo"
> strings.replace_n({"f": "1", "foo":"2"}, "foo")
"1oo"
> strings.replace_n({"f": "1", "foo":"2"}, "foo")
"1oo"
> strings.replace_n({"f": "1", "foo":"2"}, "foo")
"1oo"
> strings.replace_n({"f": "1", "foo":"2"}, "foo")
"1oo"
> strings.replace_n({"f": "1", "foo":"2"}, "foo")
"1oo"
> strings.replace_n({"f": "1", "foo":"2"}, "foo")
"2"

Steps to Reproduce the Problem

Run the snippet above many times, note the output.

Additional Info

$ opa version
Version: 0.25.0-dev
Build Commit: 467182247-dirty
Build Timestamp: 2020-10-25T11:50:25Z
Build Hostname: Stephans-MacBook-Pro.local
Go Version: go1.15.2

But I don't think that this has much to do with the OPA version used. It was introduced in v0.15.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant