Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

possible integer overflow on the number in the command buffer #239

Closed
vinc17fr opened this issue Jan 1, 2022 · 1 comment
Closed

possible integer overflow on the number in the command buffer #239

vinc17fr opened this issue Jan 1, 2022 · 1 comment

Comments

@vinc17fr
Copy link
Contributor

vinc17fr commented Jan 1, 2022

The cmd_int function in cmdbuf.c does not check integer overflows:

        for (p = cmdbuf;  *p >= '0' && *p <= '9';  p++)
                n = (n * 10) + (*p - '0');

Though one doesn't normally enter huge numbers, the result can be surprising.

Since the value (as stored in the variable number) is generally cast to int in command.c (which also makes the tests number > 0 before the cast incorrect, in particular), an immediate solution can be to saturate to INT_MAX by replacing the second line by

                n = n > (INT_MAX - (*p - '0')) / 10 ? INT_MAX : (n * 10) + (*p - '0');

This should avoid integer overflows completely.

Note that the consequence is that the values will be limited to INT_MAX for command P ("Go to the line containing byte offset N in the file."). However, this could affect only the viewing of files larger than 2 GB. Ideally, the types in the code should be cleaned up.

@gwsw
Copy link
Owner

gwsw commented Jan 1, 2022

Thanks, fixed in 88c0486.

@gwsw gwsw closed this as completed Jan 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants