Addressing coverity defect 18176 (possible out-of-bounds access) by introducing a check on the array indices.