Never return NULL from gwbuf_make_contiguous

By aborting the process if memory runs out when a buffer needs to be made
contiguous, we rule out other, more subtle, errors. Failing as soon as a
possible when memory allocation fails gives better error messages.
This commit is contained in:
Markus Mäkelä
2018-07-23 10:19:11 +03:00
parent 896c7deb03
commit ea5c5f3a07
2 changed files with 18 additions and 25 deletions

View File

@ -786,13 +786,8 @@ gwbuf_get_property(GWBUF *buf, char *name)
return NULL;
}
GWBUF *
gwbuf_make_contiguous(GWBUF *orig)
GWBUF* gwbuf_make_contiguous(GWBUF *orig)
{
GWBUF *newbuf;
uint8_t *ptr;
int len;
if (orig == NULL)
{
ss_info_dassert(!true, "gwbuf_make_contiguous: NULL buffer");
@ -803,20 +798,21 @@ gwbuf_make_contiguous(GWBUF *orig)
return orig;
}
if ((newbuf = gwbuf_alloc(gwbuf_length(orig))) != NULL)
{
newbuf->gwbuf_type = orig->gwbuf_type;
newbuf->hint = hint_dup(orig->hint);
ptr = GWBUF_DATA(newbuf);
GWBUF* newbuf = gwbuf_alloc(gwbuf_length(orig));
MXS_ABORT_IF_NULL(newbuf);
while (orig)
{
len = GWBUF_LENGTH(orig);
memcpy(ptr, GWBUF_DATA(orig), len);
ptr += len;
orig = gwbuf_consume(orig, len);
}
newbuf->gwbuf_type = orig->gwbuf_type;
newbuf->hint = hint_dup(orig->hint);
uint8_t* ptr = GWBUF_DATA(newbuf);
while (orig)
{
int len = GWBUF_LENGTH(orig);
memcpy(ptr, GWBUF_DATA(orig), len);
ptr += len;
orig = gwbuf_consume(orig, len);
}
return newbuf;
}