qdist fixes
cover qdist fixes
While fixing the return of a NULL string when printing an empty dist (patch 3) (see background here [*]), I noticed there was a leak in qdist (patch 1). Patch 2 is trivial.
Thanks,
Emilio
[*] https://lists.gnu.org/archive/html/qemu-devel/2016-07/msg05494.html
PATCH 1/3 qdist: fix memory leak during binning
In qdist_bin__internal(), to->entries is initialized to a 1-element array, which we then leak when n == from->n. Fix it.
--- util/qdist.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
util/qdist.c +1 -1
@@ -188,7 +188,7 @@ void qdist_bin__internal(struct qdist *to, const struct qdist *from, size_t n) } }/* they're equally spaced, so copy the dist and bail out */ - to->entries = g_new(struct qdist_entry, from->n); + to->entries =g_realloc_n (to->entries, n,sizeof (*to->entries)); to->n = from->n;memcpy (to->entries, from->entries,sizeof (*to->entries) * to->n);return ;
Signature · 1 line
2.5.0
Re: [Qemu-devel] [PATCH 1/3] qdist: fix memory leak during binning
Hi
Quoted 13 lines · Emilio G. Cota
In
qdist_bin__internal(),to->entriesis initialized to a 1-element array, which we then leak when n ==from->n. Fix it.Signed-off-by Emilio G. Cota <[email protected]>--- util/qdist.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)util/qdist.c +0
-0
util/qdist.c void qdist_bin__internal(struct qdist *to, const struct qdist *from, size_t n) · line 191
Quoted 5 lines
util/qdist.c +1
-1@@ -188,7 +188,7 @@ void qdist_bin__internal(struct qdist *to, const struct qdist *from, size_t n) } }/* they're equally spaced, so copy the dist and bail out */ - to->entries = g_new(struct qdist_entry, from->n); + to->entries =g_realloc_n (to->entries, n,sizeof (*to->entries));
This is already part of the leak series:
https://lists.gnu.org/archive/html/qemu-devel/2016-07/msg04168.html
Quoted 7 lines
util/qdist.c +0
-1to->n = from->n;memcpy (to->entries, from->entries,sizeof (*to->entries) * to->n);return ; --2.5.0
Signature · 1 line
Marc-André Lureau
PATCH 2/3 qdist: use g_realloc_n instead of g_realloc
In reply to Emilio G. CotaWhile at it, remove the unnecessary parentheses around dist->size.
--- util/qdist.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
util/qdist.c +2 -2
@@ -62,8 +62,8 @@ void qdist_add(struct qdist *dist, double x, long count)if (unlikely (dist->n == dist->size)) { dist->size *=2 ; - dist->entries =g_realloc (dist->entries, -sizeof (*dist->entries) * (dist->size)); + dist->entries =g_realloc_n (dist->entries, dist->size, +sizeof (*dist->entries)); } dist->n++; entry = &dist->entries[dist->n -1 ];
Signature · 1 line
2.5.0
Re: [Qemu-devel] [PATCH 2/3] qdist: use g_realloc_n instead of g_realloc
Hi
Quoted 23 lines · Emilio G. Cota
While at it, remove the unnecessary parentheses around
dist->size.Signed-off-by Emilio G. Cota <[email protected]>--- util/qdist.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)util/qdist.c +2
-3@@ -62,8 +62,8 @@ void qdist_add(struct qdist *dist, double x, long count)if (unlikely (dist->n == dist->size)) { dist->size *=2 ; - dist->entries =g_realloc (dist->entries, -sizeof (*dist->entries) * (dist->size)); + dist->entries =g_realloc_n (dist->entries, dist->size, +sizeof (*dist->entries)); } dist->n++; entry = &dist->entries[dist->n -1 ]; --
Quoted 3 lines
2.5.0
Signature · 1 line
Marc-André Lureau
PATCH 3/3 qdist: return "(empty)" instead of NULL when printing an empty dist
In reply to Emilio G. CotaPrintf'ing a NULL string is undefined behaviour. Avoid it.
--- tests/test-qdist.c | 10 ++++++++-- util/qdist.c | 6 ++++-- 2 files changed, 12 insertions(+), 4 deletions(-)
tests/test-qdist.c +8 -2
@@ -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); }
util/qdist.c +4 -2
@@ -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 ("" );
Signature · 1 line
2.5.0
Paolo Bonzini
AppliedQuoted 10 lines · Emilio G. Cota
While fixing the return of a NULL string when printing an empty dist (patch 3) (see background here [*]), I noticed there was a leak in qdist (patch 1). Patch 2 is trivial.
Thanks,
Emilio
[*] https://lists.gnu.org/archive/html/qemu-devel/2016-07/msg05494.html
Queued all, thanks.
Paolo