8000 ctype: cast characters to unsigned when classifying characters · libgit2/libgit2@e619b88 · GitHub
[go: up one dir, main page]

Skip to content

Commit e619b88

Browse files
committed
ctype: cast characters to unsigned when classifying characters
ctype classification takes an integer in as returned from getc() if we just sign extend characters to integers 128-255 will be misclassified. (255 will become EOF) Newlib in particular doesn't like this since they uses the value as an index in a lookup table.
1 parent fde12a1 commit e619b88

File tree

9 files changed

+27
-26
lines changed

9 files changed

+27
-26
lines changed

src/libgit2/config.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1447,7 +1447,7 @@ static int normalize_section(char *start, char *end)
14471447
for (scan = start; *scan; ++scan) {
14481448
if (end && scan >= end)
14491449
break;
1450-
if (isalnum(*scan))
1450+
if (isalnum((unsigned char)*scan))
14511451
*scan = (char)git__tolower(*scan);
14521452
else if (*scan != '-' || scan == start)
14531453
return GIT_EINVALIDSPEC;

src/libgit2/config_parse.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ static void set_parse_error(git_config_parser *reader, int col, const char *erro
2525
}
2626

2727

28-
GIT_INLINE(int) config_keychar(int c)
28+
GIT_INLINE(int) config_keychar(char c)
2929
{
30-
return isalnum(c) || c == '-';
30+
return isalnum((unsigned char)c) || c == '-';
3131
}
3232

3333
static int strip_comments(char *line, int in_quotes)
@@ -158,9 +158,10 @@ static int parse_subsection_header(git_config_parser *reader, const char *line,
158158
static int parse_section_header(git_config_parser *reader, char **section_out)
159159
{
160160
char *name, *name_end;
161-
int name_length, c, pos;
161+
int name_length, pos;
162162
int result;
163163
char *line;
164+
char c;
164165
size_t line_len;
165166

166167
git_parse_advance_ws(&reader->ctx);
@@ -382,7 +383,7 @@ static int parse_multiline_variable(git_config_parser *reader, git_str *value, i
382383

383384
GIT_INLINE(bool) is_namechar(char c)
384385
{
385-
return isalnum(c) || c == '-';
386+
return isalnum((unsigned char)c) || c == '-';
386387
}
387388

388389
static int parse_name(

src/libgit2/path.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ GIT_INLINE(size_t) common_prefix_icase(const char *str, size_t len, const char *
202202
{
203203
size_t count = 0;
204204

205-
while (len > 0 && tolower(*str) == tolower(*prefix)) {
205+
while (len > 0 && tolower((unsigned char)*str) == tolower((unsigned char)*prefix)) {
206206
count++;
207207
str++;
208208
prefix++;

src/libgit2/trailer.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ static const char *const git_generated_prefixes[] = {
2424
static int is_blank_line(const char *str)
2525
{
2626
const char *s = str;
27-
while (*s && *s != '\n' && isspace(*s))
27+
while (*s && *s != '\n' && isspace((unsigned char)*s))
2828
s++;
2929
return !*s || *s == '\n';
3030
}
@@ -93,7 +93,7 @@ static bool find_separator(size_t *out, const char *line, const char *separators
9393
return true;
9494
}
9595

96-
if (!whitespace_found && (isalnum(*c) || *c == '-'))
96+
if (!whitespace_found && (isalnum((unsigned char)*c) || *c == '-'))
9797
continue;
9898
if (c != line && (*c == ' ' || *c == '\t')) {
9999
whitespace_found = 1;
@@ -233,12 +233,12 @@ static size_t find_trailer_start(const char *buf, size_t len)
233233
}
234234

235235
find_separator(&separator_pos, bol, TRAILER_SEPARATORS);
236-
if (separator_pos >= 1 && !isspace(bol[0])) {
236+
if (separator_pos >= 1 && !isspace((unsigned char)bol[0])) {
237237
trailer_lines++;
238238
possible_continuation_lines = 0;
239239
if (recognized_prefix)
240240
continue;
241-
} else if (isspace(bol[0]))
241+
} else if (isspace((unsigned char)bol[0]))
242242
possible_continuation_lines++;
243243
else {
244244
non_trailer_lines++;
@@ -323,7 +323,7 @@ int git_message_trailers(git_message_trailer_array *trailer_arr, const char *mes
323323
goto ret;
324324
}
325325

326-
if (isalnum(*ptr) || *ptr == '-') {
326+
if (isalnum((unsigned char)*ptr) || *ptr == '-') {
327327
/* legal key character */
328328
NEXT(S_KEY);
329329
}

src/libgit2/transports/smart_pkt.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -535,10 +535,10 @@ static int parse_len(size_t *out, const char *line, size_t linelen)
535535
num[PKT_LEN_SIZE] = '\0';
536536

537537
for (i = 0; i < PKT_LEN_SIZE; ++i) {
538-
if (!isxdigit(num[i])) {
538+
if (!isxdigit((unsigned char)num[i])) {
539539
/* Make sure there are no special characters before passing to error message */
540540
for (k = 0; k < PKT_LEN_SIZE; ++k) {
541-
if(!isprint(num[k])) {
541+
if(!isprint((unsigned char)num[k])) {
542542
num[k] = '.';
543543
}
544544
}

src/util/date.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,9 @@ static size_t match_string(const char *date, const char *str)
129129
for (i = 0; *date; date++, str++, i++) {
130130
if (*date == *str)
131131
continue;
132-
if (toupper(*date) == toupper(*str))
132+
if (toupper((unsigned char)*date) == toupper((unsigned char)*str))
133133
continue;
134-
if (!isalnum(*date))
134+
if (!isalnum((unsigned char)*date))
135135
break;
136136
return 0;
137137
}
@@ -143,7 +143,7 @@ static int skip_alpha(const char *date)
143143
int i = 0;
144144
do {
145145
i++;
146-
} while (isalpha(date[i]));
146+
} while (isalpha((unsigned char)date[i]));
147147
return i;
148148
}
149149

@@ -251,7 +251,7 @@ static size_t match_multi_number(unsigned long num, char c, const char *date, ch
251251

252252
num2 = strtol(end+1, &end, 10);
253253
num3 = -1;
254-
if (*end == c && isdigit(end[1]))
254+
if (*end == c && isdigit((unsigned char)end[1]))
255255
num3 = strtol(end+1, &end, 10);
256256

257257
/* Time? Date? */
@@ -349,7 +349,7 @@ static size_t match_digit(const char *date, struct tm *tm, int *offset, int *tm_
349349
case '.':
350350
case '/':
351351
case '-':
352-
if (isdigit(end[1])) {
352+
if (isdigit((unsigned char)end[1])) {
353353
size_t match = match_multi_number(num, *end, date, end, tm);
354354
if (match)
355355
return match;
@@ -364,7 +364,7 @@ static size_t match_digit(const char *date, struct tm *tm, int *offset, int *tm_
364364
n = 0;
365365
do {
366366
n++;
367-
} while (isdigit(date[n]));
367+
} while (isdigit((unsigned char)date[n]));
368368

369369
/* Four-digit year or a timezone? */
370370
if (n == 4) {
@@ -518,7 +518,7 @@ static int parse_date_basic(const char *date, git_time_t *timestamp, int *offset
518518
match = match_alpha(date, &tm, offset);
519519
else if (isdigit(c))
520520
match = match_digit(date, &tm, offset, &tm_gmt);
521-
else if ((c == '-' || c == '+') && isdigit(date[1]))
521+
else if ((c == '-' || c == '+') && isdigit((unsigned char)date[1]))
522522
match = match_tz(date, offset);
523523

524524
if (!match) {
@@ -682,7 +682,7 @@ static const char *approxidate_alpha(const char *date, struct tm *tm, struct tm
682682
const char *end = date;
683683
int i;
684684

685-
while (isalpha(*++end))
685+
while (isalpha((unsigned char)*++end))
686686
/* scan to non-alpha */;
687687

688688
for (i = 0; i < 12; i++) {
@@ -783,7 +783,7 @@ static const char *approxidate_digit(const char *date, struct tm *tm, int *num)
783783
case '.':
784784
case '/':
785785
case '-':
786-
if (isdigit(end[1])) {
786+
if (isdigit((unsigned char)end[1])) {
787787
size_t match = match_multi_number(number, *end, date, end, tm);
788788
if (match)
789789
return date + match;

src/util/str.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -485,8 +485,8 @@ int git_str_decode_percent(
485485
for (str_pos = 0; str_pos < str_len; buf->size++, str_pos++) {
486486
if (str[str_pos] == '%' &&
487487
str_len > str_pos + 2 &&
488-
isxdigit(str[str_pos + 1]) &&
489-
isxdigit(str[str_pos + 2])) {
488+
isxdigit((unsigned char)str[str_pos + 1]) &&
489+
isxdigit((unsigned char)str[str_pos + 2])) {
490490
buf->ptr[buf->size] = (HEX_DECODE(str[str_pos + 1]) << 4) +
491491
HEX_DECODE(str[str_pos + 2]);
492492
str_pos += 2;

src/util/util.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ GIT_INLINE(int) git__tolower(int c)
8989
return (c >= 'A' && c <= 'Z') ? (c + 32) : c;
9090
}
9191
#else
92-
# define git__tolower(a) tolower(a)
92+
# define git__tolower(a) tolower((unsigned char)(a))
9393
#endif
9494

9595
extern size_t git__linenlen(const char *buffer, size_t buffer_len);

tests/libgit2/repo/open.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ static void unposix_path(git_str *path)
316316
src = tgt = path->ptr;
317317

318318
/* convert "/d/..." to "d:\..." */
319-
if (src[0] == '/' && isalpha(src[1]) && src[2] == '/') {
319+
if (src[0] == '/' && isalpha((unsigned char)src[1]) && src[2] == '/') {
320320
*tgt++ = src[1];
321321
*tgt++ = ':';
322322
*tgt++ = '\\';

0 commit comments

Comments
 (0)
0