Skip to content

gh-106844: Fix null-bytes handling in LCMapStringEx in _winapi #106857

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from

Conversation

sobolevn
Copy link
Member

@sobolevn sobolevn commented Jul 18, 2023

@@ -1537,42 +1537,66 @@ _winapi_PeekNamedPipe_impl(PyObject *module, HANDLE handle, int size)
/*[clinic input]
_winapi.LCMapStringEx

locale: LPCWSTR
locale: unicode
Copy link
Member

Choose a reason for hiding this comment

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

Why change it for locale?

Copy link
Member Author

Choose a reason for hiding this comment

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

I just backported the solution from 927b5af#diff-42415407f8d0ef2d42e29d13d979f633e3543770e62c3871e1101ad532d336a8R1518

So, 3.11 and 3.12 would match.

Copy link
Member

Choose a reason for hiding this comment

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

The code would be simpler if keep the LPCWSTR converter here.

Copy link
Member

Choose a reason for hiding this comment

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

I agree. We don't need to treat this argument any differently - it's only the src argument that requires special handling.

Comment on lines 1567 to 1570
srcLen = (int)srcLenAsSsize;
if (srcLen != srcLenAsSsize) {
srcLen = -1;
}
Copy link
Member

Choose a reason for hiding this comment

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

How would it work for large (more than 2**32 characters) string containing NUL?

Copy link
Member

Choose a reason for hiding this comment

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

It doesn't, but that's the caller's responsibility.

Copy link
Member

Choose a reason for hiding this comment

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

LCMapStringEx() will simply return the result for the truncated input. It is bad. How is the caller expected to detect this problem if it only occurs in special circumferences?

What does LCMapStringEx() return for large input not containing NUL? Most likely it fails. We should either emulate this response (raise an OSError with the same Windows error code) or simply raise an OverflowError.

Copy link
Member

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

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

Please address my comments. If the code in 3.11 has the same flaws, it should be fixed too.

@@ -1537,42 +1537,66 @@ _winapi_PeekNamedPipe_impl(PyObject *module, HANDLE handle, int size)
/*[clinic input]
_winapi.LCMapStringEx

locale: LPCWSTR
locale: unicode
Copy link
Member

Choose a reason for hiding this comment

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

The code would be simpler if keep the LPCWSTR converter here.

@bedevere-bot
Copy link

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@serhiy-storchaka serhiy-storchaka added needs backport to 3.11 only security fixes needs backport to 3.12 only security fixes labels Jul 19, 2023
@zooba
Copy link
Member

zooba commented Aug 9, 2023

I don't think the fix is as simple as bringing forward this fix - the underlying code should be identical in current versions, it was just earlier versions that needed a different fix because of existing bugs in the Unicode converters.

I'll take a fresh look and see what ought to happen here. Chances are, updating the LPCWSTR clinic definition makes the most sense.

@zooba
Copy link
Member

zooba commented Aug 9, 2023

Looks like the changes to make argument clinic support Py_UNICODE_converter actually work when you opt to allow embedded nulls are... complex. I can't figure it out, and pretty sure if I did it'd have wide-ranging effects that we don't want to backport.

Leaving the locale argument as LPCWSTR should be fine - we only need to handle embedded nulls in the src argument.

@zooba zooba removed the needs backport to 3.11 only security fixes label Aug 9, 2023
@serhiy-storchaka
Copy link
Member

Opened #107832 for my propositions.

@serhiy-storchaka
Copy link
Member

#107832 fixed also other issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants