From 90e973ce37e5b4bfbf57cd354ea095c0cf75269d Mon Sep 17 00:00:00 2001
From: Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
Date: Tue, 8 Nov 2016 00:36:04 -0500
Subject: [PATCH] fix memory allocation bug discovered by CI :-)

---
 include/villas/hist.h | 3 ++-
 lib/hist.c            | 6 ++++--
 tests/hist.c          | 4 +++-
 3 files changed, 9 insertions(+), 4 deletions(-)

diff --git a/include/villas/hist.h b/include/villas/hist.h
index abb3ea091..9779ffef6 100644
--- a/include/villas/hist.h
+++ b/include/villas/hist.h
@@ -11,6 +11,7 @@
 #define _HIST_H_
 
 #include <stdio.h>
+#include <stdint.h>
 
 #include "config.h"
 
@@ -46,7 +47,7 @@ struct hist {
 };
 
 /** Initialize struct hist with supplied values and allocate memory for buckets. */
-void hist_create(struct hist *h, double start, double end, double resolution);
+int hist_create(struct hist *h, double start, double end, double resolution);
 
 /** Free the dynamically allocated memory. */
 void hist_destroy(struct hist *h);
diff --git a/lib/hist.c b/lib/hist.c
index 21baa6784..2cda30811 100644
--- a/lib/hist.c
+++ b/lib/hist.c
@@ -19,7 +19,7 @@
 #define VAL(h, i)	((h)->low + (i) * (h)->resolution)
 #define INDEX(h, v)	round((v - (h)->low) / (h)->resolution)
 
-void hist_create(struct hist *h, double low, double high, double resolution)
+int hist_create(struct hist *h, double low, double high, double resolution)
 {
 	h->low = low;
 	h->high = high;
@@ -27,7 +27,7 @@ void hist_create(struct hist *h, double low, double high, double resolution)
 
 	if (resolution > 0) {
 		h->length = (high - low) / resolution;
-		h->data = alloc(h->length * sizeof(unsigned));
+		h->data = alloc(h->length * sizeof(hist_cnt_t));
 	}
 	else {
 		h->length = 0;
@@ -35,6 +35,8 @@ void hist_create(struct hist *h, double low, double high, double resolution)
 	}
 
 	hist_reset(h);
+	
+	return 0;
 }
 
 void hist_destroy(struct hist *h)
diff --git a/tests/hist.c b/tests/hist.c
index df9c655e8..aeca35701 100644
--- a/tests/hist.c
+++ b/tests/hist.c
@@ -18,8 +18,10 @@ const int hist_result[] = {};
 
 Test(hist, simple) {
 	struct hist h;
+	int ret;
 	
-	hist_create(&h, -100, 100, 1);
+	ret = hist_create(&h, -100, 100, 1);
+	cr_assert_eq(ret, 0);
 	
 	for (int i = 0; i < ARRAY_LEN(test_data); i++)
 		hist_put(&h, test_data[i]);