tool_formparse: unroll the NULL_CHECK and CONST_FREE macros

To make the code read more obvious

Assisted-by: Jay Satiro

Closes #9710
This commit is contained in:
Daniel Stenberg 2022-10-12 15:48:52 +02:00
parent d91f01f231
commit 8c452b4003
No known key found for this signature in database
GPG Key ID: 5CC908FDB71E12C2
2 changed files with 72 additions and 78 deletions

View File

@ -38,10 +38,6 @@
#include "memdebug.h" /* keep this as LAST include */ #include "memdebug.h" /* keep this as LAST include */
/* Macros to free const pointers. */
#define CONST_FREE(x) free((void *) (x))
#define CONST_SAFEFREE(x) Curl_safefree(*((void **) &(x)))
/* tool_mime functions. */ /* tool_mime functions. */
static struct tool_mime *tool_mime_new(struct tool_mime *parent, static struct tool_mime *tool_mime_new(struct tool_mime *parent,
toolmimekind kind) toolmimekind kind)
@ -65,7 +61,7 @@ static struct tool_mime *tool_mime_new_parts(struct tool_mime *parent)
} }
static struct tool_mime *tool_mime_new_data(struct tool_mime *parent, static struct tool_mime *tool_mime_new_data(struct tool_mime *parent,
const char *data) char *data)
{ {
struct tool_mime *m = NULL; struct tool_mime *m = NULL;
@ -73,7 +69,7 @@ static struct tool_mime *tool_mime_new_data(struct tool_mime *parent,
if(data) { if(data) {
m = tool_mime_new(parent, TOOLMIME_DATA); m = tool_mime_new(parent, TOOLMIME_DATA);
if(!m) if(!m)
CONST_FREE(data); free(data);
else else
m->data = data; m->data = data;
} }
@ -91,13 +87,13 @@ static struct tool_mime *tool_mime_new_filedata(struct tool_mime *parent,
*errcode = CURLE_OUT_OF_MEMORY; *errcode = CURLE_OUT_OF_MEMORY;
if(strcmp(filename, "-")) { if(strcmp(filename, "-")) {
/* This is a normal file. */ /* This is a normal file. */
filename = strdup(filename); char *filedup = strdup(filename);
if(filename) { if(filedup) {
m = tool_mime_new(parent, TOOLMIME_FILE); m = tool_mime_new(parent, TOOLMIME_FILE);
if(!m) if(!m)
CONST_FREE(filename); free(filedup);
else { else {
m->data = filename; m->data = filedup;
if(!isremotefile) if(!isremotefile)
m->kind = TOOLMIME_FILEDATA; m->kind = TOOLMIME_FILEDATA;
*errcode = CURLE_OK; *errcode = CURLE_OK;
@ -168,11 +164,11 @@ void tool_mime_free(struct tool_mime *mime)
tool_mime_free(mime->subparts); tool_mime_free(mime->subparts);
if(mime->prev) if(mime->prev)
tool_mime_free(mime->prev); tool_mime_free(mime->prev);
CONST_SAFEFREE(mime->name); Curl_safefree(mime->name);
CONST_SAFEFREE(mime->filename); Curl_safefree(mime->filename);
CONST_SAFEFREE(mime->type); Curl_safefree(mime->type);
CONST_SAFEFREE(mime->encoder); Curl_safefree(mime->encoder);
CONST_SAFEFREE(mime->data); Curl_safefree(mime->data);
curl_slist_free_all(mime->headers); curl_slist_free_all(mime->headers);
free(mime); free(mime);
} }
@ -709,24 +705,15 @@ static int get_param_part(struct OperationConfig *config, char endchar,
* *
***************************************************************************/ ***************************************************************************/
/* Convenience macros for null pointer check. */ #define SET_TOOL_MIME_PTR(m, field) \
#define NULL_CHECK(ptr, init, retcode) \
do { \ do { \
(ptr) = (init); \ if(field) { \
if(!(ptr)) { \ (m)->field = strdup(field); \
warnf(config->global, "out of memory!\n"); \ if(!(m)->field) \
curl_slist_free_all(headers); \ goto fail; \
Curl_safefree(contents); \
return retcode; \
} \ } \
} while(0) } while(0)
#define SET_TOOL_MIME_PTR(m, field, retcode) \
do { \
if(field) \
NULL_CHECK((m)->field, strdup(field), retcode); \
} while(0)
int formparse(struct OperationConfig *config, int formparse(struct OperationConfig *config,
const char *input, const char *input,
struct tool_mime **mimeroot, struct tool_mime **mimeroot,
@ -745,15 +732,20 @@ int formparse(struct OperationConfig *config,
struct curl_slist *headers = NULL; struct curl_slist *headers = NULL;
struct tool_mime *part = NULL; struct tool_mime *part = NULL;
CURLcode res; CURLcode res;
int err = 1;
/* Allocate the main mime structure if needed. */ /* Allocate the main mime structure if needed. */
if(!*mimecurrent) { if(!*mimecurrent) {
NULL_CHECK(*mimeroot, tool_mime_new_parts(NULL), 1); *mimeroot = tool_mime_new_parts(NULL);
if(!*mimeroot)
goto fail;
*mimecurrent = *mimeroot; *mimecurrent = *mimeroot;
} }
/* Make a copy we can overwrite. */ /* Make a copy we can overwrite. */
NULL_CHECK(contents, strdup(input), 2); contents = strdup(input);
if(!contents)
goto fail;
/* Scan for the end of the name. */ /* Scan for the end of the name. */
contp = strchr(contents, '='); contp = strchr(contents, '=');
@ -767,23 +759,22 @@ int formparse(struct OperationConfig *config,
/* Starting a multipart. */ /* Starting a multipart. */
sep = get_param_part(config, '\0', sep = get_param_part(config, '\0',
&contp, &data, &type, NULL, NULL, &headers); &contp, &data, &type, NULL, NULL, &headers);
if(sep < 0) { if(sep < 0)
Curl_safefree(contents); goto fail;
return 3; part = tool_mime_new_parts(*mimecurrent);
} if(!part)
NULL_CHECK(part, tool_mime_new_parts(*mimecurrent), 4); goto fail;
*mimecurrent = part; *mimecurrent = part;
part->headers = headers; part->headers = headers;
headers = NULL; headers = NULL;
SET_TOOL_MIME_PTR(part, type, 5); SET_TOOL_MIME_PTR(part, type);
} }
else if(!name && !strcmp(contp, ")") && !literal_value) { else if(!name && !strcmp(contp, ")") && !literal_value) {
/* Ending a multipart. */ /* Ending a multipart. */
if(*mimecurrent == *mimeroot) { if(*mimecurrent == *mimeroot) {
warnf(config->global, "no multipart to terminate!\n"); warnf(config->global, "no multipart to terminate!\n");
Curl_safefree(contents); goto fail;
return 6; }
}
*mimecurrent = (*mimecurrent)->parent; *mimecurrent = (*mimecurrent)->parent;
} }
else if('@' == contp[0] && !literal_value) { else if('@' == contp[0] && !literal_value) {
@ -799,8 +790,7 @@ int formparse(struct OperationConfig *config,
sep = get_param_part(config, ',', &contp, sep = get_param_part(config, ',', &contp,
&data, &type, &filename, &encoder, &headers); &data, &type, &filename, &encoder, &headers);
if(sep < 0) { if(sep < 0) {
Curl_safefree(contents); goto fail;
return 7;
} }
/* now contp point to comma or string end. /* now contp point to comma or string end.
@ -808,13 +798,17 @@ int formparse(struct OperationConfig *config,
if(!subparts) { if(!subparts) {
if(sep != ',') /* If there is a single file. */ if(sep != ',') /* If there is a single file. */
subparts = *mimecurrent; subparts = *mimecurrent;
else else {
NULL_CHECK(subparts, tool_mime_new_parts(*mimecurrent), 8); subparts = tool_mime_new_parts(*mimecurrent);
if(!subparts)
goto fail;
}
} }
/* Store that file in a part. */ /* Store that file in a part. */
NULL_CHECK(part, part = tool_mime_new_filedata(subparts, data, TRUE, &res);
tool_mime_new_filedata(subparts, data, TRUE, &res), 9); if(!part)
goto fail;
part->headers = headers; part->headers = headers;
headers = NULL; headers = NULL;
part->config = config->global; part->config = config->global;
@ -825,17 +819,16 @@ int formparse(struct OperationConfig *config,
if(part->size > 0) { if(part->size > 0) {
warnf(config->global, warnf(config->global,
"error while reading standard input\n"); "error while reading standard input\n");
Curl_safefree(contents); goto fail;
return 10;
} }
CONST_SAFEFREE(part->data); Curl_safefree(part->data);
part->data = NULL; part->data = NULL;
part->size = -1; part->size = -1;
res = CURLE_OK; res = CURLE_OK;
} }
SET_TOOL_MIME_PTR(part, filename, 11); SET_TOOL_MIME_PTR(part, filename);
SET_TOOL_MIME_PTR(part, type, 12); SET_TOOL_MIME_PTR(part, type);
SET_TOOL_MIME_PTR(part, encoder, 13); SET_TOOL_MIME_PTR(part, encoder);
/* *contp could be '\0', so we just check with the delimiter */ /* *contp could be '\0', so we just check with the delimiter */
} while(sep); /* loop if there's another file name */ } while(sep); /* loop if there's another file name */
@ -846,13 +839,13 @@ int formparse(struct OperationConfig *config,
++contp; ++contp;
sep = get_param_part(config, '\0', &contp, sep = get_param_part(config, '\0', &contp,
&data, &type, NULL, &encoder, &headers); &data, &type, NULL, &encoder, &headers);
if(sep < 0) { if(sep < 0)
Curl_safefree(contents); goto fail;
return 14;
}
NULL_CHECK(part, tool_mime_new_filedata(*mimecurrent, data, FALSE, part = tool_mime_new_filedata(*mimecurrent, data, FALSE,
&res), 15); &res);
if(!part)
goto fail;
part->headers = headers; part->headers = headers;
headers = NULL; headers = NULL;
part->config = config->global; part->config = config->global;
@ -863,10 +856,9 @@ int formparse(struct OperationConfig *config,
if(part->size > 0) { if(part->size > 0) {
warnf(config->global, warnf(config->global,
"error while reading standard input\n"); "error while reading standard input\n");
Curl_safefree(contents); goto fail;
return 16;
} }
CONST_SAFEFREE(part->data); Curl_safefree(part->data);
part->data = NULL; part->data = NULL;
part->size = -1; part->size = -1;
res = CURLE_OK; res = CURLE_OK;
@ -878,20 +870,20 @@ int formparse(struct OperationConfig *config,
else { else {
sep = get_param_part(config, '\0', &contp, sep = get_param_part(config, '\0', &contp,
&data, &type, &filename, &encoder, &headers); &data, &type, &filename, &encoder, &headers);
if(sep < 0) { if(sep < 0)
Curl_safefree(contents); goto fail;
return 17;
}
} }
NULL_CHECK(part, tool_mime_new_data(*mimecurrent, data), 18); part = tool_mime_new_data(*mimecurrent, data);
if(!part)
goto fail;
part->headers = headers; part->headers = headers;
headers = NULL; headers = NULL;
} }
SET_TOOL_MIME_PTR(part, filename, 19); SET_TOOL_MIME_PTR(part, filename);
SET_TOOL_MIME_PTR(part, type, 20); SET_TOOL_MIME_PTR(part, type);
SET_TOOL_MIME_PTR(part, encoder, 21); SET_TOOL_MIME_PTR(part, encoder);
if(sep) { if(sep) {
*contp = (char) sep; *contp = (char) sep;
@ -901,13 +893,15 @@ int formparse(struct OperationConfig *config,
} }
/* Set part name. */ /* Set part name. */
SET_TOOL_MIME_PTR(part, name, 22); SET_TOOL_MIME_PTR(part, name);
} }
else { else {
warnf(config->global, "Illegally formatted input field!\n"); warnf(config->global, "Illegally formatted input field!\n");
Curl_safefree(contents); goto fail;
return 23;
} }
err = 0;
fail:
Curl_safefree(contents); Curl_safefree(contents);
return 0; curl_slist_free_all(headers);
return err;
} }

View File

@ -43,11 +43,11 @@ struct tool_mime {
struct tool_mime *parent; /* Parent item. */ struct tool_mime *parent; /* Parent item. */
struct tool_mime *prev; /* Previous sibling (reverse order link). */ struct tool_mime *prev; /* Previous sibling (reverse order link). */
/* Common fields. */ /* Common fields. */
const char *data; /* Actual data or data filename. */ char *data; /* Actual data or data filename. */
const char *name; /* Part name. */ char *name; /* Part name. */
const char *filename; /* Part's filename. */ char *filename; /* Part's filename. */
const char *type; /* Part's mime type. */ char *type; /* Part's mime type. */
const char *encoder; /* Part's requested encoding. */ char *encoder; /* Part's requested encoding. */
struct curl_slist *headers; /* User-defined headers. */ struct curl_slist *headers; /* User-defined headers. */
/* TOOLMIME_PARTS fields. */ /* TOOLMIME_PARTS fields. */
struct tool_mime *subparts; /* Part's subparts. */ struct tool_mime *subparts; /* Part's subparts. */