Skip to content

Avoid warning when char is unsigned#1343

Merged
SteffenL merged 1 commit into
webview:masterfrom
Macintron:fixCharSignedness
Jan 6, 2026
Merged

Avoid warning when char is unsigned#1343
SteffenL merged 1 commit into
webview:masterfrom
Macintron:fixCharSignedness

Conversation

@Macintron

Copy link
Copy Markdown
Contributor

char's signedness is implementation‑defined.

https://en.cppreference.com/w/cpp/language/types.html -> Character types ->
The signedness of char depends on the compiler and the target platform: the defaults for ARM and PowerPC are typically unsigned, the defaults for x86 and x64 are typically signed.

C++17/20 standard: 6.9.1: It is implementation-defined whether a char object can hold negative values.

@SteffenL

SteffenL commented Jan 5, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the PR but the existing function signatures look correct to me so it would be great if you could explain the problem and solution. Instead of this change, would a simple static_cast<char>(0x1f) do?

@Macintron

Copy link
Copy Markdown
Contributor Author

On most ARM 64‑bit ABIs char is signed by default. When the parameter type is char, the expression c >= 0 && c <= 0x1f is evaluated as two separate comparisons:

  • c >= 0 because c is a signed integer, any value that fits in a char is already ≥ 0 (the range is –128 … 127). Therefore the compiler can prove that this part of the condition is always true and emits the warning.
  • c <= 0x1f this part is still useful, because it checks whether the value falls in the control‑character range (0 … 31).

So the warning is not about a bug in the logic; it is just telling that the first sub‑expression does nothing.

If you want to keep the interface with char we can cast to an unsigned type for the comparison

constexpr bool is_ascii_control_char(char c) {
    return static_cast<unsigned char>(c) <= 0x1f;
}

@SteffenL

SteffenL commented Jan 6, 2026

Copy link
Copy Markdown
Collaborator

On most ARM 64‑bit ABIs char is signed by default

I think there's a mixup in your explanation and you meant that it's unsigned by default.

Thank you — I understand the problem now.

Example of a warning with GCC (targeting aarch64):

test.cpp:2:57: warning: comparison is always true due to limited range of data type [-Wtype-limits]
    2 | constexpr bool is_ascii_control_char(char c) { return c >= 0 && c <= 0x1f; }
      |                                                       ~~^~~~

We can also get this warning if signedness is specified to be unsigned on the command line (--unsigned-char).

The warning does not show up with only -Wall -Wpedantic — we need -Wextra or just -Wtype-limits.

Comment thread core/include/webview/detail/json.hh Outdated
@Macintron Macintron requested a review from SteffenL January 6, 2026 09:47

@SteffenL SteffenL left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Looks good to me ― thank you!

@SteffenL SteffenL changed the title Fix char signedness. Fix default unsigned char warning Jan 6, 2026
@SteffenL SteffenL changed the title Fix default unsigned char warning Avoid warning when char is unsigned Jan 6, 2026
@SteffenL SteffenL merged commit 5c5db3f into webview:master Jan 6, 2026
102 checks passed
@Macintron Macintron deleted the fixCharSignedness branch January 20, 2026 07:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants