This logic is unsound.  Say that len == 14.  In that case, next_power_of_two(14 + 2) - 2 is still 14.