fix an integer overflow (#5086), and pre-emptively avoid more of these