Fixes #4506 test length of context before checking invalid indexes