Skip to content

Undefined-behaviour fix: safely call std::isspace in CompletionData - #5564

Merged
Flor Chacón (florelis) merged 2 commits into
microsoft:masterfrom
mohiuddin-khan-shiam:master
Jul 11, 2025
Merged

Flor Chacón (florelis) merged 2 commits into
microsoft:masterfrom
mohiuddin-khan-shiam:master

Conversation

@mohiuddin-khan-shiam

@mohiuddin-khan-shiam S. M. Mohiuddin Khan Shiam (mohiuddin-khan-shiam) commented Jun 29, 2025 •

Copy link
Copy Markdown
Contributor

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

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
Copy Markdown
Member

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@florelis
Flor Chacón (florelis) merged commit da80018 into microsoft:master Jul 11, 2025
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