Add array size check to avoid out-of-range crash.