Always check the limits of array index variables before using them.

Obtained from:	DragonFlyBSD
MFC after:	1 week