[12.x] Fix RateLimiter remaining() to handle negative maxAttempts values #58211
+15
−1
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.
Fixes #58014.
PR #57851 changed RateLimiter::remaining() to use max(0, $maxAttempts - $attempts) to prevent negative values when attempts exceed the limit. This broke cases where $maxAttempts is negative.
Before PR #57851, remaining('key', -1) returned a negative number, so !remaining('key', -1) was false. After the change, it returned 0, so !remaining('key', -1) became true, which is incorrect.
This fix checks if $maxAttempts >= 0 before applying the max() clamp. For negative values, it returns -1 to indicate unlimited attempts, restoring the previous behavior while keeping the fix for non-negative values.
The change preserves PR #57851's behavior for normal cases and restores the ability to disable rate limiting with negative values.