#1241·h3

gridRing silently returns wrong results when the ring encircles pentagons without touching one

Author: AbhayindiaCreated Sep 17, 2026Updated Sep 17, 2026

gridRing returns silently wrong results when the ring encircles pentagons without touching one.

Reported against the Python bindings as uber/h3-py#503 by @veromies; @veromies confirmed there it belongs here rather than in the wrapper.

Reproduction

Origin is the res-1 cell containing (0, 0), which is not itself a pentagon. Comparing grid_ring(origin, k) against grid_disk(origin, k) - grid_disk(origin, k-1), which is the same set by definition:

  k   ring expect  uniq  missing  extra
 10     49     49    49        0      0
 11     50     50    50        0      0
 12     72     50    61       39     59  <-- wrong
 13     78     50    66       39     65  <-- wrong
 14     84     50    71       39     71  <-- wrong
 15     90     50    76       39     77  <-- wrong
 16     96     50    81       39     83  <-- wrong
 17     50     50    50        0      0
 18     48     48    48        0      0

For k in 12..16 the output is longer than the ring, contains duplicates (uniq < length), omits 39 cells that are genuinely at distance k, and includes cells that are not. E_SUCCESS is returned throughout, so a caller has no signal that anything went wrong.

Mechanism

The failure correlates exactly with pentagons lying strictly inside the ring while none lies on it:

k= 9  pentagons on ring=2  strictly inside=1   correct=True
k=10  pentagons on ring=2  strictly inside=3   correct=True
k=11  pentagons on ring=1  strictly inside=5   correct=True
k=12  pentagons on ring=0  strictly inside=6   correct=False
k=13  pentagons on ring=0  strictly inside=6   correct=False
k=14  pentagons on ring=0  strictly inside=6   correct=False
k=15  pentagons on ring=0  strictly inside=6   correct=False
k=16  pentagons on ring=0  strictly inside=6   correct=False
k=17  pentagons on ring=2  strictly inside=6   correct=True
k=18  pentagons on ring=2  strictly inside=8   correct=True

gridRing already has the right structure for this: it tries gridRingUnsafe and, on any error, wipes the output and falls back to _gridRingInternal.

c
H3Error H3_EXPORT(gridRing)(H3Index origin, int k, H3Index *out) {
    const H3Error failed = H3_EXPORT(gridRingUnsafe)(origin, k, out);
    if (!failed) {
        return E_SUCCESS;
    }
    memset(out, 0, 6 * k * sizeof(H3Index));
    return _gridRingInternal(origin, k, out);
}

So the fallback is not missing; the problem is that gridRingUnsafe reports success here. Its pentagon checks fire only when a cell it visits is a pentagon, and its final guard is

c
    // Check that this matches the expected lastIndex, if it doesn't,
    // it indicates pentagonal distortion occurred and we should report
    // failure.
    if (lastIndex != origin) {
        return E_PENTAGON;
    }

When the ring encircles pentagons without stepping on one, the traversal is distorted by the enclosed face structure but still closes back on the origin, so lastIndex == origin holds and the distortion goes unreported. Every k where a pentagon happens to land on the ring is caught by the existing checks and falls back correctly, which is why 9..11 and 17..18 are fine.

Shape of a fix

Because the fallback already exists and is correct, this only needs gridRingUnsafe to recognise the case and return E_PENTAGON. That keeps the fast path for every ring that does not enclose a pentagon, which matters: @veromies noted on the h3-py issue that he uses grid_ring to expand outward from a cell, and unconditionally routing through the BFS would make that slower than a hand-written stateful BFS.

The closure test needs to be stronger than "did the walk return to the origin". Two candidates, both cheap relative to the BFS:

  • check that the accumulated rotations is back to its starting value at closure, not just the index, since it is the face-crossing bookkeeping that the enclosed pentagons perturb
  • check that the traversal produced 6 * k distinct cells, which the duplicates above already violate

I have not yet established which of these is sufficient in general, and I would rather confirm that against the library than guess. Happy to put up a PR if the direction looks right.

Reproduced with h3-py 4.5.0.