From: Alexander Aring <aahringo@redhat.com>
To: cluster-devel.redhat.com
Subject: [Cluster-devel] [PATCHv2 dlm-tool 2/2] dlm_controld: fix rare off by one
Date: Wed, 5 Oct 2022 15:23:12 -0400 [thread overview]
Message-ID: <20221005192312.4130838-2-aahringo@redhat.com> (raw)
In-Reply-To: <20221005192312.4130838-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. If we
would hit the case that the buffer exactly matched we truncate the
string by one byte because we need to fit '\n' and '\0' into the buffer.
---
v2:
- remove sob.
- only really do the truncate of one byte when the buffer would exact
match which is the given size and the returned size + 1 ('\0').
dlm_controld/logging.c | 10 +++++++---
1 file changed, 7 insertions(+), 3 deletions(-)
diff --git a/dlm_controld/logging.c b/dlm_controld/logging.c
index 2c57138c..bfd7d274 100644
--- a/dlm_controld/logging.c
+++ b/dlm_controld/logging.c
@@ -181,10 +181,14 @@ 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 + 1 == len - pos)
+ pos += ret - 1;
+ else
+ pos += ret;
+ }
log_str[pos++] = '\n';
log_str[pos++] = '\0';
--
2.31.1
next prev parent reply other threads:[~2022-10-05 19:23 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-10-05 19:23 [Cluster-devel] [PATCHv2 dlm-tool 1/2] dlm_controld: be sure we close logging at last Alexander Aring
2022-10-05 19:23 ` Alexander Aring [this message]
2022-10-06 12:45 ` [Cluster-devel] [PATCHv2 dlm-tool 2/2] dlm_controld: fix rare off by one Alexander Aring
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=20221005192312.4130838-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).