KEMBAR78
Undefined-behaviour fix: safely call std::isspace in CompletionData by mohiuddin-khan-shiam · Pull Request #5564 · microsoft/winget-cli · GitHub
Skip to content

Conversation

mohiuddin-khan-shiam
Copy link
Contributor

@mohiuddin-khan-shiam mohiuddin-khan-shiam commented Jun 29, 2025

What was fixed

CompletionData.cpp passed a potentially negative char to std::isspace, invoking undefined behaviour on non-ASCII input (signed char platforms). This occasionally caused crashes or incorrect cursor repositioning when autocompleting commands containing UTF-8 characters.

How it was fixed

The character is now explicitly cast to unsigned char before the std::isspace check:

!std::isspace(static_cast<unsigned char>(commandLine[cursor - 1]))

This aligns with standard C++ guidance and makes the completion logic robust across all locales.

Microsoft Reviewers: Open in CodeFlow

odiomarcelino and others added 2 commits June 29, 2025 19:00
What was fixed
[CompletionData.cpp](cci:7://file:///c:/Users/T2430514/Downloads/winget-cli/src/AppInstallerCLICore/CompletionData.cpp:0:0-0:0) passed a potentially negative `char` to `std::isspace`, invoking undefined behaviour on non-ASCII input (signed `char` platforms). This occasionally caused crashes or incorrect cursor repositioning when autocompleting commands containing UTF-8 characters.

### How it was fixed
The character is now explicitly cast to `unsigned char` before the `std::isspace` check:

```cpp
!std::isspace(static_cast<unsigned char>(commandLine[cursor - 1]))
```

This aligns with standard C++ guidance and makes the completion logic robust across all locales.

Co-Authored-By: S. M. Mohiuddin Khan Shiam <147746955+mohiuddin-khan-shiam@users.noreply.github.com>
### What was fixed
[CompletionData.cpp](cci:7://file:///c:/Users/T2430514/Downloads/winget-cli/src/AppInstallerCLICore/CompletionData.cpp:0:0-0:0) passed a potentially negative `char` to `std::isspace`, invoking undefined behaviour on non-ASCII input (signed `char` platforms). This occasionally caused crashes or incorrect cursor repositioning when autocompleting commands containing UTF-8 characters.

### How it was fixed
The character is now explicitly cast to `unsigned char` before the `std::isspace` check:

```cpp
!std::isspace(static_cast<unsigned char>(commandLine[cursor - 1]))
```

This aligns with standard C++ guidance and makes the completion logic robust across all locales.
@florelis
Copy link
Member

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@florelis florelis merged commit da80018 into microsoft:master Jul 11, 2025
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants