Simvik

Review: the ring buffer grew to 100 bytes

A change resized a ring buffer, but its index wrap still uses a bit mask. A medium embedded C problem (Code review for embedded C), run and graded in the…

medium · Code review for embedded C · find-the-bug · code-review · ring-buffer · bit-masks

Review comment. "This change makes RING_SIZE 100 so a whole NMEA sentence fits. But next() wraps the index with & (RING_SIZE - 1), which only works for a power of two. With 100, the mask is 0x63, so index 4 goes back to 0."

Fix next by changing one line, so the buffer works for any RING_SIZE.

Starter code

uint16_t next(uint16_t i) {
  return (uint16_t)((i + 1) & (RING_SIZE - 1));
}

Example tests

Six bytes through an empty ring

ring_t r = {0};
fill_and_drain(&r, 10, 6);

Expected output:

got: 10 11 12 13 14 15 (6 bytes)

Learn it first

More Code review for embedded C problems