Skip to content

Commit 2c62a90

Browse files
simarkjgalar
authored andcommitted
common: cast arguments to character classification functions to unsigned char
We get failures of this type on the cygwin CI machine: 15:28:20 common.c: In function `bt_common_string_is_printable`: 15:28:20 common.c:786:16: error: array subscript has type `char` [-Werror=char-subscripts] 15:28:20 786 | if (!isprint(*ch) && *ch != '\n' && *ch != '\r' && 15:28:20 | ^~~ This error only pops up on some platforms that have isprint implemented using a lookup table. This table is indexed using `*ch`, which is a char. And because char is signed on some platforms, gcc warns that this is dangerous: we could access the array with a negative index, which would yield unexpected results. This is on purpose in newlib (the libc used by cygwin, apparently), see this comment: https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;a=blob;f=newlib/libc/include/ctype.h;h=a0009af17485acc3d70586a0051269a7a9c350d5;hb=HEAD#l78 The Linux man page for isprint also mentions it: The standards require that the argument c for these functions is either EOF or a value that is representable in the type unsigned char. If the argument c is of type char, it must be cast to unsigned char, as in the following example: char c; ... res = toupper((unsigned char) c); This is necessary because char may be the equivalent of signed char, in which case a byte where the top bit is set would be sign extended when converting to int, yielding a value that is outside the range of unsigned char. Add casts to unsigned char to fix the various instances of this error. Change-Id: Ice2305490997f595c6f5140a8be2abaa7fd1d8f6 Signed-off-by: Simon Marchi <[email protected]> Reviewed-on: https://review.lttng.org/c/babeltrace/+/3194 Reviewed-by: Philippe Proulx <[email protected]> CI-Build: Michael Jeanson <[email protected]> Tested-by: jenkins <[email protected]> (cherry picked from commit 994cd34)
1 parent 6559432 commit 2c62a90

File tree

6 files changed

+7
-7
lines changed

6 files changed

+7
-7
lines changed

src/common/common.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -783,7 +783,7 @@ bool bt_common_string_is_printable(const char *input)
783783
BT_ASSERT_DBG(input);
784784

785785
for (ch = input; *ch != '\0'; ch++) {
786-
if (!isprint(*ch) && *ch != '\n' && *ch != '\r' &&
786+
if (!isprint((unsigned char) *ch) && *ch != '\n' && *ch != '\r' &&
787787
*ch != '\t' && *ch != '\v') {
788788
printable = false;
789789
goto end;

src/plugins/ctf/common/metadata/lexer.l

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,5 +134,5 @@ _Imaginary setstring(yyextra, yylval, yytext); return CTF_IMAGINARY;
134134

135135
{IDENTIFIER} BT_LOGT("Got identifier: id=\"%s\"", yytext); setstring(yyextra, yylval, yytext); if (is_type(yyextra, yytext)) return ID_TYPE; else return IDENTIFIER;
136136
[ \t\r\n] ; /* ignore */
137-
. _BT_LOGE_LINENO(yylineno, "Invalid character: char=\"%c\", val=0x%02x", isprint(yytext[0]) ? yytext[0] : '\0', yytext[0]); return CTF_ERROR;
137+
. _BT_LOGE_LINENO(yylineno, "Invalid character: char=\"%c\", val=0x%02x", isprint((unsigned char) yytext[0]) ? yytext[0] : '\0', yytext[0]); return CTF_ERROR;
138138
%%

src/plugins/ctf/fs-sink/translate-trace-ir-to-ctf-ir.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,14 +154,14 @@ bool ist_valid_identifier(const char *name)
154154
}
155155

156156
/* Make sure the name starts with a letter or `_` */
157-
if (!isalpha(name[0]) && name[0] != '_') {
157+
if (!isalpha((unsigned char) name[0]) && name[0] != '_') {
158158
ist_valid = false;
159159
goto end;
160160
}
161161

162162
/* Make sure the name only contains letters, digits, and `_` */
163163
for (at = name; *at != '\0'; at++) {
164-
if (!isalnum(*at) && *at != '_') {
164+
if (!isalnum((unsigned char) *at) && *at != '_') {
165165
ist_valid = false;
166166
goto end;
167167
}

src/plugins/text/dmesg/dmesg.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -773,7 +773,7 @@ bt_message_iterator_class_next_method_status dmesg_msg_iter_next_one(
773773

774774
/* Ignore empty lines, once trimmed */
775775
for (ch = dmesg_msg_iter->linebuf; *ch != '\0'; ch++) {
776-
if (!isspace(*ch)) {
776+
if (!isspace((unsigned char) *ch)) {
777777
only_spaces = false;
778778
break;
779779
}

src/plugins/text/pretty/print.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,7 @@ void print_escape_string(struct pretty_component *pretty, const char *str)
639639
}
640640

641641
/* Standard characters. */
642-
if (!iscntrl(str[i])) {
642+
if (!iscntrl((unsigned char) str[i])) {
643643
bt_common_g_string_append_c(pretty->string, str[i]);
644644
continue;
645645
}

tests/utils/tap/tap.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ _gen_result(int ok, const char *func, const char *file, unsigned int line,
109109
if(local_test_name) {
110110
name_is_digits = 1;
111111
for(c = local_test_name; *c != '\0'; c++) {
112-
if(!isdigit(*c) && !isspace(*c)) {
112+
if(!isdigit((unsigned char) *c) && !isspace((unsigned char) *c)) {
113113
name_is_digits = 0;
114114
break;
115115
}

0 commit comments

Comments
 (0)