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

Fix integer truncation in th_{tis2uni,uni2tis}_line #22

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

marcmutz
Copy link

The size_t n parameter was narrowed to an int left variable, which is wrong on 64-bit platforms, as callers expect min(n, strlen(s) + 1) result characters to be written (incl. the terminating NUL), yet, due to the truncation, at most n mod 2^31 characters are written, which may be 0, if n is size_t(INT_MAX) + .

Fix by making the left variable also be of type size_t. This is safe, as each --left is protected by a left > 1 check, so the value can never become negative.

We can't fix the return type, that would be incompatible with existing users, but users don't need the return value: it's easily calculated as min(n - 1, strlen(s)). Add a FIXME comment nonetheless, for when compatibility can be broken, and use saturation arithmetic instead of modular arithmetic to avoid the pseudo-random return value.

Amend the documentation accordingly.

The size_t n parameter was narrowed to an int left variable, which is
wrong on 64-bit platforms, as callers expect min(n, strlen(s) + 1)
result characters to be written (incl. the terminating NUL), yet, due
to the truncation, at most n mod 2^31 characters are written, which
may be 0, if n is size_t(INT_MAX) + .

Fix by making the left variable also be of type size_t. This is safe,
as each --left is protected by a left > 1 check, so the value can
never become negative.

We can't fix the return type, that would be incompatible with existing
users, but users don't need the return value: it's easily calculated
as min(n - 1, strlen(s)). Add a FIXME comment nonetheless, for when
compatibility can be broken, and use saturation arithmetic instead of
modular arithmetic to avoid the pseudo-random return value.

Amend the documentation accordingly.
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

1 participant