Skip to content

tool: improve error/warning messages when output filename sanitization fails - #20199

Closed
vszakats wants to merge 3 commits into
curl:masterfrom
vszakats:sanit2
Closed

vszakats wants to merge 3 commits into
curl:masterfrom
vszakats:sanit2

Conversation

@vszakats

@vszakats vszakats commented Jan 6, 2026 •

Copy link
Copy Markdown
Member

On MS-DOS (OOM and bad filename) and Windows (OOM only).

Given the rarity of both platform and error, we make a compromise and
return an unrelated libcurl error (43) in case of a bad output filename
on MS-DOS.

After:

$ CURL_FN_SANITIZE_OOM=1 wine curl.exe https://curl.se/ --output out.txt
curl: (27) Out of memory

$ CURL_FN_SANITIZE_BAD=1 wine curl.exe https://curl.se/ --output out.txt
Warning: bad output filename
curl: (43) A libcurl function was given a bad argument

$ CURL_FN_SANITIZE_OOM=1 wine curl.exe https://curl.se/index.html --globoff -O
curl: (27) Out of memory

$ CURL_FN_SANITIZE_BAD=1 wine curl.exe https://curl.se/index.html --globoff -O
curl: bad output filename
curl: (43) A libcurl function was given a bad argument

Before:

$ CURL_FN_SANITIZE_OOM=1 wine curl.exe https://curl.se/ --output out.txt
Warning: bad output glob
curl: (27) Out of memory

$ CURL_FN_SANITIZE_BAD=1 wine curl.exe https://curl.se/ --output out.txt
Warning: bad output glob
curl: (3) URL using bad/illegal format or missing URL

$ CURL_FN_SANITIZE_OOM=1 wine curl.exe https://curl.se/index.html --globoff -O
curl: Failed to extract a filename from the URL to use for storage
curl: (27) Out of memory

$ CURL_FN_SANITIZE_BAD=1 wine curl.exe https://curl.se/index.html --globoff -O
curl: Failed to extract a filename from the URL to use for storage
curl: (3) URL using bad/illegal format or missing URL

Ref: #20116 (simpler reboot of)
Ref: #20113 #20121
Ref: 40c1748 #20198
Ref: eb7f5b7 #20143
Ref: 8c02407 #20125
Fixes #20044

@vszakats vszakats changed the title tool: improve warning/error messages on filename sanizitation errors tool: improve error/warning messages when output filename sanitization fails Jan 6, 2026
@vszakats
vszakats requested a review from Copilot January 6, 2026 19:37

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR improves error and warning messages when output filename sanitization fails on Windows and MS-DOS platforms. It addresses misleading error messages that incorrectly referred to "output glob" issues when the actual problem was with filename sanitization. The PR propagates sanitization error codes through the call chain to enable more accurate error reporting.

Key Changes:

  • Moved SANITIZEcode enum from tool_doswin.h to tool_sdecls.h for broader accessibility
  • Modified glob_match_url() and get_url_file_name() to accept a SANITIZEcode* output parameter
  • Enhanced error handling in setup_outfile() to distinguish between out-of-memory and invalid path errors, displaying appropriate messages for each case

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/tool_sdecls.h Adds SANITIZEcode enum definition for use across tool files
src/tool_doswin.h Removes SANITIZEcode enum (relocated to tool_sdecls.h)
src/tool_urlglob.h Updates glob_match_url() signature to include SANITIZEcode* parameter
src/tool_urlglob.c Implements sanitization error propagation in glob_match_url()
src/tool_operhlp.h Updates get_url_file_name() signature to include SANITIZEcode* parameter
src/tool_operhlp.c Implements sanitization error propagation in get_url_file_name()
src/tool_operate.c Adds improved error handling logic to display context-appropriate messages based on sanitization error type

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/tool_operate.c Resolved
Comment thread src/tool_operate.c Resolved
@vszakats vszakats added the Windows Windows-specific label Jan 6, 2026
@bagder

bagder commented Jan 6, 2026

Copy link
Copy Markdown
Member

augment review

@augmentcode

augmentcode Bot commented Jan 6, 2026

Copy link
Copy Markdown
🤖 Augment PR Summary

