[4.2] fix #5235

Fixes issue #5235 (and adds test to reproduce issue and verify fix)