Avoid potential integer overflow spotted by UBSan
There were a couple of places where signed integer is wrapped around when overflow, though unlike unsigned integer, it is undefined behavior: https://www.gnu.org/software/autoconf/manual/autoconf-2.63/html_node/Integer-Overflow-Basics.html#Integer-Overflow-Basics
Spotted by UndefinedBehaviorSanitizer.