CERT-STR34-C
In this section:
Synopsis
Cast characters to unsigned char before converting to larger integer sizes.
Enabled by default
Yes
Severity/Certainty
Medium/High

Full description
Signed character data must be converted to unsigned char before being assigned or converted to a larger signed type. This rule applies to both signed char and (plain) char characters on implementations where char is defined to have the same range, representation, and behaviors as signed char.
Coding standards
- CERT STR34-C
Cast characters to unsigned char before converting to larger integer sizes
Code examples
The following code example fails the check and will give a warning:
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
static int yy_string_get(void) {
register char *c_str;
register int c;
/* c_str = bash_input.location.string; */
c = EOF;
/* If the string doesn't exist or is empty, EOF found */
if (c_str && *c_str) {
c = *c_str++;
/* bash_input.location.string = c_str; */
}
return (c);
}
The following code example passes the check and will not give a warning about this issue:
static int yy_string_get(void) {
register char *c_str;
register int c;
c_str = bash_input.location.string;
c = EOF;
/* If the string doesn't exist or is empty, EOF found */
if (c_str && *c_str) {
/* Cast to unsigned type */
c = (unsigned char)*c_str++;
bash_input.location.string = c_str;
}
return (c);
}