Spotify
Spotify logoSpotify · Feature trail

Integer Underflow Bug in Round-Robin

1 post · Jul 2015

What this trail covers

This post details a specific integer underflow bug discovered in Spotify's Java implementation of the round-robin algorithm. The bug occurred when an AtomicInteger, intended to cycle through connection indices, unexpectedly returned Integer.MIN_VALUE instead of a positive value due to an underflow condition. This led to array index out-of-bounds exceptions and crashes. The post explains integer representation, the specific failure mode of the `index()` function, and provides two fixes: one using `Integer.MIN_VALUE` and another using a bitwise AND with `Integer.MAX_VALUE` to effectively reset the sign bit. It also highlights the bug's long lifespan and the specific conditions required for it to manifest.

All Spotify trails