Patch to check for integer overflow. It has been
commented on and approved by Richard Smith.