From: Alexander Aring <aahringo@redhat.com>
To: cluster-devel.redhat.com
Subject: [Cluster-devel] [PATCH dlm-tool 2/2] dlm_controld: fix rare off by one
Date: Wed, 5 Oct 2022 14:48:20 -0400 [thread overview]
Message-ID: <20221005184820.4129480-2-aahringo@redhat.com> (raw)
In-Reply-To: <20221005184820.4129480-1-aahringo@redhat.com>
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 <aahringo@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
prev parent reply other threads:[~2022-10-05 18:48 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-10-05 18:48 [Cluster-devel] [PATCH dlm-tool 1/2] dlm_controld: be sure we close logging at last Alexander Aring
2022-10-05 18:48 ` Alexander Aring [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20221005184820.4129480-2-aahringo@redhat.com \
--to=aahringo@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).