From: "Emilio G. Cota" <cota@braap.org>
To: QEMU Developers <qemu-devel@nongnu.org>
Cc: Paolo Bonzini <pbonzini@redhat.com>,
Richard Henderson <rth@twiddle.net>,
Peter Maydell <peter.maydell@linaro.org>,
Changlong Xie <xiecl.fnst@cn.fujitsu.com>
Subject: [Qemu-devel] [PATCH 3/3] qdist: return "(empty)" instead of NULL when printing an empty dist
Date: Mon, 25 Jul 2016 11:03:45 -0400 [thread overview]
Message-ID: <1469459025-23606-4-git-send-email-cota@braap.org> (raw)
In-Reply-To: <1469459025-23606-1-git-send-email-cota@braap.org>
Printf'ing a NULL string is undefined behaviour. Avoid it.
Reported-by: Peter Maydell <peter.maydell@linaro.org>
Signed-off-by: Emilio G. Cota <cota@braap.org>
---
tests/test-qdist.c | 10 ++++++++--
util/qdist.c | 6 ++++--
2 files changed, 12 insertions(+), 4 deletions(-)
diff --git a/tests/test-qdist.c b/tests/test-qdist.c
index 0298986..9541ce3 100644
--- a/tests/test-qdist.c
+++ b/tests/test-qdist.c
@@ -360,10 +360,16 @@ static void test_none(void)
g_assert(isnan(qdist_xmax(&dist)));
pr = qdist_pr_plain(&dist, 0);
- g_assert(pr == NULL);
+ g_assert_cmpstr(pr, ==, "(empty)");
+ g_free(pr);
pr = qdist_pr_plain(&dist, 2);
- g_assert(pr == NULL);
+ g_assert_cmpstr(pr, ==, "(empty)");
+ g_free(pr);
+
+ pr = qdist_pr(&dist, 0, QDIST_PR_BORDER);
+ g_assert_cmpstr(pr, ==, "(empty)");
+ g_free(pr);
qdist_destroy(&dist);
}
diff --git a/util/qdist.c b/util/qdist.c
index cc31140..41eff08 100644
--- a/util/qdist.c
+++ b/util/qdist.c
@@ -14,6 +14,8 @@
#define NAN (0.0 / 0.0)
#endif
+#define QDIST_EMPTY_STR "(empty)"
+
void qdist_init(struct qdist *dist)
{
dist->entries = g_malloc(sizeof(*dist->entries));
@@ -234,7 +236,7 @@ char *qdist_pr_plain(const struct qdist *dist, size_t n)
char *ret;
if (dist->n == 0) {
- return NULL;
+ return g_strdup(QDIST_EMPTY_STR);
}
qdist_bin__internal(&binned, dist, n);
ret = qdist_pr_internal(&binned);
@@ -309,7 +311,7 @@ char *qdist_pr(const struct qdist *dist, size_t n_bins, uint32_t opt)
GString *s;
if (dist->n == 0) {
- return NULL;
+ return g_strdup(QDIST_EMPTY_STR);
}
s = g_string_new("");
--
2.5.0
next prev parent reply other threads:[~2016-07-25 15:04 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-07-25 15:03 [Qemu-devel] [PATCH 0/3] qdist fixes Emilio G. Cota
2016-07-25 15:03 ` [Qemu-devel] [PATCH 1/3] qdist: fix memory leak during binning Emilio G. Cota
2016-07-26 8:42 ` Marc-André Lureau
2016-07-25 15:03 ` [Qemu-devel] [PATCH 2/3] qdist: use g_realloc_n instead of g_realloc Emilio G. Cota
2016-07-26 8:48 ` Marc-André Lureau
2016-07-25 15:03 ` Emilio G. Cota [this message]
2016-08-01 8:11 ` [Qemu-devel] [PATCH 0/3] qdist fixes Paolo Bonzini
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=1469459025-23606-4-git-send-email-cota@braap.org \
--to=cota@braap.org \
--cc=pbonzini@redhat.com \
--cc=peter.maydell@linaro.org \
--cc=qemu-devel@nongnu.org \
--cc=rth@twiddle.net \
--cc=xiecl.fnst@cn.fujitsu.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).