Resolve fdo#79913 by checking index bounds

Change-Id: I023a625d73724332245d612a61cb786acefc4cf2
Reviewed-on: https://gerrit.libreoffice.org/9791
Reviewed-by: Jean-Baptiste Faure <jbfaure@libreoffice.org>
Reviewed-by: Caolán McNamara <caolanm@redhat.com>
Tested-by: Caolán McNamara <caolanm@redhat.com>
(cherry picked from commit 7eceffef49c9f498c5758d5e351c0f22126003e5)