|
-
August 5th, 2011, 04:11 PM
#1
vsprintf with string arguments containing '%' character
I'm sure this must have been discussed before here, but I can't find a solution... I have a logging function which take variable args and writes them to a log file and/or console. This has worked fine for ages, but now some string parameters passed in can contain '%' characters which causes the output buffer passed to vsprintf to be corrupted.
Example:
void trace_to_log(char* format, ...)
{
va_list arg_ptr;
char buffer[4096];
va_start (arg_ptr, format);
vsprintf (buffer, format, arg_ptr);
va_end (arg_ptr);
// Write buffer to file and/or console
...
}
void f()
{
trace_to_log("%s", "%40");
}
I know that to print a '%' character it needs to passed in as '%%' but the problem is I don't have control over the many places in the code such a string could be passed in. I need some way of making the logging function safe. Maybe do my own parsing for %s formats and check the input strings and double up any '%' chars? Any advice appreciated.
Thanks
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|