Fix overflow in #5550

and test for #5550.

Ping @lindahua