Skip to content

Commit

Permalink
xmlrpc: use reallocxf() to free old pointer in case of failure
Browse files Browse the repository at this point in the history
  • Loading branch information
miconda committed Aug 10, 2017
1 parent 49217fb commit e02990b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
5 changes: 5 additions & 0 deletions src/modules/xmlrpc/http.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,16 @@
#include <stdlib.h>
#define mxr_malloc malloc
#define mxr_realloc realloc
#define mxr_reallocxf(p, s) \
( { void *____v123; ____v123=realloc((p), (s)); \
if(!____v123 && (p)) free(p); \
____v123; } )
#define mxr_free free
#else
#include "../../core/mem/mem.h"
#define mxr_malloc pkg_malloc
#define mxr_realloc pkg_realloc
#define mxr_reallocxf pkg_reallocxf
#define mxr_free pkg_free
#endif

Expand Down
14 changes: 6 additions & 8 deletions src/modules/xmlrpc/xmlrpc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1644,12 +1644,12 @@ static int rpc_rpl_printf(rpc_ctx_t* ctx, char* fmt, ...)
if (n > -1 && n < buf_size) {
s.s = buf;
s.len = n;
if (ctx->flags & RET_ARRAY &&
if (ctx->flags & RET_ARRAY &&
add_xmlrpc_reply(reply, &value_prefix) < 0) goto err;
if (add_xmlrpc_reply(reply, &string_prefix) < 0) goto err;
if (add_xmlrpc_reply_esc(reply, &s) < 0) goto err;
if (add_xmlrpc_reply(reply, &string_suffix) < 0) goto err;
if (ctx->flags & RET_ARRAY &&
if (ctx->flags & RET_ARRAY &&
add_xmlrpc_reply(reply, &value_suffix) < 0) goto err;
if (add_xmlrpc_reply(reply, &lf) < 0) goto err;
mxr_free(buf);
Expand All @@ -1661,7 +1661,7 @@ static int rpc_rpl_printf(rpc_ctx_t* ctx, char* fmt, ...)
} else { /* glibc 2.0 */
buf_size *= 2; /* twice the old size */
}
if ((buf = mxr_realloc(buf, buf_size)) == 0) {
if ((buf = mxr_reallocxf(buf, buf_size)) == 0) {
set_fault(reply, 500, "Internal Server Error (No memory left)");
ERR("No memory left\n");
goto err;
Expand Down Expand Up @@ -1836,12 +1836,11 @@ static int rpc_array_add(struct rpc_struct* s, char* fmt, ...)

/** Create a new member from formatting string and add it to a structure.
*/
static int rpc_struct_printf(struct rpc_struct* s, char* member_name,
static int rpc_struct_printf(struct rpc_struct* s, char* member_name,
char* fmt, ...)
{
int n, buf_size;
char* buf;
char* buf0;
va_list ap;
str st, name;
struct xmlrpc_reply* reply;
Expand Down Expand Up @@ -1891,12 +1890,11 @@ static int rpc_struct_printf(struct rpc_struct* s, char* member_name,
} else { /* glibc 2.0 */
buf_size *= 2; /* twice the old size */
}
if ((buf0 = mxr_realloc(buf, buf_size)) == 0) {
if ((buf = mxr_reallocxf(buf, buf_size)) == 0) {
set_fault(reply, 500, "Internal Server Error (No memory left)");
ERR("No memory left\n");
goto err;
}
buf = buf0;
}
return 0;
err:
Expand Down Expand Up @@ -2673,4 +2671,4 @@ int mod_register(char *path, int *dlflags, void *p1, void *p2)
return 0;
}

/** @} */
/** @} */

0 comments on commit e02990b

Please sign in to comment.