Fix integer truncation in th_{tis2uni,uni2tis}_line #22
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.