Skip to content

idn: fix buffer use with WideCharToMultiByte - #19976

Closed
bagder wants to merge 1 commit into
masterfrom
bagder/winidn
Closed

bagder wants to merge 1 commit into
masterfrom
bagder/winidn

Conversation

@bagder

@bagder bagder commented Dec 15, 2025 •

Copy link
Copy Markdown
Member

Follow-up to 6694a42 #19798

Reported-by: Stanislav Fort

Follow-up to 6694a42

Reported-by: Stanislav Fort
@bagder bagder added the Windows Windows-specific label Dec 15, 2025
@github-actions github-actions Bot added the name lookup DNS and related tech label Dec 15, 2025
@bagder
bagder requested a review from Copilot December 15, 2025 07:07
@bagder

bagder commented Dec 15, 2025

Copy link
Copy Markdown
Member Author

augment review

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 fixes a buffer handling issue in the IDN (Internationalized Domain Names) conversion code for Windows. The issue was that WideCharToMultiByte may not null-terminate the output string when a positive character count is provided, leading to potential buffer issues. The fix ensures proper buffer allocation and null-termination.

Key Changes:

  • Allocates an extra byte for the null terminator in the UTF-8 buffer
  • Captures the return value from WideCharToMultiByte for proper error checking and to determine where to place the null terminator
  • Adds comprehensive documentation explaining the behavior of WideCharToMultiByte regarding null-termination

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

@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. No suggestions at this time.

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

@bagder
bagder requested a review from vszakats December 15, 2025 07:10
@bagder
bagder marked this pull request as ready for review December 15, 2025 07:10
vszakats
vszakats previously approved these changes Dec 15, 2025
@vszakats
vszakats requested a review from jay December 15, 2025 14:19
@vszakats
vszakats dismissed their stale review December 15, 2025 14:33

Still struggling with this.

@vszakats

vszakats commented Dec 15, 2025 •

Copy link
Copy Markdown
Member

Writing this to help understanding what's happening:

The curl API gets a NUL-terminated (UTF-8) string.
It first converts it to WIDE chars by first asking
MultiByteToWideChar() to calculate the length, by
passing -1. This API returns the length with the NUL
included, then does the conversion, with the output
also NUL-terminated. IdnTo* preserve this NUL as
documented. Then this NUL-terminated string gets
passed to WideCharToMultiByte(), which indeed
works as documented here, also meaning that it
preserves the NUL, if included in the length, which
is the case here.

This makes me think that adding an extra NUL is
not necessary here. Also confirmed by local tests
which didn't reveal this issue before the initial
commit, and I also couldn't replicate it now.

(I can replicate the non-NUL terminated behavior
by passing a length that excludes the NUL, but
that's not happening in the curl code.)

https://learn.microsoft.com/windows/win32/api/stringapiset/nf-stringapiset-multibytetowidechar
https://learn.microsoft.com/windows/win32/api/stringapiset/nf-stringapiset-widechartomultibyte
https://learn.microsoft.com/windows/win32/api/winnls/nf-winnls-idntoascii
https://learn.microsoft.com/windows/win32/api/winnls/nf-winnls-idntounicode

edit: WINE source code also seems to agree:

https://gitlab.winehq.org/wine/wine/-/blob/master/dlls/kernelbase/locale.c
https://gitlab.winehq.org/wine/wine/-/blob/master/dlls/ntdll/locale.c
https://gitlab.winehq.org/wine/wine/-/blob/master/dlls/ntdll/locale_private.h

@bagder

bagder commented Dec 15, 2025

Copy link
Copy Markdown
Member Author

Replaced by #19980

@bagder bagder closed this Dec 15, 2025
@bagder
bagder deleted the bagder/winidn branch December 15, 2025 16:00
vszakats added a commit to vszakats/curl that referenced this pull request Dec 16, 2025
Add comments to clarify that a terminating null is always present in
the buffers returned to the caller.

The curl APIs `win32_idn_to_ascii()` or `win32_ascii_to_idn()` receive
a null-terminated UTF-8 string as input. They first convert it to wide
chars by first asking `MultiByteToWideChar()` to calculate the length,
by passing -1. This API returns the length with the null char included
(= `strlen() + 1`), does the conversion, with the output also
null-terminated. `IdnTo*()` preserve this null character as documented.
Then we pass this null-terminated, fixed-length buffer ito
`WideCharToMultiByte()`, which keeps preserving the null, ending up in
the buffer returned to the caller.

Refs:
https://learn.microsoft.com/windows/win32/api/stringapiset/nf-stringapiset-multibytetowidechar
https://learn.microsoft.com/windows/win32/api/stringapiset/nf-stringapiset-widechartomultibyte
https://learn.microsoft.com/windows/win32/api/winnls/nf-winnls-idntoascii
https://learn.microsoft.com/windows/win32/api/winnls/nf-winnls-idntounicode

WINE source code:
https://gitlab.winehq.org/wine/wine/-/blob/wine-10.20/dlls/kernelbase/locale.c
https://gitlab.winehq.org/wine/wine/-/blob/wine-10.20/dlls/ntdll/locale.c
https://gitlab.winehq.org/wine/wine/-/blob/wine-10.20/dlls/ntdll/locale_private.h

Ref: curl#19976 (comment)
Follow-up to 6694a42 curl#19798

Closes curl#19980
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

name lookup DNS and related tech Windows Windows-specific

Development

Successfully merging this pull request may close these issues.

3 participants