Skip to content
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

Fix input buffering for A APIs #16313

Merged

Conversation

lhecker
Copy link
Member

@lhecker lhecker commented Nov 15, 2023

This fixes an issue where character-wise reading of an input like "abc"
would return "a" to the caller, store "b" as a partial translation
(= wrong) and return "c" for the caller to store it for the next call.

Closes #16223
Closes #16299

Validation Steps Performed

  • ReadFile with a buffer size of 1 returns inputs character by
    character without dropping any inputs ✅

@microsoft-github-policy-service microsoft-github-policy-service bot added Issue-Bug It either shouldn't be doing this or needs an investigation. Area-Input Related to input processing (key presses, mouse, etc.) Area-Server Down in the muck of API call servicing, interprocess communication, eventing, etc. Priority-1 A description (P1) Product-Conhost For issues in the Console codebase labels Nov 15, 2023
Comment on lines +90 to +93
for (const auto& s : til::utf16_iterator{ source })
{
char buffer[8];
const auto length = WideCharToMultiByte(cp, 0, &wch, 1, &buffer[0], sizeof(buffer), nullptr, nullptr);
const auto length = WideCharToMultiByte(cp, 0, s.data(), gsl::narrow_cast<int>(s.size()), &buffer[0], sizeof(buffer), nullptr, nullptr);
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'm more confident now, Dustin (#14745 (comment)). 😄

Copy link
Member

Choose a reason for hiding this comment

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

👀

@DHowett DHowett added this to To Cherry Pick in 1.18 Servicing Pipeline via automation Nov 15, 2023
@lhecker
Copy link
Member Author

lhecker commented Nov 16, 2023

Look ma! A new VS version dropped:
image

(Image for history, since it's gone when I fix it.)

@lhecker lhecker force-pushed the dev/lhecker/16223-input-buffer-fixup branch from 6ba2b45 to 0e9a776 Compare November 16, 2023 12:47
@DHowett
Copy link
Member

DHowett commented Nov 16, 2023

Would you be willing to pull the audit fixes into a separate PR?

@lhecker
Copy link
Member Author

lhecker commented Nov 16, 2023

Sure! #16325

// * don't store anything in `_cachedTextA`
// * leave "bc" in the `source` string, for the caller to handle
// Otherwise we'll copy "a", store "b" and return "c", which is wrong. See GH#16223.
if (target.empty())
Copy link
Member

Choose a reason for hiding this comment

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

in this case, target.empty() means target is full, right? based on how bytes_transfer works, the length will be zero when there is no space remaining

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, exactly. The exit condition is still !source.empty() as it was before but we now need to add target.empty() as one, as described in the comment. This results in target.empty() || !source.empty(), but the latter is already an implication of the former so we can shorten it.

@DHowett DHowett added this to To Cherry Pick in 1.19 Servicing Pipeline via automation Nov 16, 2023
@lhecker lhecker added the AutoMerge Marked for automatic merge by the bot when requirements are met label Nov 21, 2023
@microsoft-github-policy-service microsoft-github-policy-service bot merged commit 63b3820 into main Nov 21, 2023
17 checks passed
@microsoft-github-policy-service microsoft-github-policy-service bot deleted the dev/lhecker/16223-input-buffer-fixup branch November 21, 2023 20:57
@DHowett DHowett moved this from To Cherry Pick to Cherry Picked in 1.19 Servicing Pipeline Dec 4, 2023
DHowett added a commit that referenced this pull request Dec 4, 2023
This fixes an issue where character-wise reading of an input like "abc"
would return "a" to the caller, store "b" as a partial translation
(= wrong) and return "c" for the caller to store it for the next call.

Closes #16223
Closes #16299

## Validation Steps Performed
* `ReadFile` with a buffer size of 1 returns inputs character by
  character without dropping any inputs ✅

---------

Co-authored-by: Dustin L. Howett <duhowett@microsoft.com>
(cherry picked from commit 63b3820)
Service-Card-Id: 91122022
Service-Version: 1.19
@DHowett DHowett moved this from To Cherry Pick to Cherry Picked in 1.18 Servicing Pipeline Jan 22, 2024
DHowett added a commit that referenced this pull request Jan 23, 2024
This fixes an issue where character-wise reading of an input like "abc"
would return "a" to the caller, store "b" as a partial translation
(= wrong) and return "c" for the caller to store it for the next call.

Closes #16223
Closes #16299

## Validation Steps Performed
* `ReadFile` with a buffer size of 1 returns inputs character by
  character without dropping any inputs ✅

---------

Co-authored-by: Dustin L. Howett <duhowett@microsoft.com>
(cherry picked from commit 63b3820)
Service-Card-Id: 91108808
Service-Version: 1.18
@DHowett DHowett moved this from Cherry Picked to Shipped in 1.19 Servicing Pipeline Feb 21, 2024
@DHowett DHowett moved this from Cherry Picked to Shipped in 1.18 Servicing Pipeline Feb 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Input Related to input processing (key presses, mouse, etc.) Area-Server Down in the muck of API call servicing, interprocess communication, eventing, etc. AutoMerge Marked for automatic merge by the bot when requirements are met Issue-Bug It either shouldn't be doing this or needs an investigation. Priority-1 A description (P1) Product-Conhost For issues in the Console codebase
Development

Successfully merging this pull request may close these issues.

interaction of _read on fd 0 in terminal Lost new line character when using ReadFile
3 participants