ND2: prevent index out of bounds exceptions

Fixes #10311.