lib/idr.c says it returns positive IDs always (actually the comments say "in the range 0 ... 0x7fffffff"). So I guess we would want something like: if (!ret) next_id = id == INT_MAX ? 0 : id + 1; (current code has a similar bug, plus exposes undefined behavior of signed overflow). - R.