-
Notifications
You must be signed in to change notification settings - Fork 212
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
cups/string.c: Add _cups_snprintf()
#867
Conversation
Add the function which will remove the truncated UTF-8 multibyte characters.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was actually thinking of adding a public API - cupsFormatString
- and also want to do some other filtering of invalid/unsafe chars like we do for logging.
Make the previous function public, use `_cups_safe_vsnprintf()` and fix `_cups_safe_vsnprintf()` to use the almost whole buffer size (except for the last index, which is for null terminator)
@michaelrsweet ok, I made the function public + used _cups_safe_vsnprintf(), which does the job for logging - however it seems there was a bug in the function, which returned shorter string than the maximum possible, and returned higher number of written bytes than it actually wrote. |
Actually, that is the correct behavior. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See comments...
cups/debug.c
Outdated
@@ -570,7 +570,7 @@ _cups_safe_vsnprintf( | |||
* C character escapes... | |||
*/ | |||
|
|||
for (bufend --; *s && bufptr < bufend; s ++) | |||
for (; *s && bufptr < bufend; s ++) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is needed to avoid buffer overflow.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The decrement is there to prevent buffer overflow if we add an escaping character, right?
Shouldn't we check it in the specific 'ifs'? Otherwise this way we will lose one more character from string to additional null?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@michaelrsweet I've updated checks in other ifs to prevent buffer overflows, but use up all possible space for string. Did I understand it correctly that bufend decrement was to protect from overflow in characters which we need to escape?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, yes but I ended up doing things a little differently and pushed the results to CUPS 2.5 and libcups v3.
UTF-8 processing moved to `_cups_safe_vsnprintf()`, updated incrementing of return value and code style fixes
Fixes are in master. |
No problem, I hope I helped in some way in the end. |
@zdohnal For sure, aside from fixing the bug in this function, it also lead to related fixes in the |
Add the function which will remove the truncated UTF-8 multibyte characters.