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
No known key found for this signature in database
GPG Key ID: 72D48FCE664F7B19
2 changed files with 18 additions and 25 deletions

View File

@ -368,16 +368,13 @@ extern char *gwbuf_get_property(GWBUF *buf, char *name);
/**
* Convert a chain of GWBUF structures into a single GWBUF structure
*
* @param orig The chain to convert
* @param orig The chain to convert, must not be used after the function call
*
* @return NULL if @c buf is NULL or if a memory allocation fails,
* @c buf if @c buf already is contiguous, and otherwise
* a contigious copy of @c buf.
* @return A contiguous version of @c buf.
*
* @attention If a non-NULL value is returned, the @c buf should no
* longer be used as it may have been freed.
* @attention Never returns NULL, memory allocation failures abort the process
*/
extern GWBUF *gwbuf_make_contiguous(GWBUF *buf);
extern GWBUF* gwbuf_make_contiguous(GWBUF *buf);
/**
* Add a buffer object to GWBUF buffer.

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;
}