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.BYTEORDER.SIZE

Use of an incompatible type with a network conversion macro

The PORTING checkers identify code that might rely on specific implementation details in different compilers. The PORTING.BYTEORDER.SIZE checker detects situations in which an incompatible type is used with network byte-order macros ntoh*() and hton*().

Vulnerability and risk

Based on the underlying ANSI semantics for type promotion and demotion, the common data transformation functions can accept inappropriate types as arguments, resulting in potentially incorrect transformations. In the worst case, this could cause random data to be transformed as part of the byte-manipulation process.

Mitigation and prevention

Always use the appropriate ntoh*() or hton*() variant when code is transforming data to or from a network.

Vulnerable code example

1   unsigned short foo(int socket)
2   {
3     int len;
4     unsigned short val = 0;

5       len = read(socket, &val, sizeof(unsigned short));
6       if( len == sizeof(unsigned short) )
7           val = ntohl(val);     // PORTING.BYTEORDER.SIZE
8       return val;
9   }

The checker produces a warning at the line 7 because the size of the type being passed (unsigned short) is different from that expected by the transformation function, ntohl().

Fixed code example

1   unsigned short foo(int socket)
2   {
3     int len;
4     unsigned short val = 0;

5       len = read(socket, &val, sizeof(unsigned short));
6       if( len == sizeof(unsigned short) )
7           val = ntohs(val);
8       return val;
9   }

The fixed example uses a size-appropriate macro for the transformation, ensuring that the expected bytes are swapped.