Skip to content

Commit

Permalink
Use unsigned chars for ctype macros.
Browse files Browse the repository at this point in the history
On linux these tables are 384 wide, to accommodate -128 to +255.
However this is an implementation defined security improvement and the
specification is such that the parameter to e.g. isalpha should be
unsigned.

Detected with the stock windows gcc install (mingw-w64 didn't complain).
  • Loading branch information
jkbonfield committed Jul 2, 2024
1 parent 5a2627e commit 29b69fb
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions htscodecs/tokenise_name3.c
Original file line number Diff line number Diff line change
Expand Up @@ -765,19 +765,20 @@ static int encode_name(name_context *ctx, char *name, int len, int mode) {
}

/* Determine data type of this segment */
if (isalpha(name[i])) {
if (isalpha((uint8_t)name[i])) {
int s = i+1;
// int S = i+1;

// // FIXME: try which of these is best. alnum is good sometimes.
// while (s < len && isalpha(name[s]))
while (s < len && (isalpha(name[s]) || ispunct(name[s])))
// while (s < len && isalpha((uint8_t)name[s]))
while (s < len && (isalpha((uint8_t)name[s]) ||
ispunct((uint8_t)name[s])))
// while (s < len && name[s] != ':')
// while (s < len && !isdigit(name[s]) && name[s] != ':')
// while (s < len && !isdigit((uint8_t)name[s]) && name[s] != ':')
s++;

// if (!is_fixed) {
// while (S < len && isalnum(name[S]))
// while (S < len && isalnum((uint8_t)name[S]))
// S++;
// if (s < S)
// s = S;
Expand Down Expand Up @@ -821,7 +822,7 @@ static int encode_name(name_context *ctx, char *name, int len, int mode) {
uint32_t v = 0;
int d = 0;

while (s < len && isdigit(name[s]) && s-i < 9) {
while (s < len && isdigit((uint8_t)name[s]) && s-i < 9) {
v = v*10 + name[s] - '0';
//putchar(name[s]);
s++;
Expand Down Expand Up @@ -866,13 +867,13 @@ static int encode_name(name_context *ctx, char *name, int len, int mode) {
ctx->lc[cnum].last[ntok].token_type = N_DIGITS0;

i = s-1;
} else if (isdigit(name[i])) {
} else if (isdigit((uint8_t)name[i])) {
// digits starting 1-9; encode value
uint32_t s = i;
uint32_t v = 0;
int d = 0;

while (s < len && isdigit(name[s]) && s-i < 9) {
while (s < len && isdigit((uint8_t)name[s]) && s-i < 9) {
v = v*10 + name[s] - '0';
//putchar(name[s]);
s++;
Expand Down Expand Up @@ -938,7 +939,7 @@ static int encode_name(name_context *ctx, char *name, int len, int mode) {
i = s-1;
} else {
n_char:
//if (!isalpha(name[i])) putchar(name[i]);
//if (!isalpha((uint8_t)name[i])) putchar(name[i]);
if (pnum < cnum && ntok < ctx->lc[pnum].last_ntok && ctx->lc[pnum].last[ntok].token_type == N_CHAR) {
if (name[i] == ctx->lc[pnum].last[ntok].token_int) {
#ifdef ENC_DEBUG
Expand Down

0 comments on commit 29b69fb

Please sign in to comment.