Fix cpplint.
Move error handling code to avoid undefined behavior.