check that stacks are not empty before top()

Change-Id: Iad5b3381b8201c5ac32a5bdf99bd3d5872ea3afc
(cherry picked from commit a1ea3838adee634ccf725bc5f2be7805c6294a16)
Reviewed-on: https://gerrit.libreoffice.org/8933
Tested-by: Michael Stahl <mstahl@redhat.com>
Reviewed-by: Michael Stahl <mstahl@redhat.com>
(cherry picked from commit 1a8d06881ee7ba679da8b7cc37b53a20a04ee3e8)
Reviewed-on: https://gerrit.libreoffice.org/9127
Reviewed-by: Eike Rathke <erack@redhat.com>
Reviewed-by: Christian Lohmaier <lohmaier+LibreOffice@googlemail.com>
Tested-by: Christian Lohmaier <lohmaier+LibreOffice@googlemail.com>