use safe math (#3249)

11 jobs for master in 25 minutes and 37 seconds (queued for 4 seconds)