I think what your looking for can be found in the quake 1 source...
Code:
int NUM_FOR_EDICT(edict_t *e)
{
int b;
b = (byte *)e - (byte *)sv.edicts;
b = b / pr_edict_size;
if (b < 0 || b >= sv.num_edicts)
Sys_Error ("NUM_FOR_EDICT: bad pointer");
return b;
}
Basically make sure any calls to ENTINDEX are within range.