fix a severe bug in vector extension logic

This commit is contained in:
Lephenixnoir 2021-05-13 14:07:12 +02:00
parent 87d6d4eea4
commit cd4e3969bd
No known key found for this signature in database
GPG key ID: 1BBA026E13FC0495
3 changed files with 7 additions and 5 deletions

View file

@ -28,6 +28,7 @@ jlabel *jlabel_create(char const *text, void *parent)
l->color = C_BLACK;
l->font = NULL;
l->wrap_mode = J_WRAP_NONE;
l->text = text;
l->owns_text = false;

View file

@ -24,21 +24,22 @@ bool vec_add(vec_t *v, size_t n)
/* Allocate either size/2 new elements or 4 elements */
size_t ext = max(v->size / 2, 4);
/* Make sure that no more than 255 free elements will remain */
if(v->size + v->free + ext > n + 255)
ext = (n + 255) - (v->size + v->free);
if(v->free + ext > n + 255)
ext = (n + 255) - v->free;
size_t newsize = v->size + v->free + ext;
void *newdata = realloc(v->data, newsize * v->elsize);
if(!newdata) return false;
v->data = newdata;
v->free = newsize - n;
v->free = newsize - v->size - n;
v->size += n;
}
else {
v->free -= n;
v->size += n;
}
v->size += n;
return true;
}