validate index and length