Summary: Improves curl tool diagnostics on Windows/MS-DOS when output filename sanitization fails, avoiding misleading “bad output glob”/URL-format errors.

Changes:

  • Propagates sanitization status via new `SANITIZEcode*` out-parameters in `get_url_file_name()` and `glob_match_url()`.
  • Updates `setup_outfile()` to print “bad output filename” and to map sanitization OOM to `CURLE_OUT_OF_MEMORY`.
  • Moves the `SANITIZEcode` enum to `tool_sdecls.h` for wider reuse across tool modules.

Technical Notes: Sanitization failures now surface as CURLE_BAD_FUNCTION_ARGUMENT (43) where appropriate, aligning the exit code with the underlying cause.

🤖 Was this summary useful? React with 👍 or 👎

@augmentcode augmentcode Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review completed. 1 suggestions posted.

Fix All in Augment

Comment augment review to trigger a new review at any time.

Comment thread src/tool_doswin.h
SANITIZE_ERR_LAST /* never use! */
} SANITIZEcode;

SANITIZEcode sanitize_file_name(char ** const sanitized, const char *file_name,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tool_doswin.h now declares sanitize_file_name() using SANITIZEcode, but no longer defines that type (and this header only includes tool_setup.h). That creates an include-order dependency and looks like it could break compilation for translation units (notably MS-DOS ones) that include tool_doswin.h without having seen SANITIZEcode first.

Fix This in Augment

🤖 Was this useful? React with 👍 or 👎

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice pick. Fix pushed. tool_doswin.h relies on its includer to include tool_cfgable.h first, but in case of tool_doswin.c this was only done for _WIN32.

(Tried to build-test it, but the DJGPP build breaks while compiling libcurl. (for unrelated reasons))

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(also nice knowing Augment works on fork branches now?)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(also nice knowing Augment works on fork branches now?)

Right, I took the liberty of testing that out on your PR as I was just told it started working today!

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fallout is kind of not good: after the uint32_t update, printf masks mismatch, which is for some reason upsetting DJGPP (but nothing in CI). Tried using PRIu32, which'd need inttypes.h. Which is a no-go doing last minute.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(also nice knowing Augment works on fork branches now?)

Right, I took the liberty of testing that out on your PR as I was just told it started working today!

Nice! I'll give it a spin.

@vszakats vszakats Jan 7, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Passes MS-DOS with #20200 applied.

@jay

jay commented Jan 6, 2026

Copy link
Copy Markdown
Member

Couldn't we just do this in two lines? The chances of the sanitizer failing now are limited to a corner case on MS-DOS.

diff --git a/src/tool_operhlp.c b/src/tool_operhlp.c
index 4ad8f84..dde6e65 100644
--- a/src/tool_operhlp.c
+++ b/src/tool_operhlp.c
@@ -225,7 +225,7 @@ CURLcode get_url_file_name(char **filename, const char *url)
         if(sc) {
           if(sc == SANITIZE_ERR_OUT_OF_MEMORY)
             return CURLE_OUT_OF_MEMORY;
-          return CURLE_URL_MALFORMAT;
+          return CURLE_BAD_FUNCTION_ARGUMENT;
         }
         *filename = sanitized;
       }
diff --git a/src/tool_urlglob.c b/src/tool_urlglob.c
index 7a0e153..cbbb850 100644
--- a/src/tool_urlglob.c
+++ b/src/tool_urlglob.c
@@ -707,7 +707,7 @@ CURLcode glob_match_url(char **output, const char *filename,
     if(sc) {
       if(sc == SANITIZE_ERR_OUT_OF_MEMORY)
         return CURLE_OUT_OF_MEMORY;
-      return CURLE_URL_MALFORMAT;
+      return CURLE_BAD_FUNCTION_ARGUMENT;
     }
     *output = sanitized;
     return CURLE_OK;

@vszakats

vszakats commented Jan 6, 2026

Copy link
Copy Markdown
Member Author

Couldn't we just do this in two lines? The chances of the sanitizer failing now are limited to a corner case on MS-DOS.

diff --git a/src/tool_operhlp.c b/src/tool_operhlp.c
index 4ad8f84..dde6e65 100644
--- a/src/tool_operhlp.c
+++ b/src/tool_operhlp.c
@@ -225,7 +225,7 @@ CURLcode get_url_file_name(char **filename, const char *url)
         if(sc) {
           if(sc == SANITIZE_ERR_OUT_OF_MEMORY)
             return CURLE_OUT_OF_MEMORY;
-          return CURLE_URL_MALFORMAT;
+          return CURLE_BAD_FUNCTION_ARGUMENT;
         }
         *filename = sanitized;
       }
