From 8b1e5df73decb3920ca5476710ee89e1495bb760 Mon Sep 17 00:00:00 2001 From: Emil Engler Date: Sun, 11 Dec 2022 18:08:17 +0100 Subject: [PATCH] tool: determine the correct fopen option for -D This commit fixes a bug in the dump-header feature regarding the determination of the second fopen(3) option. Reported-by: u20221022 on github See #4753 See #4762 Fixes #10074 Closes #10079 --- src/tool_operate.c | 16 ++++++++++++++- tests/data/Makefile.inc | 2 +- tests/data/test3029 | 39 +++++++++++++++++++++++++++++++++++++ tests/data/test3030 | 43 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 98 insertions(+), 2 deletions(-) create mode 100644 tests/data/test3029 create mode 100644 tests/data/test3030 diff --git a/src/tool_operate.c b/src/tool_operate.c index 7af73a266c..79db063a50 100644 --- a/src/tool_operate.c +++ b/src/tool_operate.c @@ -969,7 +969,21 @@ static CURLcode single_transfer(struct GlobalConfig *global, /* open file for output: */ if(strcmp(config->headerfile, "-")) { FILE *newfile; - newfile = fopen(config->headerfile, per->prev == NULL?"wb":"ab"); + + /* + * this checks if the previous transfer had the same + * OperationConfig, which would mean, that the an output file has + * already been created and data can be appened to it, instead + * of overwriting it. + * TODO: Consider placing the file handle inside the + * OperationConfig, so that it does not need to be opened/closed + * for every transfer. + */ + if(per->prev && per->prev->config == config) + newfile = fopen(config->headerfile, "ab+"); + else + newfile = fopen(config->headerfile, "wb+"); + if(!newfile) { warnf(global, "Failed to open %s\n", config->headerfile); result = CURLE_WRITE_ERROR; diff --git a/tests/data/Makefile.inc b/tests/data/Makefile.inc index d7194376eb..e662dd1cdc 100644 --- a/tests/data/Makefile.inc +++ b/tests/data/Makefile.inc @@ -248,7 +248,7 @@ test2500 \ test3000 test3001 test3002 test3003 test3004 test3005 test3006 test3007 \ test3008 test3009 test3010 test3011 test3012 test3013 test3014 test3015 \ test3016 test3017 test3018 test3019 test3020 test3021 test3022 test3023 \ -test3024 test3025 test3026 test3027 test3028 \ +test3024 test3025 test3026 test3027 test3028 test3029 test3030 \ \ test3100 test3101 \ test3200 diff --git a/tests/data/test3029 b/tests/data/test3029 new file mode 100644 index 0000000000..a5bc14be45 --- /dev/null +++ b/tests/data/test3029 @@ -0,0 +1,39 @@ + + + +HTTP + + + + + +HTTP/1.1 200 OK +Date: Tue, 09 Nov 2010 14:49:00 GMT +Content-Length: 6 + +-foo- + + + + + +http + + +HTTP with multiple -D + + +-D log/heads%TESTNUMBER http://%HOSTIP:%HTTPPORT/%TESTNUMBER http://%HOSTIP:%HTTPPORT/%TESTNUMBER --next -D log/heads%TESTNUMBER http://%HOSTIP:%HTTPPORT/%TESTNUMBER + + + + + +HTTP/1.1 200 OK +Date: Tue, 09 Nov 2010 14:49:00 GMT +Content-Length: 6 + + + + + \ No newline at end of file diff --git a/tests/data/test3030 b/tests/data/test3030 new file mode 100644 index 0000000000..b4d7ee38db --- /dev/null +++ b/tests/data/test3030 @@ -0,0 +1,43 @@ + + + +HTTP + + + + + +HTTP/1.1 200 OK +Date: Tue, 09 Nov 2010 14:49:00 GMT +Content-Length: 6 + +-foo- + + + + + +http + + +HTTP with multiple transfers in one -D + + +-D log/heads%TESTNUMBER http://%HOSTIP:%HTTPPORT/%TESTNUMBER http://%HOSTIP:%HTTPPORT/%TESTNUMBER + + + + + +HTTP/1.1 200 OK +Date: Tue, 09 Nov 2010 14:49:00 GMT +Content-Length: 6 + +HTTP/1.1 200 OK +Date: Tue, 09 Nov 2010 14:49:00 GMT +Content-Length: 6 + + + + + \ No newline at end of file -- 2.47.3