Skip to content

Commit

Permalink
curl: writeout: fix repeated header outputs
Browse files Browse the repository at this point in the history
The function stored a terminating zero into the buffer for convenience,
but when on repeated calls that would cause problems. Starting now, the
passed in buffer is not modified.

Reported-by: highmtworks on github
Fixes curl#9150
Closes curl#9152
  • Loading branch information
bagder committed Jul 17, 2022
1 parent a88dbe4 commit 0ef4f08
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions src/tool_writeout.c
Original file line number Diff line number Diff line change
Expand Up @@ -338,8 +338,8 @@ void ourWriteOut(const char *writeinfo, struct per_transfer *per,
else {
/* this is meant as a variable to output */
char *end;
size_t vlen;
if('{' == ptr[1]) {
char keepit;
int i;
bool match = FALSE;
end = strchr(ptr, '}');
Expand All @@ -348,10 +348,10 @@ void ourWriteOut(const char *writeinfo, struct per_transfer *per,
fputs("%{", stream);
continue;
}
keepit = *end;
*end = 0; /* null-terminate */
vlen = end - ptr;
for(i = 0; variables[i].name; i++) {
if(curl_strequal(ptr, variables[i].name)) {
if((strlen(variables[i].name) == vlen) &&
curl_strnequal(ptr, variables[i].name, vlen)) {
match = TRUE;
switch(variables[i].id) {
case VAR_ONERROR:
Expand Down Expand Up @@ -380,21 +380,26 @@ void ourWriteOut(const char *writeinfo, struct per_transfer *per,
}
}
if(!match) {
fprintf(stderr, "curl: unknown --write-out variable: '%s'\n", ptr);
fprintf(stderr, "curl: unknown --write-out variable: '%.*s'\n",
(int)vlen, ptr);
}
ptr = end + 1; /* pass the end */
*end = keepit;
}
else if(!strncmp("header{", &ptr[1], 7)) {
ptr += 8;
end = strchr(ptr, '}');
if(end) {
char hname[256]; /* holds the longest header field name */
struct curl_header *header;
*end = 0;
if(CURLHE_OK == curl_easy_header(per->curl, ptr, 0, CURLH_HEADER,
-1, &header))
fputs(header->value, stream);
ptr = end + 1; /* pass the end */
vlen = end - ptr;
if(vlen < sizeof(hname)) {
memcpy(hname, ptr, vlen);
hname[vlen] = 0;
if(CURLHE_OK == curl_easy_header(per->curl, hname, 0,
CURLH_HEADER, -1, &header))
fputs(header->value, stream);
}
ptr = end + 1;
}
else
fputs("%header{", stream);
Expand Down

0 comments on commit 0ef4f08

Please sign in to comment.