tool_setopt.c: more OOM handling fixes

This commit is contained in:
Yang Tse 2012-03-17 20:55:15 +01:00
parent ede9ad43fc
commit 804da995c5
1 changed files with 25 additions and 21 deletions

View File

@ -284,6 +284,7 @@ CURLcode tool_setopt_httppost(CURL *curl, struct Configurable *config,
struct curl_httppost *post) struct curl_httppost *post)
{ {
CURLcode ret = CURLE_OK; CURLcode ret = CURLE_OK;
char *escaped = NULL;
bool skip = FALSE; bool skip = FALSE;
ret = curl_easy_setopt(curl, tag, post); ret = curl_easy_setopt(curl, tag, post);
@ -308,40 +309,39 @@ CURLcode tool_setopt_httppost(CURL *curl, struct Configurable *config,
for(pp=p; pp; pp=pp->more) { for(pp=p; pp; pp=pp->more) {
/* May be several files uploaded for one name; /* May be several files uploaded for one name;
* these are linked through the 'more' pointer */ * these are linked through the 'more' pointer */
char *e; Curl_safefree(escaped);
e = c_escape(pp->contents); escaped = c_escape(pp->contents);
if(!e) { if(!escaped) {
ret = CURLE_OUT_OF_MEMORY; ret = CURLE_OUT_OF_MEMORY;
goto nomem; goto nomem;
} }
if(pp->flags & HTTPPOST_FILENAME) { if(pp->flags & HTTPPOST_FILENAME) {
/* file upload as for -F @filename */ /* file upload as for -F @filename */
DATA1(" CURLFORM_FILE, \"%s\",", e); DATA1(" CURLFORM_FILE, \"%s\",", escaped);
} }
else if(pp->flags & HTTPPOST_READFILE) { else if(pp->flags & HTTPPOST_READFILE) {
/* content from file as for -F <filename */ /* content from file as for -F <filename */
DATA1(" CURLFORM_FILECONTENT, \"%s\",", e); DATA1(" CURLFORM_FILECONTENT, \"%s\",", escaped);
} }
else else
DATA1(" CURLFORM_COPYCONTENTS, \"%s\",", e); DATA1(" CURLFORM_COPYCONTENTS, \"%s\",", escaped);
free(e);
if(pp->showfilename) { if(pp->showfilename) {
e = c_escape(pp->showfilename); Curl_safefree(escaped);
if(!e) { escaped = c_escape(pp->showfilename);
if(!escaped) {
ret = CURLE_OUT_OF_MEMORY; ret = CURLE_OUT_OF_MEMORY;
goto nomem; goto nomem;
} }
DATA1(" CURLFORM_FILENAME, \"%s\",", e); DATA1(" CURLFORM_FILENAME, \"%s\",", escaped);
free(e);
} }
if(pp->contenttype) { if(pp->contenttype) {
e = c_escape(pp->contenttype); Curl_safefree(escaped);
if(!e) { escaped = c_escape(pp->contenttype);
if(!escaped) {
ret = CURLE_OUT_OF_MEMORY; ret = CURLE_OUT_OF_MEMORY;
goto nomem; goto nomem;
} }
DATA1(" CURLFORM_CONTENTTYPE, \"%s\",", e); DATA1(" CURLFORM_CONTENTTYPE, \"%s\",", escaped);
free(e);
} }
} }
DATA0(" CURLFORM_END);"); DATA0(" CURLFORM_END);");
@ -350,6 +350,7 @@ CURLcode tool_setopt_httppost(CURL *curl, struct Configurable *config,
} }
nomem: nomem:
Curl_safefree(escaped);
return ret; return ret;
} }
@ -359,6 +360,7 @@ CURLcode tool_setopt_slist(CURL *curl, struct Configurable *config,
struct curl_slist *list) struct curl_slist *list)
{ {
CURLcode ret = CURLE_OK; CURLcode ret = CURLE_OK;
char *escaped = NULL;
bool skip = FALSE; bool skip = FALSE;
ret = curl_easy_setopt(curl, tag, list); ret = curl_easy_setopt(curl, tag, list);
@ -375,18 +377,19 @@ CURLcode tool_setopt_slist(CURL *curl, struct Configurable *config,
CLEAN1("curl_slist_free_all(slist%d);", i); CLEAN1("curl_slist_free_all(slist%d);", i);
CLEAN1("slist%d = NULL;", i); CLEAN1("slist%d = NULL;", i);
for(s=list; s; s=s->next) { for(s=list; s; s=s->next) {
char *e = c_escape(s->data); Curl_safefree(escaped);
if(!e) { escaped = c_escape(s->data);
if(!escaped) {
ret = CURLE_OUT_OF_MEMORY; ret = CURLE_OUT_OF_MEMORY;
goto nomem; goto nomem;
} }
DATA3("slist%d = curl_slist_append(slist%d, \"%s\");", i, i, e); DATA3("slist%d = curl_slist_append(slist%d, \"%s\");", i, i, escaped);
free(e);
} }
CODE2("curl_easy_setopt(hnd, %s, slist%d);", name, i); CODE2("curl_easy_setopt(hnd, %s, slist%d);", name, i);
} }
nomem: nomem:
Curl_safefree(escaped);
return ret; return ret;
} }
@ -401,6 +404,7 @@ CURLcode tool_setopt(CURL *curl, bool str, struct Configurable *config,
bool remark = FALSE; bool remark = FALSE;
bool skip = FALSE; bool skip = FALSE;
bool escape = FALSE; bool escape = FALSE;
char *escaped = NULL;
CURLcode ret = CURLE_OK; CURLcode ret = CURLE_OK;
va_start(arg, tag); va_start(arg, tag);
@ -463,13 +467,12 @@ CURLcode tool_setopt(CURL *curl, bool str, struct Configurable *config,
REM2("%s set to a %s", name, value); REM2("%s set to a %s", name, value);
else { else {
if(escape) { if(escape) {
char *escaped = c_escape(value); escaped = c_escape(value);
if(!escaped) { if(!escaped) {
ret = CURLE_OUT_OF_MEMORY; ret = CURLE_OUT_OF_MEMORY;
goto nomem; goto nomem;
} }
CODE2("curl_easy_setopt(hnd, %s, \"%s\");", name, escaped); CODE2("curl_easy_setopt(hnd, %s, \"%s\");", name, escaped);
free(escaped);
} }
else else
CODE2("curl_easy_setopt(hnd, %s, %s);", name, value); CODE2("curl_easy_setopt(hnd, %s, %s);", name, value);
@ -477,6 +480,7 @@ CURLcode tool_setopt(CURL *curl, bool str, struct Configurable *config,
} }
nomem: nomem:
Curl_safefree(escaped);
return ret; return ret;
} }