Skip to content

Commit

Permalink
Avoid compilation warnings from ctype(3) helpers
Browse files Browse the repository at this point in the history
Characters should be casted as unsigned before use in functions from
<ctype.h>. Otherwise the compiler treats 8-bit characters (eg UTF-8) as
negative values (since it expects signed integers) and they no longer
match the comparison tables. Worse, the character 0xff gets interpreted
as -1 (like EOF). In turn, it helps to explicitly cast the result as a
signed integer, since this is what is expected. Characters in the range
0x80-0xff do keep their original values.

See the manual page for ctype(3) for more details (eg from NetBSD)
  • Loading branch information
khorben committed Jun 12, 2018
1 parent 42be3dc commit 5228fe8
Show file tree
Hide file tree
Showing 10 changed files with 25 additions and 25 deletions.
4 changes: 2 additions & 2 deletions client/config_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ is_rc_section(const char* line, uint16_t line_size, char* rc_section, uint16_t r

ndx = buf;

while(isspace(*ndx))
while(isspace((int)(unsigned char)*ndx))
ndx++;

if(*ndx == '[')
Expand Down Expand Up @@ -761,7 +761,7 @@ parse_time_offset(const char *offset_str, int *offset)

j=0;
for (i=0; i < os_len; i++) {
if (isdigit(offset_str[i])) {
if (isdigit((int)(unsigned char)offset_str[i])) {
offset_digits[j] = offset_str[i];
j++;
if(j >= MAX_TIME_STR_LEN)
Expand Down
4 changes: 2 additions & 2 deletions client/fwknop.c
Original file line number Diff line number Diff line change
Expand Up @@ -749,7 +749,7 @@ set_nat_access(fko_ctx_t ctx, fko_cli_options_t *options, const char * const acc
}
ndx++;

while(*ndx != '\0' && isdigit(*ndx) && i < MAX_PORT_STR_LEN)
while(*ndx != '\0' && isdigit((int)(unsigned char)*ndx) && i < MAX_PORT_STR_LEN)
{
tmp_access_port[i] = *ndx;
ndx++;
Expand Down Expand Up @@ -793,7 +793,7 @@ set_nat_access(fko_ctx_t ctx, fko_cli_options_t *options, const char * const acc

{
tmp_nat_port[i] = *ndx;
if ((i > MAX_PORT_STR_LEN) || (!isdigit(*ndx)))
if ((i > MAX_PORT_STR_LEN) || (!isdigit((int)(unsigned char)*ndx)))
{
log_msg(LOG_VERBOSITY_ERROR, "[*] Invalid port value in -N arg.");
return FKO_ERROR_INVALID_DATA;
Expand Down
4 changes: 2 additions & 2 deletions client/http_resolve_host.c
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ try_url(struct url *url, fko_cli_options_t *options)
* (possibly followed by whitespace or other not-digit value).
*/
for(i=0; i<MAX_IPV4_STR_LEN; i++) {
if(! isdigit(*(ndx+i)) && *(ndx+i) != '.')
if(! isdigit((int)(unsigned char)*(ndx+i)) && *(ndx+i) != '.')
break;
}

Expand Down Expand Up @@ -497,7 +497,7 @@ resolve_ip_https(fko_cli_options_t *options)
{
ndx = resp;
for(i=0; i<MAX_IPV4_STR_LEN; i++) {
if(! isdigit(*(ndx+i)) && *(ndx+i) != '.')
if(! isdigit((int)(unsigned char)*(ndx+i)) && *(ndx+i) != '.')
break;
}
*(ndx+i) = '\0';
Expand Down
10 changes: 5 additions & 5 deletions common/fko_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ is_valid_ipv4_addr(const char * const ip_str, const int len)

if(*ndx == '.')
dot_ctr++;
else if(isdigit(*ndx) == 0)
else if(isdigit((int)(unsigned char)*ndx) == 0)
{
res = 0;
break;
Expand Down Expand Up @@ -196,17 +196,17 @@ is_valid_hostname(const char * const hostname_str, const int len)

if (label_size == 0) //More restrictions on first character of a label
{
if (!isalnum(*ndx))
if (!isalnum((int)(unsigned char)*ndx))
return 0;
}
else if (!(isalnum(*ndx) | (*ndx == '.') | (*ndx == '-')))
else if (!(isalnum((int)(unsigned char)*ndx) | (*ndx == '.') | (*ndx == '-')))
return 0;

if (*ndx == '.')
{
if (label_size > 63)
return 0;
if (!isalnum(*(ndx-1))) //checks that previous character was not a . or -
if (!isalnum((int)(unsigned char)*(ndx-1))) //checks that previous character was not a . or -
return 0;

label_size = 0;
Expand Down Expand Up @@ -776,7 +776,7 @@ strtoargv(const char * const args_str, char **argv_new, int *argc_new)

for (i=0; i < (int)strlen(args_str); i++)
{
if (!isspace(args_str[i]))
if (!isspace((int)(unsigned char)args_str[i]))
{
arg_tmp[current_arg_ctr] = args_str[i];
current_arg_ctr++;
Expand Down
2 changes: 1 addition & 1 deletion lib/fko_decode.c
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ fko_decode_spa_data(fko_ctx_t ctx)
/* Make sure there are no non-ascii printable chars
*/
for (i=0; i < (int)strnlen(ctx->encoded_msg, MAX_SPA_ENCODED_MSG_SIZE); i++)
if(isprint(ctx->encoded_msg[i]) == 0)
if(isprint((int)(unsigned char)ctx->encoded_msg[i]) == 0)
return(FKO_ERROR_INVALID_DATA_DECODE_NON_ASCII);

/* Make sure there are enough fields in the SPA packet
Expand Down
6 changes: 3 additions & 3 deletions lib/fko_message.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ have_allow_ip(const char *msg)
}
if(*ndx == '.')
dot_ctr++;
else if(isdigit(*ndx) == 0)
else if(isdigit((int)(unsigned char)*ndx) == 0)
{
res = FKO_ERROR_INVALID_ALLOW_IP;
break;
Expand Down Expand Up @@ -83,13 +83,13 @@ have_port(const char *msg)

/* Must have at least one digit for the port number
*/
if(isdigit(*ndx) == 0)
if(isdigit((int)(unsigned char)*ndx) == 0)
return(FKO_ERROR_INVALID_SPA_ACCESS_MSG);

while(*ndx != '\0' && *ndx != ',')
{
port_str_len++;
if((isdigit(*ndx) == 0) || (port_str_len > MAX_PORT_STR_LEN))
if((isdigit((int)(unsigned char)*ndx) == 0) || (port_str_len > MAX_PORT_STR_LEN))
return(FKO_ERROR_INVALID_SPA_ACCESS_MSG);
port_str[i] = *ndx;
ndx++;
Expand Down
2 changes: 1 addition & 1 deletion lib/fko_user.c
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ validate_username(const char *username)
*/
for (i=0; i < (int)strnlen(username, MAX_SPA_USERNAME_SIZE); i++)
{
if((isalnum(username[i]) == 0)
if((isalnum((int)(unsigned char)username[i]) == 0)
&& ((username[i] < 0x20 || username[i] > 0x7e)
/* Not allowed chars: " / \ [ ] : ; | = , + * ? < >
*/
Expand Down
12 changes: 6 additions & 6 deletions server/access.c
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ expand_acc_int_list(acc_int_list_t **ilist, char *ip)
{
/* Skip over any leading whitespace.
*/
while(isspace(*start))
while(isspace((int)(unsigned char)*start))
start++;

if(((ndx-start)+1) >= ACCESS_BUF_LEN)
Expand All @@ -542,7 +542,7 @@ expand_acc_int_list(acc_int_list_t **ilist, char *ip)

/* Skip over any leading whitespace (once again for the last in the list).
*/
while(isspace(*start))
while(isspace((int)(unsigned char)*start))
start++;

if(((ndx-start)+1) >= ACCESS_BUF_LEN)
Expand Down Expand Up @@ -716,7 +716,7 @@ expand_acc_port_list(acc_port_list_t **plist, char *plist_str)
{
/* Skip over any leading whitespace.
*/
while(isspace(*start))
while(isspace((int)(unsigned char)*start))
start++;

if(((ndx-start)+1) >= ACCESS_BUF_LEN)
Expand All @@ -733,7 +733,7 @@ expand_acc_port_list(acc_port_list_t **plist, char *plist_str)

/* Skip over any leading whitespace (once again for the last in the list).
*/
while(isspace(*start))
while(isspace((int)(unsigned char)*start))
start++;

if(((ndx-start)+1) >= ACCESS_BUF_LEN)
Expand Down Expand Up @@ -763,7 +763,7 @@ expand_acc_string_list(acc_string_list_t **stlist, char *stlist_str)
{
/* Skip over any leading whitespace.
*/
while(isspace(*start))
while(isspace((int)(unsigned char)*start))
start++;

if(((ndx-start)+1) >= MAX_LINE_LEN)
Expand All @@ -779,7 +779,7 @@ expand_acc_string_list(acc_string_list_t **stlist, char *stlist_str)

/* Skip over any leading whitespace (once again for the last in the list).
*/
while(isspace(*start))
while(isspace((int)(unsigned char)*start))
start++;

if(((ndx-start)+1) >= MAX_LINE_LEN)
Expand Down
4 changes: 2 additions & 2 deletions server/incoming_spa.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ preprocess_spa_data(const fko_srv_options_t *opts, spa_pkt_info_t *spa_pkt, spa_
xff += 17;

for (i = 0; *xff != '\0'; i++)
if (isspace(*xff))
if (isspace((int)(unsigned char)*xff))
*xff = '\0';
else
xff++;
Expand All @@ -135,7 +135,7 @@ preprocess_spa_data(const fko_srv_options_t *opts, spa_pkt_info_t *spa_pkt, spa_

for(i=0; i<pkt_data_len; i++)
{
if(isspace(*ndx)) /* The first space marks the end of the req */
if(isspace((int)(unsigned char)*ndx)) /* The first space marks the end of the req */
{
*ndx = '\0';
break;
Expand Down
2 changes: 1 addition & 1 deletion server/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ is_digits(const char * const str)
{
for (i=0; i<strlen(str); i++)
{
if(!isdigit(str[i]))
if(!isdigit((int)(unsigned char)str[i]))
return 0;
i++;
}
Expand Down

0 comments on commit 5228fe8

Please sign in to comment.