ND2: prevent StringIndexOutOfBounds

Fixes #10507.