Skip to contentSkip to conversation

qdist fixes

PATCH Applied Emilio G. Cota 3 participants 7 messages Jul 25, 2016 – Aug 1, 2016

lore
Share & tools
lore
#1 lore raw

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
index 56f5738..eb2236c 100644
@@ -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
#2 lore raw

Re: [Qemu-devel] [PATCH 1/3] qdist: fix memory leak during binning

Hi

Quoted 13 lines · Emilio G. Cota

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 +0 -0
index 56f5738..eb2236c 100644

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 -1
         to->n = from->n;
         memcpy(to->entries, from->entries, sizeof(*to->entries) * to->n);
         return;
--

2.5.0

Signature · 1 line
Marc-André Lureau
#3 lore raw

PATCH 2/3 qdist: use g_realloc_n instead of g_realloc

In reply to Emilio G. Cota

While 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
index eb2236c..cc31140 100644
@@ -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
Reviewed-by on v1 2/3
#4 lore raw

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.

---
 util/qdist.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
util/qdist.c +2 -3
index eb2236c..cc31140 100644
@@ -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
#5 lore raw

PATCH 3/3 qdist: return "(empty)" instead of NULL when printing an empty dist

In reply to Emilio G. Cota

Printf'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
index 0298986..9541ce3 100644
@@ -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
index cc31140..41eff08 100644
@@ -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