fix: add termination guards to nanoid_optimized()#33
Conversation
|
Warning Review limit reachedYou’ve reached a temporary PR review limit under our Fair Usage Limits Policy. Next review available in: 24 minutes Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available. How can I continue?After more reviews become available, a review can be triggered using the To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews. How do review limits work?CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability. For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window. Please refer docs for additional details. 📝 WalkthroughWalkthrough
ChangesOptimized NanoID termination guards
Estimated code review effort: 2 (Simple) | ~10 minutes Possibly related issues
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@nanoid.sql`:
- Around line 105-118: Update the alphabet validation in the nanoid guard to
align with the minimal checks it performs: either remove the unsupported “bigger
than 255 symbols” claim from the exception message, or add the corresponding
upper-bound condition if that constraint is required. Preserve the existing null
and empty-alphabet validation and the documented termination-guard scope.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 607fea14-c7b3-4f7e-90cb-cad30dce1219
📒 Files selected for processing (3)
README.mddev/test/regression_tests.sqlnanoid.sql
…check #32 The alphabet termination guard message claimed the alphabet could not be "bigger than 255 symbols", but the guard only checks for NULL or empty alphabet (a >255 alphabet does not cause the generation loop to spin, so it is out of the termination-guard scope). Removed the stale upper-bound claim so the message matches the condition.
Closes #32
What this PR does
Adds minimal termination guards to
nanoid_optimized(). The only exit of its generation loop is reached after a character has been appended, so a direct call with a non-positive size, an empty or NULL alphabet, or a NULL or non-positive mask or step never terminates: it burns a connection and CPU until the backend is killed. These inputs now raise a clear exception up front instead.The "no checks" performance contract stays intact:
IFs before the loop, nothing in the per-byte path).nanoid_optimized()remains the root function for callers who know what they are doing.Also corrects the mask parameter description in the README and
nanoid.sql: the mask is(2^k) - 1where2^kis the smallest power of two greater than or equal to the alphabet size (the old wording callednitself "a power of 2"), and updates thenanoid_optimized()warning wording to reflect the guards.Same fix as shipped in the MySQL/MariaDB port (viascom/nanoid-mysql-mariadb#6).
Verification
dev/test/run_tests.sh(PostgreSQL 9.6 through 18, official Docker images): all versions PASS, including the new regression test that asserts the guard fires fornanoid_optimized(0, ...).Summary by CodeRabbit
Bug Fixes
nanoid_optimized()from entering infinite loops when given invalid generation parameters.Documentation
nanoid_optimized().