Learning center

Settle an off-by-one review comment with a boundary test

An off-by-one claim is a claim about one index, so a test at that boundary decides it faster than any argument about the code. Write a test that pins the first value, the last value and the first value past the end. If it fails, change the code. If it passes and the convention it relies on is documented, dismiss the comment and cite the test.

Restate the claim as an input and an expected index#

A bot comment such as "this slice drops the last element" or "the loop should use <=" is a prediction about what one input produces. Before you read the code a second time, turn it into a concrete case: for this input, which index or item should come out first, and which should come out last?

Then check the convention each side assumes. Most disputed boundaries are half-open ranges, where the start is included and the end is not:

  • Python defines a slice from i to j as the items with index k such that i <= k < j (common sequence operations), so "abcdef"[1:3] is "bc", and range(1, 4) yields 1, 2 and 3.
  • JavaScript's Array.prototype.slice excludes its end index: [0, 1, 2, 3, 4].slice(1, 3) is [1, 2].
  • SQL's BETWEEN is closed. PostgreSQL documents it as "inclusive of the range endpoints" (comparison operators), so created_at BETWEEN '2026-11-01' AND '2026-11-30' misses most of 30 November when the column holds timestamps.
  • Pagination with LIMIT and OFFSET starts page n at offset (n - 1) * size when pages are numbered from 1.

A reviewer that reads a half-open range as closed, or the reverse, will report an off-by-one in correct code. A reviewer that reads both correctly can still be right, because the bug is often where two conventions meet: a buffer where position 0 holds something other than the first item, or an API that counts from 1 called by code that counts from 0.

Follow a flagged kernel that shipped the bug#

A vLLM pull request added support for the bad_words sampling parameter to a new model runner, including a Triton kernel with a branch for speculative decoding. On 30 January 2026, gemini-code-assist's review summary reported "a critical off-by-one error in the speculative decoding kernel that could lead to incorrect behavior" (review). Its inline threads are marked resolved, and GitHub hides their text, so the page does not show which line the comment named or what anyone answered. No later commit title mentions a boundary fix, and the pull request merged on 17 February 2026.

In August 2026, a follow-up pull request fixed an off-by-one in that kernel, present "since the kernel was introduced in #33433." Its description explains the defect: "every draft-region read was shifted back by one token (the boundary token was read twice)." (pull request) The input buffer holds the last committed token at position 0 and the draft tokens from position 1, and the kernel read the drafts from position 0. The fix is one term:

             spec_offset = actual_pos - output_len
-            actual = tl.load(input_ids_ptr + cur_req_first_pos + spec_offset)
+            actual = tl.load(input_ids_ptr + cur_req_first_pos + spec_offset + 1)

Diff

A few lines of Python reproduce the indexing. With committed tokens [5, 10, 11] and an input buffer of [11, 12, 13], the old read returns [5, 10, 11, 11, 12] for the five positions, and the fixed read returns [5, 10, 11, 12, 13].

We cannot tell from the pages whether the January comment pointed at this line. What the two pull requests do show is that a resolved thread is not a checked claim. The August author described their new test as the "first coverage for this kernel". It has four cases at the boundary: a bad-word prefix entirely inside the drafts, one spanning the last committed token and the first draft, one that would match only if the boundary token were read twice, and one entirely in committed tokens. They reported "3 failed, 1 passed" with the old kernel swapped in and "4 passed" with the fix.

That test is what the January thread needed. Each case takes a few lines, and the kernel as merged in February contains the line the August fix changed, so a test like it could have settled the thread before the merge, whichever line the comment named.

Write the test at the boundary, not in the middle#

Pick inputs that sit on the boundary the comment names:

  1. The empty case: zero items, page past the end, a range whose start equals its end.
  2. The first item, and the last item.
  3. A case that spans the seam, such as a page boundary, the end of a buffer or the switch from committed to draft tokens.
  4. The first value outside the range, which must not appear.

Assert exact indexes or items, not just a count. A count can be right while the window is shifted by one, as in the vLLM case, where the old kernel still read five tokens.

Decide from the test#

Boundary test resultAnswer
Fails as the comment predictedChange the code, and keep the test
Fails, but differently from the comment's predictionChange the code for the defect the test shows, and say in the thread that the comment's diagnosis was different
Passes, and the convention is documented in the language, library or APIDismiss, citing the test and the documented convention
Passes, but the convention exists only in the author's headKeep the test, and name the convention in a comment or a variable name such as end_exclusive
You cannot build the input in a testTreat the comment as unresolved, not as dismissed

The January thread was marked resolved with no boundary test on record, which is what the last row warns against. If you need the history of a finding like this one, the guide to reconstructing an accepted finding shows how to tie a later fix to an earlier comment, and date shifts across time zones covers the off-by-one that is really a time zone conversion.

One month of free code review.

New organizations get one calendar month of hosted code review for private and public GitHub repositories. No card or cloud connection required. Eligibility and fair-use terms.

Start free with GitHub

Related reading