tool_cb_wrt: fix no-clobber error handling

When saving a file with --no-clobber, make sure the existing file name
remains set when creating the name fails. In a retry scenario, it comes
back and uses that variable again.

Add test 3036 to verify.

Reported-by: James Fuller
Closes #20939
This commit is contained in:
Daniel Stenberg 2026-03-16 14:51:52 +01:00
parent 3525ed9b2f
commit 29cb750fc2
No known key found for this signature in database
GPG key ID: 5CC908FDB71E12C2
3 changed files with 69 additions and 3 deletions

View file

@ -61,6 +61,7 @@ bool tool_create_output_file(struct OutStruct *outs,
if(config->file_clobber_mode == CLOBBER_NEVER && fd == -1) {
int next_num = 1;
struct dynbuf fbuffer;
char *newfile;
curlx_dyn_init(&fbuffer, 1025);
/* !checksrc! disable ERRNOVAR 1 */
while(fd == -1 && /* have not successfully opened a file */
@ -78,8 +79,11 @@ bool tool_create_output_file(struct OutStruct *outs,
/* Keep retrying in the hope that it is not interrupted sometime */
} while(fd == -1 && errno == EINTR);
}
outs->filename = curlx_dyn_ptr(&fbuffer); /* remember the new one */
outs->alloc_filename = TRUE;
newfile = curlx_dyn_ptr(&fbuffer); /* remember the new one */
if(newfile) {
outs->filename = newfile;
outs->alloc_filename = TRUE;
}
}
/* An else statement to not overwrite existing files and not retry with
new numbered names (which would cover