Skip to content

Conversation

@vszakats
Copy link
Member

@vszakats vszakats commented Jul 7, 2025

This callback was permanently mapped to libcurl's internal
Curl_wcsdup(), which always uses the customizable malloc for
allocation, thus making a custom mapping redundant anyway.

To simplify, drop the callback and map _tcsdup() in Unicode mode
directly to Curl_wcsdup().

Also fixes:

Follow-up to 76e047f #7540
Assisted-by: Jay Satiro

Closes #17843

@vszakats vszakats added Windows Windows-specific libcurl API labels Jul 7, 2025
@github-actions github-actions bot added the tests label Jul 7, 2025
@vszakats vszakats changed the title windows: fix wcsdup callback initialization in curl_global_init() windows: fix wcsdup callback init in curl_global_init() Jul 7, 2025
@vszakats vszakats changed the title windows: fix wcsdup callback init in curl_global_init() windows: fix wcsdup callback in curl_global_init() Jul 7, 2025
@vszakats
Copy link
Member Author

vszakats commented Jul 7, 2025

@jay What do you think?

@vszakats vszakats added the Unicode Unicode, code page, character encoding label Jul 7, 2025
@jay
Copy link
Member

jay commented Jul 7, 2025

re #7540 (comment) it looks like he's correct and I think we should give him credit Reported-by: Luca Kellermann even though he didn't follow up.

Curl_cwcsdup isn't set in curl_global_init_mem presumably because there's no wcsdup callback that the user can set. We probably thought it would be covered since the user sets a malloc callback which is then called by our internal wcsdup (Curl_wcsdup) but wcsdup (defined to Curl_cwcsdup pointer) doesn't always point to Curl_wcsdup. So your fix is right,

#if defined(_WIN32) && defined(UNICODE)
-    Curl_cwcsdup = (curl_wcsdup_callback)_wcsdup;
+    Curl_cwcsdup = (curl_wcsdup_callback)Curl_wcsdup;
#endif

however it makes that line redundant. It's already set before main so you could just remove those 3 lines instead. Another way that may be more palatable is leave the line in curl_global_init unchanged and add the line to curl_global_init_mem instead. There may be some slight performance benefit to have curl_global_init (ie no user memory callbacks) continue to call _wcsdup directly. Possibly though I'm forgetting something like trackmemory needs to track it. So 3 different ways to do it, one of the ways is the most correct but I'm not sure which.

Also:
- add two related casts to match rest of code.
- replace deprecated `wcsdup` with `_wcsdup` in libtests.
  Ref: https://learn.microsoft.com/cpp/c-runtime-library/reference/strdup-wcsdup

Follow-up to 76e047f curl#7540
Bug: curl#17840 (comment)
Bug: curl#7540 (comment)

Closes curl#17840
@vszakats
Copy link
Member Author

vszakats commented Jul 7, 2025

In theory someone might rely on this reset behavior if setting Curl_cwcsdup
behind the back of libcurl as described in https://curl.se/mail/lib-2021-07/0056.html.
Such hack isn't necessary after #7540, and the reset code was wrong, still,
IMO it'd be nicer to reset it from global_init(), for correctness.

I think setting it to _wcsdup would not work, because a customized allocator
could free a pointer allocated by the standard malloc called from _wcsdup.

I'm now wondering if it'd be possible to just #define _tcsdup Curl_wcsdup in
Unicode builds, and drop Curl_cwcsdup completely? Since we don't allow
overriding it, and there is also no use of doing that, because it internally calls into
the custom malloc anyway.

Credits: The original report remained hidden until I bumped into this issue while
working on #17840, to find out about the first report while reading up the code's
history. It was still useful to reinforce the problem, but like you mentioned in #7540,
it's important to report it as a new issue. Does "Co-reported-by" sound OK?

@vszakats vszakats changed the title windows: fix wcsdup callback in curl_global_init() windows: drop curl_wcsdup_callback callback Jul 7, 2025
@vszakats vszakats changed the title windows: drop curl_wcsdup_callback callback windows: drop redundant curl_wcsdup_callback callback Jul 7, 2025
@vszakats
Copy link
Member Author

vszakats commented Jul 7, 2025

Dropping the Windows-specific, redundant, callback, seems to work fine.

@jay
Copy link
Member

jay commented Jul 8, 2025

Dropping the Windows-specific, redundant, callback, seems to work fine.

Ah the fourth way. Yes, that works :)

Does "Co-reported-by" sound OK?

Sure, however you think is best.

@vszakats vszakats closed this in 8afb623 Jul 8, 2025
@vszakats vszakats deleted the wcsdup-fix branch July 8, 2025 11:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

libcurl API tests Unicode Unicode, code page, character encoding Windows Windows-specific

Development

Successfully merging this pull request may close these issues.

2 participants