summaryrefslogtreecommitdiff
path: root/util/qdist.c
diff options
context:
space:
mode:
authorEmilio G. Cota <cota@braap.org>2016-07-25 11:03:44 -0400
committerPaolo Bonzini <pbonzini@redhat.com>2016-08-03 18:42:35 +0200
commit071d4054770205ddb8a58a9e2735069d8fe52af1 (patch)
tree3004d8291f4f13c857327005942d7908123652e4 /util/qdist.c
parentf9dbc19e8bf58d0cbc830083352475bb16f315c4 (diff)
qdist: use g_renew and g_new instead of g_realloc and g_malloc.
This is safer against overflow. g_renew is available in all version of glib, while g_realloc_n is only available in 2.24. Signed-off-by: Emilio G. Cota <cota@braap.org> Message-Id: <1469459025-23606-3-git-send-email-cota@braap.org> [Rewritten to use g_new/g_renew. - Paolo] Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Diffstat (limited to 'util/qdist.c')
-rw-r--r--util/qdist.c7
1 files changed, 3 insertions, 4 deletions
diff --git a/util/qdist.c b/util/qdist.c
index eb2236cdc8..e95722b8a1 100644
--- a/util/qdist.c
+++ b/util/qdist.c
@@ -16,7 +16,7 @@
void qdist_init(struct qdist *dist)
{
- dist->entries = g_malloc(sizeof(*dist->entries));
+ dist->entries = g_new(struct qdist_entry, 1);
dist->size = 1;
dist->n = 0;
}
@@ -62,8 +62,7 @@ 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_renew(struct qdist_entry, dist->entries, dist->size);
}
dist->n++;
entry = &dist->entries[dist->n - 1];
@@ -188,7 +187,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_realloc_n(to->entries, n, sizeof(*to->entries));
+ to->entries = g_renew(struct qdist_entry, to->entries, n);
to->n = from->n;
memcpy(to->entries, from->entries, sizeof(*to->entries) * to->n);
return;