Fix infinite loop reported by Jakup Wilk via Niels Thykier.