fix bugs, add overflow check

Signed-off-by: glkclass <glkclass@gmail.com>