Fixing memory leaks and indexes out of bounds

This commit is contained in:
Leonardo Vencovsky committed 2020-04-22 18:38:05 -03:00
1 parent 6d4b8618bf
commit 64201cb630
7 files changed
+50 -40

No files matched your search

+5 -1
View File
@@ -351,7 +351,7 @@ static const char *cmc_string_fmt_heap = "struct %s<%s> "
\
_heap_->buffer[_heap_->count++] = value; \
\
if (!PFX##_empty(_heap_)) \
if (PFX##_count(_heap_) > 1) \
{ \
PFX##_impl_float_up(_heap_, _heap_->count - 1); \
} \
@@ -739,6 +739,10 @@ static const char *cmc_string_fmt_heap = "struct %s<%s> "
/* Go to parent */ \
C = (C - 1) / 2; \
\
/* Prevent the two lines below from accessing a negative index */ \
if (C == 0) \
break; \
\
child = _heap_->buffer[C]; \
parent = _heap_->buffer[(C - 1) / 2]; \
} \
+1 -1
View File
@@ -805,7 +805,7 @@ static const char *cmc_string_fmt_sortedlist = "struct %s<%s> "
L = M + 1; \
} \
\
if (_list_->f_val->cmp(_list_->buffer[L - 1], value) == 0) \
if (L > 0 && _list_->f_val->cmp(_list_->buffer[L - 1], value) == 0) \
return L - 1; \
\
/* Not found */ \
+3 -1
View File
@@ -175,7 +175,7 @@ _Bool h_insert(struct heap *_heap_, size_t value)
return 0;
}
_heap_->buffer[_heap_->count++] = value;
if (!h_empty(_heap_))
if (h_count(_heap_) > 1)
{
h_impl_float_up(_heap_, _heap_->count - 1);
}
@@ -460,6 +460,8 @@ static void h_impl_float_up(struct heap *_heap_, size_t index)
_heap_->buffer[C] = _heap_->buffer[(C - 1) / 2];
_heap_->buffer[(C - 1) / 2] = tmp;
C = (C - 1) / 2;
if (C == 0)
break;
child = _heap_->buffer[C];
parent = _heap_->buffer[(C - 1) / 2];
}
+1 -1
View File
@@ -529,7 +529,7 @@ static size_t sl_impl_binary_search_last(struct sortedlist *_list_,
else
L = M + 1;
}
if (_list_->f_val->cmp(_list_->buffer[L - 1], value) == 0)
if (L > 0 && _list_->f_val->cmp(_list_->buffer[L - 1], value) == 0)
return L - 1;
return _list_->count;
}
+2
View File
@@ -242,6 +242,8 @@ CMC_CREATE_UNIT(IntervalHeap, true, {
cmc_assert_equals(size_t, 10000, ih_count(ih));
cmc_assert_equals(int32_t, 10000, total_create);
ih_free(ih);
// max
ih = ih_new(1, ih_fval);
+37 -36
View File
@@ -339,82 +339,82 @@ CMC_CREATE_UNIT(Queue, true, {
});
CMC_CREATE_TEST(copy_of, {
struct queue *d1 = q_new(100, q_fval);
struct queue *q1 = q_new(100, q_fval);
cmc_assert_not_equals(ptr, NULL, d1);
cmc_assert_not_equals(ptr, NULL, q1);
for (size_t i = 1; i <= 150; i++)
cmc_assert(q_enqueue(d1, i));
cmc_assert(q_enqueue(q1, i));
struct queue *d2 = q_copy_of(d1);
struct queue *q2 = q_copy_of(q1);
cmc_assert_not_equals(ptr, NULL, d2);
cmc_assert_equals(size_t, q_count(d1), q_count(d2));
cmc_assert_not_equals(ptr, NULL, q2);
cmc_assert_equals(size_t, q_count(q1), q_count(q2));
for (size_t i1 = d1->front, i2 = d2->front, j = 0; j < d1->count; j++)
for (size_t i1 = q1->front, i2 = q2->front, j = 0; j < q1->count; j++)
{
cmc_assert_equals(size_t, d1->buffer[i1], d2->buffer[i2]);
cmc_assert_equals(size_t, q1->buffer[i1], q2->buffer[i2]);
i1 = (i1 + 1) % d1->capacity;
i2 = (i2 + 1) % d2->capacity;
i1 = (i1 + 1) % q1->capacity;
i2 = (i2 + 1) % q2->capacity;
}
q_free(d1);
q_free(d2);
q_free(q1);
q_free(q2);
});
CMC_CREATE_TEST(equals, {
struct queue *d1 = q_new(100, q_fval);
struct queue *d2 = q_new(100, q_fval);
struct queue *q1 = q_new(100, q_fval);
struct queue *q2 = q_new(100, q_fval);
cmc_assert_not_equals(ptr, NULL, d1);
cmc_assert_not_equals(ptr, NULL, d2);
cmc_assert_not_equals(ptr, NULL, q1);
cmc_assert_not_equals(ptr, NULL, q2);
for (size_t i = 1; i <= 150; i++)
{
cmc_assert(q_enqueue(d1, i));
cmc_assert(q_enqueue(d2, i));
cmc_assert(q_enqueue(q1, i));
cmc_assert(q_enqueue(q2, i));
}
cmc_assert(q_equals(d1, d2));
cmc_assert(q_equals(q1, q2));
q_free(d1);
q_free(d2);
q_free(q1);
q_free(q2);
});
CMC_CREATE_TEST(equals[from copy], {
struct queue *d1 = q_new(100, q_fval);
struct queue *q1 = q_new(100, q_fval);
cmc_assert_not_equals(ptr, NULL, d1);
cmc_assert_not_equals(ptr, NULL, q1);
for (size_t i = 1; i <= 150; i++)
{
cmc_assert(q_enqueue(d1, i));
cmc_assert(q_enqueue(q1, i));
}
struct queue *d2 = q_copy_of(d1);
struct queue *q2 = q_copy_of(q1);
cmc_assert_not_equals(ptr, NULL, d2);
cmc_assert_not_equals(ptr, NULL, q2);
cmc_assert(q_equals(d1, d2));
cmc_assert(q_equals(q1, q2));
q_free(d1);
q_free(d2);
q_free(q1);
q_free(q2);
});
CMC_CREATE_TEST(equals[count = 0], {
struct queue *d1 = q_new(100, q_fval);
struct queue *q1 = q_new(100, q_fval);
cmc_assert_not_equals(ptr, NULL, d1);
cmc_assert_not_equals(ptr, NULL, q1);
struct queue *d2 = q_copy_of(d1);
struct queue *q2 = q_copy_of(q1);
cmc_assert_not_equals(ptr, NULL, d2);
cmc_assert_not_equals(ptr, NULL, q2);
cmc_assert(q_equals(d1, d2));
cmc_assert(q_equals(q1, q2));
q_free(d1);
q_free(d2);
q_free(q1);
q_free(q2);
});
CMC_CREATE_TEST(flag, {
@@ -474,6 +474,7 @@ CMC_CREATE_UNIT(Queue, true, {
cmc_assert_equals(int32_t, cmc_flags.OK, q_flag(q2));
q_free(q);
q_free(q2);
});
CMC_CREATE_TEST(callbacks, {
+1
View File
@@ -667,6 +667,7 @@ CMC_CREATE_UNIT(Stack, true, {
cmc_assert_equals(int32_t, cmc_flags.OK, s_flag(s2));
s_free(s);
s_free(s2);
});
CMC_CREATE_TEST(callbacks, {