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

@ -368,16 +368,13 @@ extern char *gwbuf_get_property(GWBUF *buf, char *name);
/** /**
* Convert a chain of GWBUF structures into a single GWBUF structure * 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, * @return A contiguous version of @c buf.
* @c buf if @c buf already is contiguous, and otherwise
* a contigious copy of @c buf.
* *
* @attention If a non-NULL value is returned, the @c buf should no * @attention Never returns NULL, memory allocation failures abort the process
* longer be used as it may have been freed.
*/ */
extern GWBUF *gwbuf_make_contiguous(GWBUF *buf); extern GWBUF* gwbuf_make_contiguous(GWBUF *buf);
/** /**
* Add a buffer object to GWBUF buffer. * Add a buffer object to GWBUF buffer.

View File

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