Start here

Home
About Klocwork
What's new
Fixed issues
Release notes
Installation

Reference

C/C++ checkers
Java checkers
C# checkers
MISRA C 2004 checkers
MISRA C++ 2008 checkers
MISRA C 2012 checkers
MISRA C 2012 checkers with Amendment 1
Commands
Metrics
Troubleshooting
Reference

Product components

C/C++ Integration build analysis
Java Integration build analysis
Desktop analysis
Refactoring
Klocwork Static Code Analysis
Klocwork Code Review
Structure101
Tuning
Custom checkers

Coding environments

Visual Studio
Eclipse for C/C++
Eclipse for Java
IntelliJ IDEA
Other

Administration

Project configuration
Build configuration
Administration
Analysis performance
Server performance
Security/permissions
Licensing
Klocwork Static Code Analysis Web API
Klocwork Code Review Web API

Community

View help online
Visit RogueWave.com
Klocwork Support
Rogue Wave Videos

Legal

Legal information

PORTING.UNSIGNEDCHAR.OVERFLOW.FALSE

Relational expression may be always false

The PORTING checkers identify code that might rely on specific implementation details in different compilers. The PORTING.UNSIGNEDCHAR.OVERFLOW.FALSE checker detects situations in which a relational expression may be always false, depending on 'char' type signedness.

Vulnerability and risk

The 'char' data type isn't precisely defined in the C standard, so an instance may or may not be considered to be signed. Some compilers allow the sign of 'char' to be switched using compiler options, but best practice is for developers to write unambiguous code at all times to avoid problems in porting code.

Mitigation and prevention

Always specify whether or not the 'char' type is signed. This is best done by a using a typedef or #define definition that is then rigorously used everywhere.

Vulnerable code example

1   int has_utf8_byte_order_mark(char *s) {
2     return s[0] == 0xEF && s[1] == 0xBB && s[2] == 0xBF;  /* PORTING.UNSIGNEDCHAR.OVERFLOW.FALSE */
3   }

4   int main() {
5     char *s = "\xEF\xBB\xBFHello, World!\n";
6     if (has_utf8_byte_order_mark(s)) {
7       printf("unicode\n");
8     } else {
9       printf("not unicode\n");
10    }
11    return 0;
12  }

In this example, the code would detect a UTF-8 byte-order mark only when char is unsigned.

Fixed code example 1

1   int has_utf8_byte_order_mark(char *s) {
2     return s[0] == '\xEF' && s[1] == '\xBB' && s[2] == '\xBF';
3   }

4   int main() {
5     char *s = "\xEF\xBB\xBFHello, World!\n";
6     if (has_utf8_byte_order_mark(s)) {
7       printf("unicode\n");
8     } else {
9       printf("not unicode\n");
10    }
11    return 0;
12  }

In this example, the fixed code uses char literals in the comparison, rather than integer literals.

Fixed code example 2

1   int has_utf8_byte_order_mark(unsigned char *s) {
2     return s[0] == 0xEF && s[1] == 0xBB && s[2] == 0xBF;  
3   }

4   int main() {
5     char *s = "\xEF\xBB\xBFHello, World!\n";
6     if (has_utf8_byte_order_mark(s)) {
7       printf("unicode\n");
8     } else {
9       printf("not unicode\n");
10    }
11    return 0;
12  }

In this approach, unsigned char is used instead of char.