Reverted f088588c fix for #6187 that fixes infinite loop, the fix cause a use-after-free regression.