Skip to content

Commit

Permalink
Avoid plain char for ctype macros
Browse files Browse the repository at this point in the history
On some platforms ctype functions are defined as macros accesing tables.
A plain char may be `signed` or `unsigned` per implementations and the
extension result implementation dependent.

gcc warns such case:

```
parser.c: In function 'rstring_cache_fetch':
parser.c:138:33: warning: array subscript has type 'char' [-Wchar-subscripts]
  138 |     if (RB_UNLIKELY(!isalpha(str[0]))) {
      |                              ~~~^~~
parser.c: In function 'rsymbol_cache_fetch':
parser.c:190:33: warning: array subscript has type 'char' [-Wchar-subscripts]
  190 |     if (RB_UNLIKELY(!isalpha(str[0]))) {
      |                              ~~~^~~
```
  • Loading branch information
nobu committed Jan 30, 2025
1 parent f3fb8a2 commit 4431b36
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions ext/json/ext/parser/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ static VALUE rstring_cache_fetch(rvalue_cache *cache, const char *str, const lon
return Qfalse;
}

if (RB_UNLIKELY(!isalpha(str[0]))) {
if (RB_UNLIKELY(!isalpha((unsigned char)str[0]))) {
// Simple heuristic, if the first character isn't a letter,
// we're much less likely to see this string again.
// We mostly want to cache strings that are likely to be repeated.
Expand Down Expand Up @@ -187,7 +187,7 @@ static VALUE rsymbol_cache_fetch(rvalue_cache *cache, const char *str, const lon
return Qfalse;
}

if (RB_UNLIKELY(!isalpha(str[0]))) {
if (RB_UNLIKELY(!isalpha((unsigned char)str[0]))) {
// Simple heuristic, if the first character isn't a letter,
// we're much less likely to see this string again.
// We mostly want to cache strings that are likely to be repeated.
Expand Down

0 comments on commit 4431b36

Please sign in to comment.