While debugging I came across a rare off by one when the snprintf() filled string _exactly_ matched the size (with '\0') and we return the bytes written without \0. We will then write a "\n\0" pattern at the end but when the string exactly matched there is missing byte in the calculation of the "\n\0" pattern because the return value only reduced the size by one. To fix that we substract -1 from the return value of snprintf() to have at the end two bytes for the "\n\0" pattern. We just need to be careful we do that in cases where snprintf() doesn't return zero to not hit a negative offset calculation.
Signed-off-by: Alexander Aring <aahri...@redhat.com> --- dlm_controld/logging.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/dlm_controld/logging.c b/dlm_controld/logging.c index 2c57138c..42712d8b 100644 --- a/dlm_controld/logging.c +++ b/dlm_controld/logging.c @@ -181,10 +181,12 @@ void log_level(char *name_in, uint32_t level_in, const char *fmt, ...) ret = vsnprintf(log_str + pos, len - pos, fmt, ap); va_end(ap); - if (ret >= len - pos) + if (ret >= len - pos) { pos = len - 1; - else - pos += ret; + } else { + if (ret != 0) + pos += ret - 1; + } log_str[pos++] = '\n'; log_str[pos++] = '\0'; -- 2.31.1