Skip to content

urlapi: do not pass a NULL path to printf for file URLs - #23585

Closed
Ar-maan05 wants to merge 1 commit into
curl:masterfrom
Ar-maan05:urlapi-file-null-path
Closed

Ar-maan05 wants to merge 1 commit into
curl:masterfrom
Ar-maan05:urlapi-file-null-path

Conversation

@Ar-maan05

Copy link
Copy Markdown
Contributor

Getting the full URL from a handle that only has the scheme set to "file" prints a NULL pointer:

CURLU *u = curl_url();
curl_url_set(u, CURLUPART_SCHEME, "file", 0);
curl_url_get(u, CURLUPART_URL, &p, 0);
before: file://(nil)
after:  file:///

file_url() passes u->path straight to %s, and it is NULL when no path was set. Getting CURLUPART_PATH on the same handle already returns "/", and the non-file branch of urlget_url() uses u->path ? u->path : "/", so this does the same for file URLs. On non-glibc libcs this is undefined behavior rather than a funny string.

This is the "(nil)" cleanup bagder mentioned in #21743. I added a case to the set_parts list in lib1560.c (fails before, passes after) and the other URL tests still pass locally.

I found this with an AI-assisted code review and then checked it by hand on a local build.

A CURLU handle with only the scheme set to "file" has no path. Getting
CURLUPART_PATH returns "/" for it, but getting CURLUPART_URL passed the
NULL path to the %s in file_url(), which gives "file://(nil)" on glibc
and is undefined behavior elsewhere.

Use "/" when there is no path, like the non-file branch already does.

Extend test 1560 to verify.
@github-actions github-actions Bot added the tests label Oct 24, 2026

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.

🟢 Approval recommended

The narrow fallback preserves existing paths and includes a regression case covering the reported failure.

No open issues 🎉

What changed in this PR

This PR fixes file URL serialization when a URL handle has no path, addressing the NULL-path output reported in #21743 without changing existing paths.

Changes:

  • Use / instead of passing a NULL path to the file URL formatter.
  • Add a regression case verifying that a handle with only the file scheme returns file:///.
File Description
tests/​libtest/​lib1560.c Adds coverage for a file URL handle with no path set.
lib/​urlapi.c Defaults a missing file URL path to / during serialization.

Review effort: Balanced

@bagder bagder closed this in d611ca7 Oct 24, 2026
@bagder

bagder commented Oct 24, 2026

Copy link
Copy Markdown
Member

Thanks!

dkarpov1970 pushed a commit to dkarpov1970/curl that referenced this pull request Nov 18, 2026
A CURLU handle with only the scheme set to "file" has no path. Getting
CURLUPART_PATH returns "/" for it, but getting CURLUPART_URL passed the
NULL path to the %s in file_url(), which gives "file://(nil)" on glibc
and is undefined behavior elsewhere.

Use "/" when there is no path, like the non-file branch already does.

Extend test 1560 to verify.

Closes curl#23585

Backport notes:

- The release builds file:// URLs inline in urlget_url(); applied the
  NULL path check there.

(cherry picked from commit d611ca7)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Development

Successfully merging this pull request may close these issues.

3 participants