8000 Merge pull request #6770 from libgit2/ethomson/ctype · libgit2/libgit2@0f12a01 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0f12a01

Browse files
authored
Merge pull request #6770 from libgit2/ethomson/ctype
cast characters to unsigned when classifying characters
2 parents 06c0ac6 + 4675569 commit 0f12a01

File tree

12 files changed

+103
-65
lines changed

12 files changed

+103
-65
lines changed

deps/ntlmclient/ntlm.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -988,9 +988,9 @@ static inline bool generate_lm_hash(
988988
keystr2_len = (password_len > 7) ? MIN(14, password_len) - 7 : 0;
989989

990990
for (i = 0; i < keystr1_len; i++)
991-
keystr1[i] = (unsigned char)toupper(password[i]);
991+
keystr1[i] = (unsigned char)toupper((unsigned char)password[i]);
992992
for (i = 0; i < keystr2_len; i++)
993-
keystr2[i] = (unsigned char)toupper(password[i+7]);
993+
keystr2[i] = (unsigned char)toupper((unsigned char)password[i+7]);
994994

995995
/* DES encrypt the LM constant using the password as the key */
996996
des_key_from_password(&key1, keystr1, keystr1_len);

src/libgit2/config.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1499,7 +1499,7 @@ static int normalize_section(char *start, char *end)
14991499
for (scan = start; *scan; ++scan) {
15001500
if (end && scan >= end)
15011501
break;
1502-
if (isalnum(*scan))
1502+
if (git__isalnum(*scan))
15031503
*scan = (char)git__tolower(*scan);
15041504
else if (*scan != '-' || scan == start)
15051505
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 git__isalnum(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 git__isalnum(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 && git__tolower(*str) == git__tolower(*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' && git__isspace(*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 && (git__isalnum(*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 && !git__isspace(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 (git__isspace(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 (git__isalnum(*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
@@ -536,10 +536,10 @@ static int parse_len(size_t *out, const char *line, size_t linelen)
536536
num[PKT_LEN_SIZE] = '\0';
537537

538538
for (i = 0; i < PKT_LEN_SIZE; ++i) {
539-
if (!isxdigit(num[i])) {
539+
if (!git__isxdigit(num[i])) {
540540
/* Make sure there are no special characters before passing to error message */
541541
for (k = 0; k < PKT_LEN_SIZE; ++k) {
542-
if(!isprint(num[k])) {
542+
if(!git__isprint(num[k])) {
543543
num[k] = '.';
544544
}
545545
}

src/util/ctype_compat.h

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright (C) the libgit2 contributors. All rights reserved.
3+
*
4+
* This file is part of libgit2, distributed under the GNU GPL v2 with
5+
* a Linking Exception. For full terms see the included COPYING file.
6+
*/
7+
#ifndef INCLUDE_ctype_compat_h__
8+
#define INCLUDE_ctype_compat_h__
9+
10+
/*
11+
* The Microsoft C runtime (MSVCRT) may take a heavy lock on the
12+
* locale in order to figure out how the `ctype` functions work.
13+
* This is deeply slow. Provide our own to avoid that.
14+
*/
15+
16+
#ifdef GIT_WIN32
17+
18+
GIT_INLINE(int) git__tolower(int c)
19+
{
20+
return (c >= 'A' && c <= 'Z') ? (c + 32) : c;
21+
}
22+
23+
GIT_INLINE(int) git__toupper(int c)
24+
{
25+
return (c >= 'a' && c <= 'z') ? (c - 32) : c;
26+
}
27+
28+
GIT_INLINE(bool) git__isalpha(int c)
29+
{
30+
return ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'));
31+
}
32+
33+
GIT_INLINE(bool) git__isdigit(int c)
34+
{
35+
return (c >= '0' && c <= '9');
36+
}
37+
38+
GIT_INLINE(bool) git__isalnum(int c)
39+
{
40+
return git__isalpha(c) || git__isdigit(c);
41+
}
42+
43+
GIT_INLINE(bool) git__isspace(int c)
44+
{
45+
return (c == ' ' || c == '\t' || c == '\n' || c == '\f' || c == '\r' || c == '\v');
46+
}
47+
48+
GIT_INLINE(bool) git__isxdigit(int c)
49+
{
50+
return ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'));
51+
}
52+
53+
GIT_INLINE(bool) git__isprint(int c)
54+
{
55+
return (c >= ' ' && c <= '~');
56+
}
57+
58+
#else
59+
# define git__tolower(a) tolower((unsigned char)(a))
60+
# define git__toupper(a) toupper((unsigned char)(a))
61+
62+
# define git__isalpha(a) (!!isalpha((unsigned char)(a)))
63+
# define git__isdigit(a) (!!isdigit((unsigned char)(a)))
64+
# define git__isalnum(a) (!!isalnum((unsigned char)(a)))
65+
# define git__isspace(a) (!!isspace((unsigned char)(a)))
66+
# define git__isxdigit(a) (!!isxdigit((unsigned char)(a)))
67+
# define git__isprint(a) (!!isprint((unsigned char)(a)))
68+
#endif
69+
70+
#endif

src/util/date.c

Lines changed: 13 additions & 13 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 (git__toupper(*date) == git__toupper(*str))
133133
continue;
134-
if (!isalnum(*date))
134+
if (!git__isalnum(*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 (git__isalpha(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 && git__isdigit(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 (git__isdigit(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 (git__isdigit(date[n]));
368368

369369
/* Four-digit year or a timezone? */
370370
if (n == 4) {
@@ -514,11 +514,11 @@ static int parse_date_basic(const char *date, git_time_t *timestamp, int *offset
514514
if (!c || c == '\n')
515515
break;
516516

517-
if (isalpha(c))
517+
if (git__isalpha(c))
518518
match = match_alpha(date, &tm, offset);
519-
else if (isdigit(c))
519+
else if (git__isdigit(c))
520520
match = match_digit(date, &tm, offset, &tm_gmt);
521-
else if ((c == '-' || c == '+') && isdigit(date[1]))
521+
else if ((c == '-' || c == '+') && git__isdigit(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 (git__isalpha(*++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 (git__isdigit(end[1])) {
787787
size_t match = match_multi_number(number, *end, date, end, tm);
788788
if (match)
789789
return date + match;
@@ -843,13 +843,13 @@ static git_time_t approxidate_str(const char *date,
843843
if (!c)
844844
break;
845845
date++;
846-
if (isdigit(c)) {
846+
if (git__isdigit(c)) {
847847
pending_number(&tm, &number);
848848
date = approxidate_digit(date-1, &tm, &number);
849849
touched = 1;
850850
continue;
851851
}
852-
if (isalpha(c))
852+
if (git__isalpha(c))
853853
date = approxidate_alpha(date-1, &tm, &now, &number, &touched);
854854
}
855855
pending_number(&tm, &number);

src/util/git2_util.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,5 +165,6 @@ typedef struct git_str git_str;
165165
if (GIT_MULTIPLY_SIZET_OVERFLOW(out, nelem, elsize)) { return -1; }
166166

167167
#include "util.h"
168+
#include "ctype_compat.h"
168169

169170
#endif

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+
git__isxdigit(str[str_pos + 1]) &&
489+
git__isxdigit(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: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -83,15 +83,6 @@ extern char *git__strsep(char **end, const char *sep);
8383
extern void git__strntolower(char *str, size_t len);
8484
extern void git__strtolower(char *str);
8585

86-
#ifdef GIT_WIN32
87-
GIT_INLINE(int) git__tolower(int c)
88-
{
89-
return (c >= 'A' && c <= 'Z') ? (c + 32) : c;
90-
}
91-
#else
92-
# define git__tolower(a) tolower(a)
93-
#endif
94-
9586
extern size_t git__linenlen(const char *buffer, size_t buffer_len);
9687

9788
GIT_INLINE(const char *) git__next_line(const char *s)
@@ -249,26 +240,6 @@ GIT_INLINE(size_t) git__size_t_powerof2(size_t v)
249240
return git__size_t_bitmask(v) + 1;
250241
}
251242

252-
GIT_INLINE(bool) git__isupper(int c)
253-
{
254-
return (c >= 'A' && c <= 'Z');
255-
}
256-
257-
GIT_INLINE(bool) git__isalpha(int c)
258-
{
259-
return ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'));
260-
}
261-
262-
GIT_INLINE(bool) git__isdigit(int c)
263-
{
264-
return (c >= '0' && c <= '9');
265-
}
266-
267-
GIT_INLINE(bool) git__isspace(int c)
268-
{
269-
return (c == ' ' || c == '\t' || c == '\n' || c == '\f' || c == '\r' || c == '\v');
270-
}
271-
272243
GIT_INLINE(bool) git__isspace_nonlf(int c)
273244
{
274245
return (c == ' ' || c == '\t' || c == '\f' || c == '\r' || c == '\v');
@@ -279,11 +250,6 @@ GIT_INLINE(bool) git__iswildcard(int c)
279250
return (c == '*' || c == '?' || c == '[');
280251
}
281252

282-
GIT_INLINE(bool) git__isxdigit(int c)
283-
{
284-
return ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'));
285-
}
286-
287253
/*
288254
* Parse a string value as a boolean, just like Core Git does.
289255
*

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] == '/' && git__isalpha(src[1]) && src[2] == '/') {
320320
*tgt++ = src[1];
321321
*tgt++ = ':';
322322
*tgt++ = '\\';

0 commit comments

Comments
 (0)
0