mirror of
https://git.rwth-aachen.de/acs/public/villas/node/
synced 2025-03-09 00:00:00 +01:00
fix memory allocation bug discovered by CI :-)
This commit is contained in:
parent
f50e449373
commit
90e973ce37
3 changed files with 9 additions and 4 deletions
|
@ -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);
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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]);
|
||||
|
|
Loading…
Add table
Reference in a new issue