Fix the bug that int overflow can bypass the range check

Fix the bug mentioned in #1266