Skip to content

Commit

Permalink
[libc] fix -Wtype-limits in wctob (#74511)
Browse files Browse the repository at this point in the history
GCC warns:
```
libc/src/__support/wctype_utils.h:33:20: error: comparison of unsigned
expression in ‘< 0’ is always false [-Werror=type-limits]
   33 |   if (c > 127 || c < 0)
      |                  ~~^~~
```

Looking into the signedness of wint_t, it looks like depending on the
platform,
__WINT_TYPE__ is defined to int or unsigned int depending on the
platform.

Link:
https://lab.llvm.org/buildbot/#/builders/250/builds/14891/steps/6/logs/stdio
  • Loading branch information
nickdesaulniers committed Dec 5, 2023
1 parent 07157db commit 079ca05
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions libc/src/__support/wctype_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ LIBC_INLINE cpp::optional<int> wctob(wint_t c) {
// This needs to be translated to EOF at the callsite. This is to avoid
// including stdio.h in this file.
// The standard states that wint_t may either be an alias of wchar_t or
// an alias of an integer type, so we need to keep the c < 0 check.
if (c > 127 || c < 0)
// an alias of an integer type, different platforms define this type with
// different signedness. This is equivalent to `(c > 127) || (c < 0)` but also
// works without -Wtype-limits warnings when `wint_t` is unsigned.
if ((c & ~127) != 0)
return cpp::nullopt;
return static_cast<int>(c);
}
Expand Down

0 comments on commit 079ca05

Please sign in to comment.