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

Check for buffer underruns in checkType #285

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Conversation

rkta
Copy link
Contributor

@rkta rkta commented Nov 17, 2023

This fixes Issue #282.

etc.c Outdated
@@ -389,6 +389,7 @@ checkType(Str s, Lineprop **oprop, Linecolor **ocolor)
else {
Strshrink(s, plen);
prop -= plen;
prop = prop < prop_buffer ? prop_buffer : prop;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strictly speaking, a pointer going out of bound (with one exception of 1 past the end) is already UB 1. Instead of trying to check for UB and then correcting it, it's better to check plen before subtracting.

if (prop - prop_buffer < plen) // would underflow
    prop = prop_buffer;
else
    prop -= plen;

Footnotes

  1. https://port70.net/~nsz/c/c99/n1256.html#6.5.6p8

@rkta
Copy link
Contributor Author

rkta commented Nov 17, 2023 via email

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

Successfully merging this pull request may close these issues.

None yet

2 participants