Fix a bug that a division overflow causes a fatal error

Redmine: fixes #2307