Log messages might get truncated when C interface is used
Logging Framework for C++
Brought to you by:
wilx
Hi,
I'm using C interface of log4cplus 1.1.0 on Oracle Linux Server 6.3.
If a short format pattern is provided for log4cplus_logger_log(), which includes '%s' placeholder, then string, that should be printed, might appear truncated in log. The resulting message's length never exceeds 511 bytes.
This seems to be a result of a bug in snprintf_buf::print_va_list(), which prevents re-sizing of a destination buffer if it is found to be too small.
In attachment you can find a sample program.
Also I have crafted a simple patch, that fixes the issue (at least in Linux environment).
Thank you for the bug report and the patch. Unfortunately, as it stands, it is incomplete. The
va_copy()
is not portable to Visual Studio. We will have to improve it a little bit.Here is an alternative patch. It should fix the problem without any need for
va_copy()
.Please test the patch on your box.
Thank you for the patch. The situation with long strings is better now. I.e. they are not truncated at 511 border. But a different issue is now evident. If string, that should be printed exceeds the default buffer size, it will always be printed without the last character.
Consider checking my previous test case. There a program requests to print a 1024-byte long string with '%s%n' patter layout. This means, that printed string should be 1025 bytes long. But the library produces an output, that is only 1024-byte long with the trailing new line.
I suppose this is caused by the incorrect buffer size passed to the vsntprintf() function. It should be exactly buf_size instead of (buf_size - 1).
From man:
"The functions snprintf() and vsnprintf() write at most size bytes (including the trailing null byte ('\0')) to str.
"Upon successful return, these functions return the number of characters printed (not including the trailing '\0' used to end output to strings).
"The functions snprintf() and vsnprintf() do not write more than size bytes (including the trailing '\0'). If the output was truncated due to this limit then the return value is the number of characters (not including the trailing '\0') which would have been written to the final string if enough space had been available. Thus, a return value of size or more means that the output was truncated."
Apply this patch in addition to the previous one. I hope this fixes all of the issues now.
Yes, now everything seems to be in order.
Thank you for your support.
Closing.