James Bursa added better error checking for failer memory calls when
building formposts
This commit is contained in:
parent
8c2ce33c0b
commit
e55dee3807
127
lib/formdata.c
127
lib/formdata.c
@ -735,13 +735,18 @@ CURLFORMcode curl_formadd(struct curl_httppost **httppost,
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
* AddFormData() adds a chunk of data to the FormData linked list.
|
* AddFormData() adds a chunk of data to the FormData linked list.
|
||||||
|
*
|
||||||
|
* size is incremented by the chunk length, unless it is NULL
|
||||||
*/
|
*/
|
||||||
static size_t AddFormData(struct FormData **formp,
|
static CURLcode AddFormData(struct FormData **formp,
|
||||||
const void *line,
|
const void *line,
|
||||||
size_t length)
|
size_t length,
|
||||||
|
size_t *size)
|
||||||
{
|
{
|
||||||
struct FormData *newform = (struct FormData *)
|
struct FormData *newform = (struct FormData *)
|
||||||
malloc(sizeof(struct FormData));
|
malloc(sizeof(struct FormData));
|
||||||
|
if (!newform)
|
||||||
|
return CURLE_OUT_OF_MEMORY;
|
||||||
newform->next = NULL;
|
newform->next = NULL;
|
||||||
|
|
||||||
/* we make it easier for plain strings: */
|
/* we make it easier for plain strings: */
|
||||||
@ -749,6 +754,10 @@ static size_t AddFormData(struct FormData **formp,
|
|||||||
length = strlen((char *)line);
|
length = strlen((char *)line);
|
||||||
|
|
||||||
newform->line = (char *)malloc(length+1);
|
newform->line = (char *)malloc(length+1);
|
||||||
|
if (!newform->line) {
|
||||||
|
free(newform);
|
||||||
|
return CURLE_OUT_OF_MEMORY;
|
||||||
|
}
|
||||||
memcpy(newform->line, line, length);
|
memcpy(newform->line, line, length);
|
||||||
newform->length = length;
|
newform->length = length;
|
||||||
newform->line[length]=0; /* zero terminate for easier debugging */
|
newform->line[length]=0; /* zero terminate for easier debugging */
|
||||||
@ -760,14 +769,17 @@ static size_t AddFormData(struct FormData **formp,
|
|||||||
else
|
else
|
||||||
*formp = newform;
|
*formp = newform;
|
||||||
|
|
||||||
return length;
|
if (size)
|
||||||
|
*size += length;
|
||||||
|
return CURLE_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* AddFormDataf() adds printf()-style formatted data to the formdata chain.
|
* AddFormDataf() adds printf()-style formatted data to the formdata chain.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
static size_t AddFormDataf(struct FormData **formp,
|
static CURLcode AddFormDataf(struct FormData **formp,
|
||||||
|
size_t *size,
|
||||||
const char *fmt, ...)
|
const char *fmt, ...)
|
||||||
{
|
{
|
||||||
char s[4096];
|
char s[4096];
|
||||||
@ -776,7 +788,7 @@ static size_t AddFormDataf(struct FormData **formp,
|
|||||||
vsprintf(s, fmt, ap);
|
vsprintf(s, fmt, ap);
|
||||||
va_end(ap);
|
va_end(ap);
|
||||||
|
|
||||||
return AddFormData(formp, s, 0);
|
return AddFormData(formp, s, 0, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -875,7 +887,7 @@ CURLcode Curl_getFormData(struct FormData **finalform,
|
|||||||
struct curl_httppost *file;
|
struct curl_httppost *file;
|
||||||
CURLcode result = CURLE_OK;
|
CURLcode result = CURLE_OK;
|
||||||
|
|
||||||
curl_off_t size =0;
|
size_t size =0;
|
||||||
char *boundary;
|
char *boundary;
|
||||||
char *fileboundary=NULL;
|
char *fileboundary=NULL;
|
||||||
struct curl_slist* curList;
|
struct curl_slist* curList;
|
||||||
@ -888,28 +900,43 @@ CURLcode Curl_getFormData(struct FormData **finalform,
|
|||||||
boundary = Curl_FormBoundary();
|
boundary = Curl_FormBoundary();
|
||||||
|
|
||||||
/* Make the first line of the output */
|
/* Make the first line of the output */
|
||||||
AddFormDataf(&form,
|
result = AddFormDataf(&form, 0,
|
||||||
"Content-Type: multipart/form-data;"
|
"Content-Type: multipart/form-data;"
|
||||||
" boundary=%s\r\n",
|
" boundary=%s\r\n",
|
||||||
boundary);
|
boundary);
|
||||||
|
if (result != CURLE_OK) {
|
||||||
|
free(boundary);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
/* we DO NOT count that line since that'll be part of the header! */
|
/* we DO NOT count that line since that'll be part of the header! */
|
||||||
|
|
||||||
firstform = form;
|
firstform = form;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
|
|
||||||
if(size)
|
if(size) {
|
||||||
size += AddFormDataf(&form, "\r\n");
|
result = AddFormDataf(&form, &size, "\r\n");
|
||||||
|
if (result != CURLE_OK)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
/* boundary */
|
/* boundary */
|
||||||
size += AddFormDataf(&form, "--%s\r\n", boundary);
|
result = AddFormDataf(&form, &size, "--%s\r\n", boundary);
|
||||||
|
if (result != CURLE_OK)
|
||||||
|
break;
|
||||||
|
|
||||||
size += AddFormData(&form,
|
result = AddFormData(&form,
|
||||||
"Content-Disposition: form-data; name=\"", 0);
|
"Content-Disposition: form-data; name=\"", 0, &size);
|
||||||
|
if (result != CURLE_OK)
|
||||||
|
break;
|
||||||
|
|
||||||
size += AddFormData(&form, post->name, post->namelength);
|
result = AddFormData(&form, post->name, post->namelength, &size);
|
||||||
|
if (result != CURLE_OK)
|
||||||
|
break;
|
||||||
|
|
||||||
size += AddFormData(&form, "\"", 0);
|
size += AddFormData(&form, "\"", 0, &size);
|
||||||
|
if (result != CURLE_OK)
|
||||||
|
break;
|
||||||
|
|
||||||
if(post->more) {
|
if(post->more) {
|
||||||
/* If used, this is a link to more file names, we must then do
|
/* If used, this is a link to more file names, we must then do
|
||||||
@ -917,10 +944,12 @@ CURLcode Curl_getFormData(struct FormData **finalform,
|
|||||||
|
|
||||||
fileboundary = Curl_FormBoundary();
|
fileboundary = Curl_FormBoundary();
|
||||||
|
|
||||||
size += AddFormDataf(&form,
|
result = AddFormDataf(&form, &size,
|
||||||
"\r\nContent-Type: multipart/mixed,"
|
"\r\nContent-Type: multipart/mixed,"
|
||||||
" boundary=%s\r\n",
|
" boundary=%s\r\n",
|
||||||
fileboundary);
|
fileboundary);
|
||||||
|
if (result != CURLE_OK)
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
file = post;
|
file = post;
|
||||||
@ -933,35 +962,48 @@ CURLcode Curl_getFormData(struct FormData **finalform,
|
|||||||
|
|
||||||
if(post->more) {
|
if(post->more) {
|
||||||
/* if multiple-file */
|
/* if multiple-file */
|
||||||
size += AddFormDataf(&form,
|
result = AddFormDataf(&form, &size,
|
||||||
"\r\n--%s\r\nContent-Disposition: "
|
"\r\n--%s\r\nContent-Disposition: "
|
||||||
"attachment; filename=\"%s\"",
|
"attachment; filename=\"%s\"",
|
||||||
fileboundary,
|
fileboundary,
|
||||||
(file->showfilename?file->showfilename:
|
(file->showfilename?file->showfilename:
|
||||||
file->contents));
|
file->contents));
|
||||||
|
if (result != CURLE_OK)
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
else if((post->flags & HTTPPOST_FILENAME) ||
|
else if((post->flags & HTTPPOST_FILENAME) ||
|
||||||
(post->flags & HTTPPOST_BUFFER)) {
|
(post->flags & HTTPPOST_BUFFER)) {
|
||||||
|
|
||||||
size += AddFormDataf(&form,
|
result = AddFormDataf(&form, &size,
|
||||||
"; filename=\"%s\"",
|
"; filename=\"%s\"",
|
||||||
(post->showfilename?post->showfilename:
|
(post->showfilename?post->showfilename:
|
||||||
post->contents));
|
post->contents));
|
||||||
|
if (result != CURLE_OK)
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(file->contenttype) {
|
if(file->contenttype) {
|
||||||
/* we have a specified type */
|
/* we have a specified type */
|
||||||
size += AddFormDataf(&form,
|
result = AddFormDataf(&form, &size,
|
||||||
"\r\nContent-Type: %s",
|
"\r\nContent-Type: %s",
|
||||||
file->contenttype);
|
file->contenttype);
|
||||||
|
if (result != CURLE_OK)
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
curList = file->contentheader;
|
curList = file->contentheader;
|
||||||
while( curList ) {
|
while( curList ) {
|
||||||
/* Process the additional headers specified for this form */
|
/* Process the additional headers specified for this form */
|
||||||
size += AddFormDataf( &form, "\r\n%s", curList->data );
|
result = AddFormDataf( &form, &size, "\r\n%s", curList->data );
|
||||||
|
if (result != CURLE_OK)
|
||||||
|
break;
|
||||||
curList = curList->next;
|
curList = curList->next;
|
||||||
}
|
}
|
||||||
|
if (result != CURLE_OK) {
|
||||||
|
Curl_formclean(firstform);
|
||||||
|
free(boundary);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
#if 0
|
#if 0
|
||||||
/* The header Content-Transfer-Encoding: seems to confuse some receivers
|
/* The header Content-Transfer-Encoding: seems to confuse some receivers
|
||||||
@ -977,7 +1019,9 @@ CURLcode Curl_getFormData(struct FormData **finalform,
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
size += AddFormData(&form, "\r\n\r\n", 0);
|
result = AddFormData(&form, "\r\n\r\n", 0, &size);
|
||||||
|
if (result != CURLE_OK)
|
||||||
|
break;
|
||||||
|
|
||||||
if((post->flags & HTTPPOST_FILENAME) ||
|
if((post->flags & HTTPPOST_FILENAME) ||
|
||||||
(post->flags & HTTPPOST_READFILE)) {
|
(post->flags & HTTPPOST_READFILE)) {
|
||||||
@ -995,8 +1039,16 @@ CURLcode Curl_getFormData(struct FormData **finalform,
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
if(fileread) {
|
if(fileread) {
|
||||||
while((nread = fread(buffer, 1, 1024, fileread)))
|
while((nread = fread(buffer, 1, 1024, fileread))) {
|
||||||
size += AddFormData(&form, buffer, nread);
|
result = AddFormData(&form, buffer, nread, &size);
|
||||||
|
if (result != CURLE_OK)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (result != CURLE_OK) {
|
||||||
|
Curl_formclean(firstform);
|
||||||
|
free(boundary);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
if(fileread != stdin)
|
if(fileread != stdin)
|
||||||
fclose(fileread);
|
fclose(fileread);
|
||||||
@ -1011,30 +1063,53 @@ CURLcode Curl_getFormData(struct FormData **finalform,
|
|||||||
}
|
}
|
||||||
else if (post->flags & HTTPPOST_BUFFER) {
|
else if (post->flags & HTTPPOST_BUFFER) {
|
||||||
/* include contents of buffer */
|
/* include contents of buffer */
|
||||||
size += AddFormData(&form, post->buffer, post->bufferlength);
|
result = AddFormData(&form, post->buffer, post->bufferlength,
|
||||||
|
&size);
|
||||||
|
if (result != CURLE_OK)
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
else {
|
else {
|
||||||
/* include the contents we got */
|
/* include the contents we got */
|
||||||
size += AddFormData(&form, post->contents, post->contentslength);
|
result = AddFormData(&form, post->contents, post->contentslength,
|
||||||
|
&size);
|
||||||
|
if (result != CURLE_OK)
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
} while((file = file->more)); /* for each specified file for this field */
|
} while((file = file->more)); /* for each specified file for this field */
|
||||||
|
if (result != CURLE_OK) {
|
||||||
|
Curl_formclean(firstform);
|
||||||
|
free(boundary);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
if(post->more) {
|
if(post->more) {
|
||||||
/* this was a multiple-file inclusion, make a termination file
|
/* this was a multiple-file inclusion, make a termination file
|
||||||
boundary: */
|
boundary: */
|
||||||
size += AddFormDataf(&form,
|
result = AddFormDataf(&form, &size,
|
||||||
"\r\n--%s--",
|
"\r\n--%s--",
|
||||||
fileboundary);
|
fileboundary);
|
||||||
free(fileboundary);
|
free(fileboundary);
|
||||||
|
if (result != CURLE_OK)
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
} while((post=post->next)); /* for each field */
|
} while((post=post->next)); /* for each field */
|
||||||
|
if (result != CURLE_OK) {
|
||||||
|
Curl_formclean(firstform);
|
||||||
|
free(boundary);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
/* end-boundary for everything */
|
/* end-boundary for everything */
|
||||||
size += AddFormDataf(&form,
|
result = AddFormDataf(&form, &size,
|
||||||
"\r\n--%s--\r\n",
|
"\r\n--%s--\r\n",
|
||||||
boundary);
|
boundary);
|
||||||
|
if (result != CURLE_OK) {
|
||||||
|
Curl_formclean(firstform);
|
||||||
|
free(boundary);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
*sizep = size;
|
*sizep = size;
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user