Fix the bug without using code that invokes undefined behaviour.