diff --git a/src/tool_urlglob.c b/src/tool_urlglob.c
index 7a0e153..cbbb850 100644
--- a/src/tool_urlglob.c
+++ b/src/tool_urlglob.c
@@ -707,7 +707,7 @@ CURLcode glob_match_url(char **output, const char *filename,
     if(sc) {
       if(sc == SANITIZE_ERR_OUT_OF_MEMORY)
         return CURLE_OUT_OF_MEMORY;
-      return CURLE_URL_MALFORMAT;
+      return CURLE_BAD_FUNCTION_ARGUMENT;
     }
     *output = sanitized;
     return CURLE_OK;

This also doesn't fix the misleading messages.

@curl curl deleted a comment from dua6629847-sketch Jan 6, 2026
@testclutch

This comment was marked as outdated.

vszakats added a commit that referenced this pull request Jan 7, 2026
Also adjust a printf mask for signedness.

Fixing with MS-DOS DJGPP gcc 12.2.0:
```
lib/conncache.c:612:22: error: format '%u' expects argument of type 'unsigned int', but argument 4 has type 'uint32_t' {aka 'long unsigned int'}
lib/multi.c:394:22: error: format '%u' expects argument of type 'unsigned int', but argument 3 has type 'uint32_t' {aka 'long unsigned int'}
lib/multi.c:520:20: error: format '%u' expects argument of type 'unsigned int', but argument 3 has type 'uint32_t' {aka 'long unsigned int'}
lib/multi.c:520:20: error: format '%u' expects argument of type 'unsigned int', but argument 5 has type 'uint32_t' {aka 'long unsigned int'}
lib/multi.c:611:20: error: format '%u' expects argument of type 'unsigned int', but argument 3 has type 'uint32_t' {aka 'long unsigned int'}
lib/multi.c:614:22: error: format '%u' expects argument of type 'unsigned int', but argument 3 has type 'uint32_t' {aka 'long unsigned int'}
lib/multi.c:887:20: error: format '%u' expects argument of type 'unsigned int', but argument 3 has type 'uint32_t' {aka 'long unsigned int'}
lib/multi.c:887:20: error: format '%u' expects argument of type 'unsigned int', but argument 5 has type 'uint32_t' {aka 'long unsigned int'}
lib/multi.c:2719:26: error: format '%u' expects argument of type 'unsigned int', but argument 3 has type 'uint32_t' {aka 'long unsigned int'}
lib/multi.c:2725:30: error: format '%u' expects argument of type 'unsigned int', but argument 3 has type 'uint32_t' {aka 'long unsigned int'}
lib/multi.c:2729:28: error: format '%u' expects argument of type 'unsigned int', but argument 3 has type 'uint32_t' {aka 'long unsigned int'}
lib/multi.c:3126:34: error: format '%u' expects argument of type 'unsigned int', but argument 3 has type 'uint32_t' {aka 'long unsigned int'}
lib/multi.c:3348:34: error: format '%u' expects argument of type 'unsigned int', but argument 3 has type 'uint32_t' {aka 'long unsigned int'}
lib/multi.c:3991:28: error: format '%u' expects argument of type 'unsigned int', but argument 3 has type 'uint32_t' {aka 'long unsigned int'}
lib/multi_ev.c:343:24: error: format '%u' expects argument of type 'unsigned int', but argument 6 has type 'uint32_t' {aka 'long unsigned int'}
lib/multi_ev.c:413:24: error: format '%u' expects argument of type 'unsigned int', but argument 4 has type 'uint32_t' {aka 'long unsigned int'}
lib/multi_ev.c:584:36: error: format '%u' expects argument of type 'unsigned int', but argument 3 has type 'uint32_t' {aka 'long unsigned int'}
lib/multi_ntfy.c:113:34: error: format '%d' expects argument of type 'int', but argument 3 has type 'uint32_t' {aka 'long unsigned int'}
lib/multi_ntfy.c:113:34: error: format '%u' expects argument of type 'unsigned int', but argument 4 has type 'uint32_t' {aka 'long unsigned int'}
lib/multi_ntfy.c:171:22: error: format '%u' expects argument of type 'unsigned int', but argument 4 has type 'uint32_t' {aka 'long unsigned int'}
lib/url.c:883:22: error: format '%u' expects argument of type 'unsigned int', but argument 3 has type 'uint32_t' {aka 'long unsigned int'}
lib/url.c:889:22: error: format '%u' expects argument of type 'unsigned int', but argument 3 has type 'uint32_t' {aka 'long unsigned int'}
```

Bug: #20199 (comment)
Follow-up to 4c9e4e9 #20208

Closes #20200
vszakats added a commit that referenced this pull request Jan 7, 2026
In `!ssl` variant.

It's useful to catch `uint32_t` mismatches with `unsigned int` or its
printf mask.

Also add Renovate version bump rule.

It takes about 1m (autotools) + 30s (cmake) in CI.

Bug: #20199 (comment)
Follow-up to 8881a52 #20210
Follow-up to e70436a #20200
Follow-up to 0630e66 #18338

Closes #20204
After:
```
$ CURL_FN_SANITIZE_OOM=1 wine curl.exe https://curl.se/ --output out.txt
curl: (27) Out of memory

$ CURL_FN_SANITIZE_BAD=1 wine curl.exe https://curl.se/ --output out.txt
Warning: bad output filename
curl: (43) A libcurl function was given a bad argument

$ CURL_FN_SANITIZE_OOM=1 wine curl.exe https://curl.se/index.html --globoff -O
curl: (27) Out of memory

$ CURL_FN_SANITIZE_BAD=1 wine curl.exe https://curl.se/index.html --globoff -O
curl: bad output filename
curl: (43) A libcurl function was given a bad argument
```

Before:
```
$ CURL_FN_SANITIZE_OOM=1 wine curl.exe https://curl.se/ --output out.txt
Warning: bad output glob
curl: (27) Out of memory

$ CURL_FN_SANITIZE_BAD=1 wine curl.exe https://curl.se/ --output out.txt
Warning: bad output glob
curl: (3) URL using bad/illegal format or missing URL

$ CURL_FN_SANITIZE_OOM=1 wine curl.exe https://curl.se/index.html --globoff -O
curl: Failed to extract a filename from the URL to use for storage
curl: (27) Out of memory

$ CURL_FN_SANITIZE_BAD=1 wine curl.exe https://curl.se/index.html --globoff -O
curl: Failed to extract a filename from the URL to use for storage
curl: (3) URL using bad/illegal format or missing URL
```

Ref: curl#20116
Comment thread src/tool_sdecls.h
SANITIZE_ERR_BAD_ARGUMENT, /* 2 - bad function parameter */
SANITIZE_ERR_OUT_OF_MEMORY, /* 3 - out of memory */
SANITIZE_ERR_LAST /* never use! */
} SANITIZEcode;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why move the declaration of the SANITIZEcode ? Since it is used for return values for code in tool_doswin.c it seems logical to me to have it declared in tool_doswin.h

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this change it's no longer a DOS/Windows-specific enum, ie any platform may use it or return it and two low-level functions now return this type on all platforms. (Though it will be an OK value on non WinDOS ones). (These are also not inherently WinDOS things.)

Moving it allows to avoid littering the code with tool_doswin includes and/or avoids adding more platform ifdefs, which I thought was the more elegant/simple way to do this. (I find minimizing platform-specific branches and code generally a good thing)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aha. Thanks!

@vszakats vszakats closed this in 2465f7c Jan 10, 2026
@vszakats
vszakats deleted the sanit2 branch January 10, 2026 12:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cmdline tool Windows Windows-specific

Development

Successfully merging this pull request may close these issues.

misleading/wrong error when using long output filename on Windows

5 participants