max_index could have been less than min_index, which later caused integer underflow followed by a segfault in memcpy.