check that stacks are not empty before top()

Change-Id: Iad5b3381b8201c5ac32a5bdf99bd3d5872ea3afc
(cherry picked from commit a1ea3838adee634ccf725bc5f2be7805c6294a16)
Reviewed-on: https://gerrit.libreoffice.org/8932
Reviewed-by: Miklos Vajna <vmiklos@collabora.co.uk>
Tested-by: Miklos Vajna <vmiklos@collabora.co.uk>