Skip to content

Avoid min/max crashes from mutating key functions#30207

Closed
zozo123 wants to merge 2 commits into
bazelbuild:masterfrom
zozo123:fix-starlark-extreme-mutation
Closed

Avoid min/max crashes from mutating key functions#30207
zozo123 wants to merge 2 commits into
bazelbuild:masterfrom
zozo123:fix-starlark-extreme-mutation

Conversation

@zozo123

@zozo123 zozo123 commented Jul 9, 2026

Copy link
Copy Markdown

Summary

  • Snapshot min/max inputs before invoking user-provided key functions, matching the safe behavior sorted already gets from copying its iterable first.
  • Add regression coverage for min, max, and sorted when the key function mutates the original list.
  • Keep key comparison as a straight-line loop so checked Starlark exceptions propagate normally and no stream wrapper allocation is needed.

Fixes #30201

Test plan

  • git diff --check
  • Islo Linux x86_64 sandbox: bazelisk test --test_output=errors --test_filter=net.starlark.java.eval.MethodLibraryTest //src/test/java/net/starlark/java/eval:EvalTests

Co-authored-by: Cursor <cursoragent@cursor.com>
@google-cla

google-cla Bot commented Jul 9, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@github-actions github-actions Bot added team-Starlark-Interpreter Issues involving the Starlark interpreter used by Bazel awaiting-review PR is awaiting review from an assigned reviewer labels Jul 9, 2026
Co-authored-by: Cursor <cursoragent@cursor.com>
@tetromino

Copy link
Copy Markdown
Contributor

Thanks for the PR, but I'd prefer to avoid unnecessary copies; and I think the more "Starlarkic" solution would be to prohibit key from mutating the sequence. That fix is in review internally at https://bazel-review.googlesource.com/c/bazel/+/339590

@tetromino tetromino closed this Jul 9, 2026
@github-actions github-actions Bot removed the awaiting-review PR is awaiting review from an assigned reviewer label Jul 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

team-Starlark-Interpreter Issues involving the Starlark interpreter used by Bazel

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bazel crash with fiendish Starlark code

2 participants