bidi: Be safer against bad input

Don't run off the end of an array that we
allocated to certain length.

Closes: #342
2 jobs for master in 2 minutes and 21 seconds (queued for 1 second)
Status Job ID Name Coverage
  Build
passed #350851
gnome-runtime-linux

00:02:21

passed #350852
win32
msys2-mingw64

00:02:09