Investigate why the COOKED_READ_DATA ctor takes a string_view for wchar_t data? #7766

Closed
opened 2026-01-31 01:12:08 +00:00 by claunia · 3 comments
Owner

Originally created by @zadjii-msft on GitHub (Apr 28, 2020).

COOKED_READ_DATA takes an initialData parameter of a string_view
d6cae40d26/src/host/readDataCooked.cpp (L46-L54)

However, when it memcpy's it, it copies it as a wchar_t buffer:
d6cae40d26/src/host/readDataCooked.cpp (L97-L103)

As you can see here, _bufPtr is a wchar_t* which is initialized to _bufPtr = reinterpret_cast<wchar_t*>(_buffer.get())
d6cae40d26/src/host/readDataCooked.hpp (L124-L136)

As I'm writing tests that use some cooked read data for #1856, I'm finding that the string I'm initializing the cooked read with is getting horribly mangled, because I'm passing a char buffer in.

Why is it like this? Does this magically just normally work when users call ReadConsole with wchar_t data in the buffer? I couldn't really figure it out in just a morning, so I'm filing this to make sure to follow up.

The test in question is ConptyRoundtripTests::TestResizeWithCookedRead.

Originally created by @zadjii-msft on GitHub (Apr 28, 2020). `COOKED_READ_DATA` takes an `initialData` parameter of a `string_view` https://github.com/microsoft/terminal/blob/d6cae40d26d2f4ba249d3960e8478c04681c8e1e/src/host/readDataCooked.cpp#L46-L54 However, when it `memcpy`'s it, it copies it as a `wchar_t` buffer: https://github.com/microsoft/terminal/blob/d6cae40d26d2f4ba249d3960e8478c04681c8e1e/src/host/readDataCooked.cpp#L97-L103 As you can see here, `_bufPtr` is a `wchar_t*` which is initialized to `_bufPtr = reinterpret_cast<wchar_t*>(_buffer.get())` https://github.com/microsoft/terminal/blob/d6cae40d26d2f4ba249d3960e8478c04681c8e1e/src/host/readDataCooked.hpp#L124-L136 As I'm writing tests that use some cooked read data for #1856, I'm finding that the string I'm initializing the cooked read with is getting horribly mangled, because I'm passing a `char` buffer in. Why is it like this? Does this magically just normally work when users call `ReadConsole` with `wchar_t` data in the buffer? I couldn't really figure it out in just a morning, so I'm filing this to make sure to follow up. The test in question is `ConptyRoundtripTests::TestResizeWithCookedRead`.
Author
Owner

@DHowett-MSFT commented on GitHub (Apr 28, 2020):

AFAIK this is related to how we share cooked read between A and W APIs?

@DHowett-MSFT commented on GitHub (Apr 28, 2020): AFAIK this is related to how we share cooked read between A and W APIs?
Author
Owner

@DHowett-MSFT commented on GitHub (Apr 29, 2020):

Actually, yeah, that doesn't make sense.

@DHowett-MSFT commented on GitHub (Apr 29, 2020): Actually, yeah, that doesn't make sense.
Author
Owner

@DHowett commented on GitHub (Aug 1, 2023):

IT TURNS OUT

That this was because initialData was explicitly documented as being broken for ReadConsoleA, and only worked for ReadConsoleW. We didn't need to support the ANSI version, so we were just being weirdos using string_view to pass around a pointer+length.

@DHowett commented on GitHub (Aug 1, 2023): IT TURNS OUT That this was because `initialData` was explicitly documented as being broken for `ReadConsoleA`, and only worked for `ReadConsoleW`. We didn't _need_ to support the ANSI version, so we were just being weirdos using `string_view` to pass around a pointer+length.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#7766