8000 ctype: cast characters to unsigned when classifying characters by boretrk · Pull Request #6679 · libgit2/libgit2 · GitHub
[go: up one dir, main page]

Skip to content

ctype: cast characters to unsigned when classifying characters #6679

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
  • Loading branch information
boretrk committed Dec 16, 2023
commit e619b884d5f121d7e584f77e2187b720d4d23f01
2 changes: 1 addition & 1 deletion src/libgit2/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -1447,7 +1447,7 @@ static int normalize_section(char *start, char *end)
for (scan = start; *scan; ++scan) {
if (end && scan >= end)
break;
if (isalnum(*scan))
if (isalnum((unsigned char)*scan))
*scan = (char)git__tolower(*scan);
else if (*scan != '-' || scan == start)
return GIT_EINVALIDSPEC;
Expand Down
9 changes: 5 additions & 4 deletions src/libgit2/config_parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ static void set_parse_error(git_config_parser *reader, int col, const char *erro
}


GIT_INLINE(int) config_keychar(int c)
GIT_INLINE(int) config_keychar(char c)
{
return isalnum(c) || c == '-';
return isalnum((unsigned char)c) || c == '-';
}

static int strip_comments(char *line, int in_quotes)
Expand Down Expand Up @@ -158,9 +158,10 @@ static int parse_subsection_header(git_config_parser *reader, const char *line,
static int parse_section_header(git_config_parser *reader, char **section_out)
{
char *name, *name_end;
int name_length, c, pos;
int name_length, pos;
int result;
char *line;
char c;
size_t line_len;

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

GIT_INLINE(bool) is_namechar(char c)
{
return isalnum(c) || c == '-';
return isalnum((unsigned char)c) || c == '-';
}

static int parse_name(
Expand Down
2 changes: 1 addition & 1 deletion src/libgit2/path.c
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ GIT_INLINE(size_t) common_prefix_icase(const char *str, size_t len, const char *
{
size_t count = 0;

while (len > 0 && tolower(*str) == tolower(*prefix)) {
while (len > 0 && tolower((unsigned char)*str) == tolower((unsigned char)*prefix)) {
count++;
str++;
prefix++;
Expand Down
10 changes: 5 additions & 5 deletions src/libgit2/trailer.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ static const char *const git_generated_prefixes[] = {
static int is_blank_line(const char *str)
{
const char *s = str;
while (*s && *s != '\n' && isspace(*s))
while (*s && *s != '\n' && isspace((unsigned char)*s))
s++;
return !*s || *s == '\n';
}
Expand Down Expand Up @@ -93,7 +93,7 @@ static bool find_separator(size_t *out, const char *line, const char *separators
return true;
}

if (!whitespace_found && (isalnum(*c) || *c == '-'))
if (!whitespace_found && (isalnum((unsigned char)*c) || *c == '-'))
continue;
if (c != line && (*c == ' ' || *c == '\t')) {
whitespace_found = 1;
Expand Down Expand Up @@ -233,12 +233,12 @@ static size_t find_trailer_start(const char *buf, size_t len)
}

find_separator(&separator_pos, bol, TRAILER_SEPARATORS);
if (separator_pos >= 1 && !isspace(bol[0])) {
if (separator_pos >= 1 && !isspace((unsigned char)bol[0])) {
trailer_lines++;
possible_continuation_lines = 0;
if (recognized_prefix)
continue;
} else if (isspace(bol[0]))
} else if (isspace((unsigned char)bol[0]))
possible_continuation_lines++;
else {
non_trailer_lines++;
Expand Down Expand Up @@ -323,7 +323,7 @@ int git_message_trailers(git_message_trailer_array *trailer_arr, const char *mes
goto ret;
}

if (isalnum(*ptr) || *ptr == '-') {
if (isalnum((unsigned char)*ptr) || *ptr == '-') {
/* legal key character */
NEXT(S_KEY);
}
Expand Down
4 changes: 2 additions & 2 deletions src/libgit2/transports/smart_pkt.c
Original file line number Diff line number Diff line change
Expand Up @@ -535,10 +535,10 @@ static int parse_len(size_t *out, const char *line, size_t linelen)
num[PKT_LEN_SIZE] = '\0';

for (i = 0; i < PKT_LEN_SIZE; ++i) {
if (!isxdigit(num[i])) {
if (!isxdigit((unsigned char)num[i])) {
/* Make sure there are no special characters before passing to error message */
for (k = 0; k < PKT_LEN_SIZE; ++k) {
if(!isprint(num[k])) {
if(!isprint((unsigned char)num[k])) {
num[k] = '.';
}
}
Expand Down
18 changes: 9 additions & 9 deletions src/util/date.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,9 @@ static size_t match_string(const char *date, const char *str)
for (i = 0; *date; date++, str++, i++) {
if (*date == *str)
continue;
if (toupper(*date) == toupper(*str))
if (toupper((unsigned char)*date) == toupper((unsigned char)*str))
continue;
if (!isalnum(*date))
if (!isalnum((unsigned char)*date))
break;
return 0;
}
Expand All @@ -143,7 +143,7 @@ static int skip_alpha(const char *date)
int i = 0;
do {
i++;
} while (isalpha(date[i]));
} while (isalpha((unsigned char)date[i]));
return i;
}

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

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

/* Time? Date? */
Expand Down Expand Up @@ -349,7 +349,7 @@ static size_t match_digit(const char *date, struct tm *tm, int *offset, int *tm_
case '.':
case '/':
case '-':
if (isdigit(end[1])) {
if (isdigit((unsigned char)end[1])) {
size_t match = match_multi_number(num, *end, date, end, tm);
if (match)
return match;
Expand All @@ -364,7 +364,7 @@ static size_t match_digit(const char *date, struct tm *tm, int *offset, int *tm_
n = 0;
do {
n++;
} while (isdigit(date[n]));
} while (isdigit((unsigned char)date[n]));

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

if (!match) {
Expand Down Expand Up @@ -682,7 +682,7 @@ static const char *approxidate_alpha(const char *date, struct tm *tm, struct tm
const char *end = date;
int i;

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

for (i = 0; i < 12; i++) {
Expand Down Expand Up @@ -783,7 +783,7 @@ static const char *approxidate_digit(const char *date, struct tm *tm, int *num)
case '.':
case '/':
case '-':
if (isdigit(end[1])) {
if (isdigit((unsigned char)end[1])) {
size_t match = match_multi_number(number, *end, date, end, tm);
if (match)
return date + match;
Expand Down
4 changes: 2 additions & 2 deletions src/util/str.c
Original file line number Diff line number Diff line change
Expand Up @@ -485,8 +485,8 @@ int git_str_decode_percent(
for (str_pos = 0; str_pos < str_len; buf->size++, str_pos++) {
if (str[str_pos] == '%' &&
str_len > str_pos + 2 &&
isxdigit(str[str_pos + 1]) &&
isxdigit(str[str_pos + 2])) {
isxdigit((unsigned char)str[str_pos + 1]) &&
isxdigit((unsigned char)str[str_pos + 2])) {
buf->ptr[buf->size] = (HEX_DECODE(str[str_pos + 1]) << 4) +
HEX_DECODE(str[str_pos + 2]);
str_pos += 2;
Expand Down
2 changes: 1 addition & 1 deletion src/util/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ GIT_INLINE(int) git__tolower(int c)
return (c >= 'A' && c <= 'Z') ? (c + 32) : c;
}
#else
# define git__tolower(a) tolower(a)
# define git__tolower(a) tolower((unsigned char)(a))
#endif

extern size_t git__linenlen(const char *buffer, size_t buffer_len);
Expand Down
2 changes: 1 addition & 1 deletion tests/libgit2/repo/open.c
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ static void unposix_path(git_str *path)
src = tgt = path->ptr;

/* convert "/d/..." to "d:\..." */
if (src[0] == '/' && isalpha(src[1]) && src[2] == '/') {
if (src[0] == '/' && isalpha((unsigned char)src[1]) && src[2] == '/') {
*tgt++ = src[1];
*tgt++ = ':';
*tgt++ = '\\';
Expand Down
0