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

Convert to unsigned char before invoking std::isprint(). #174

Merged
merged 1 commit into from
Aug 18, 2024

Conversation

cristian64
Copy link
Collaborator

As the documentation states, the caller must check that the input character in std::isprint() is a valid unsigned char:

Like all other functions from <cctype>, the behavior of std::isprint is undefined if the argument's value is neither representable as unsigned char nor equal to EOF. To use these functions safely with plain chars (or signed chars), the argument should first be converted to unsigned char:

bool my_isprint(char ch)
{
    return std::isprint(static_cast<unsigned char>(ch));
}

The aforementioned undefined behavior manifests as a debug assertion when compiled with MSVC:

image

The affected functionality was introduced in #122.

Fixes #173.

As the documentation states, the caller must check that the input
character in [`std::isprint()`](https://en.cppreference.com/w/cpp/string/byte/isprint) is a valid `unsigned char`:

> Like all other functions from `<cctype>`, the behavior of
> `std::isprint` is undefined if the argument's value is neither
> representable as `unsigned char` nor equal to `EOF`. To use these
> functions safely with plain `char`s (or `signed char`s), the argument
> should first be converted to `unsigned char`:
>
> ```cpp
> bool my_isprint(char ch)
> {
>     return std::isprint(static_cast<unsigned char>(ch));
> }
> ```

The aforementioned undefined behavior manifests as a debug assertion
when compiled with MSVC:

![image](https://github.com/user-attachments/assets/144e3573-f53c-4e89-a0cd-d615caa2749e)

The affected functionality was introduced in aldelaro5#122.

Fixes aldelaro5#173.
@dreamsyntax
Copy link
Collaborator

Thanks, beat me to it

Copy link
Collaborator

@dreamsyntax dreamsyntax left a comment

Choose a reason for hiding this comment

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

Tested reproducing the crash on VC++, no longer occurs.

@dreamsyntax dreamsyntax merged commit bcb146f into aldelaro5:master Aug 18, 2024
4 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.

Bug: Crash on Scrolling with Certain Watchlists
2 participants