Skip to content

Avoid int overflow of the output index in gridDiskDistancesUnsafe - #1270

Open
madib06ops wants to merge 1 commit into
uber:masterfrom
madib06ops:griddisk-int64-idx
Open

madib06ops wants to merge 1 commit into
uber:masterfrom
madib06ops:griddisk-int64-idx

Conversation

@madib06ops

Copy link
Copy Markdown

gridDiskDistancesUnsafe fills the output with an int idx, but callers size that array with maxGridDiskSize(k), which has been 64 bit since #686. For k >= 26755 the disk is larger than INT32_MAX cells, so for an origin whose walk doesn't touch a pentagon (e.g. 8f283082800b390, res 15) idx++ overflows at algos.c:733 under UBSan and the next store goes to out[INT32_MIN], about 16 GiB before a correctly sized buffer. gridDisk and gridDiskDistances hit this because they try the unsafe path first. The int counters in gridRingUnsafe and _gridRingInternal, which index arrays of 6 * k, are widened the same way.

I checked it with a reproducer that backs the 16 GiB output with one 256 MiB file mapped repeatedly. Before the change it faults in gridDiskDistancesUnsafe under ASan/UBSan, and after it the call returns E_SUCCESS. There is no regression test because the case needs more than 2^31 output slots, which is too much for CI. The existing test suite passes.

@coderabbitai

coderabbitai Bot commented Oct 16, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: f3cfe3a1-84da-46fe-b876-b6dff0d94046
📥 Commits

Reviewing files that changed from the base of the PR and between 28d83cd and e2c3dd5.

📒 Files selected for processing: 2
  • CHANGELOG.md
  • src/h3lib/lib/algos.c

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

Walkthrough

Grid traversal output indexes in _gridRingInternal, gridDiskDistancesUnsafe, and gridRingUnsafe now use int64_t. The Unreleased changelog records the signed overflow fix for large disk sizes and the ring index updates.

Changes

Grid output indexes

Layer / File(s) Summary
Use 64-bit output indexes
src/h3lib/lib/algos.c, CHANGELOG.md
_gridRingInternal, gridDiskDistancesUnsafe, and gridRingUnsafe use int64_t for output indexes. The changelog records the fix for disk sizes where k >= 26755 and maxGridDiskSize(k) > INT32_MAX.

Priority: ➖ Normal

Estimated code review effort: 2 (Simple) | ~8 minutes

Change: Bug fix

Suggested reviewers: isaacbrodsky

Merge Risk: ⚪ Minimal · up to e2c3d

The change addresses large-disk index overflow. No actionable merge-blocking issue is established by the available evidence.

Pre-merge checks: Passed 5
Check name Status Explanation
Description check Passed The description clearly explains the signed integer overflow in gridDiskDistancesUnsafe and the related counter widening in gridRingUnsafe and _gridRingInternal.
Title check Passed The title clearly and concisely identifies the main change: preventing integer overflow in gridDiskDistancesUnsafe.
Docstring Coverage Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 3 functions across 1 files. (1 skipped: 1 …
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Docstring Coverage

Explanation

Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 3 functions across 1 files. (1 skipped: 1 unsupported.)

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coveralls

Copy link
Copy Markdown

Coverage Status

coverage: 99.758%. remained the same — madib06ops:griddisk-int64-idx into uber:master

This branch has not been deployed

No deployments
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 this pull request may close these issues.

2 participants