Unit tests works. Pushed. Thanks!
On Friday 12 of April 2013 12:55:36 Martin Milata wrote:
Fixes rhbz#951432.
Signed-off-by: Martin Milata <mmilata(a)redhat.com>
---
lib/cluster.c | 2 +-
lib/strbuf.c | 13 ++++---------
lib/utils.c | 13 +++++++++++++
3 files changed, 18 insertions(+), 10 deletions(-)
diff --git a/lib/cluster.c b/lib/cluster.c
index 4a93649..c7f174a 100644
--- a/lib/cluster.c
+++ b/lib/cluster.c
@@ -76,7 +76,7 @@ cluster_add_index(struct cluster *cluster, int index)
if (cluster->size >= cluster->alloced)
{
cluster->alloced = cluster->alloced >= 1 ? cluster->alloced * 2 :
1; - cluster->objects = realloc(cluster->objects, sizeof
(*cluster->objects) + cluster->objects =
btp_realloc(cluster->objects, sizeof (*cluster->objects) *
cluster->alloced);
}
cluster->objects[cluster->size++] = index;
diff --git a/lib/strbuf.c b/lib/strbuf.c
index 061192d..532337d 100644
--- a/lib/strbuf.c
+++ b/lib/strbuf.c
@@ -77,15 +77,10 @@ btp_strbuf_grow(struct btp_strbuf *strbuf, int num)
{
if (strbuf->len + num + 1 > strbuf->alloc)
{
- while (strbuf->len + num + 1 > strbuf->alloc)
- strbuf->alloc *= 2; /* huge grow = infinite loop */
-
- strbuf->buf = realloc(strbuf->buf, strbuf->alloc);
- if (!strbuf->buf)
- {
- puts("Error while allocating memory for string buffer.");
- exit(5);
- }
+ while (strbuf->len + num + 1 > strbuf->alloc)
+ strbuf->alloc *= 2; /* huge grow = infinite loop */
+
+ strbuf->buf = btp_realloc(strbuf->buf, strbuf->alloc);
}
}
diff --git a/lib/utils.c b/lib/utils.c
index 1b9495e..779e70a 100644
--- a/lib/utils.c
+++ b/lib/utils.c
@@ -52,6 +52,19 @@ btp_mallocz(size_t size)
return ptr;
}
+void *
+btp_realloc(void *ptr, size_t size)
+{
+ void *result = realloc(ptr, size);
+ /* When size is 0, realloc may return NULL on success. */
+ if (!result && size > 0)
+ {
+ fprintf(stderr, "btp: out of memory");
+ exit(1);
+ }
+ return result;
+}
+
char *
btp_vasprintf(const char *format, va_list p)
{