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.MACRO.NUMTYPE

Macro describing a builtin numeric type

The PORTING checkers identify code that might rely on specific implementation details in different compilers. The PORTING.MACRO.NUMTYPE checker detects situations in which a macro describing a built-in numeric type is used.

Vulnerability and risk

It is typical for compilers to provide "courtesy" numeric limits (normally in limits.h) for their particular data model and capabilities. Using these values across platforms is dangerous, as the limits provided by another compiler can be different, and of entirely different widths (in terms of bit-wise representation). This practice can cause stack overflow, numeric overflow or underflow, operational runtime budget exhaustion, and many other problems.

Mitigation and prevention

Define your own limits so you know exactly what you're dealing with, rather than relying on an abstract number provided by the compiler on a particular platform.

Vulnerable code example


1   extern void do_something_that_takes_a_while();

2   void foo()
3   {
4      for( int i = 0; i < INT_MAX; i++ )
5        do_something_that_takes_a_while();
5   }

In moving this code between two platforms (such as 16-bit and 32-bit), the operational budget for this function can expand exponentially.

Fixed code example

1   #define MAX_OPS 65535

2   extern void do_something_that_takes_a_while();

3   void foo()
4   {
5      for( int i = 0; i < MAX_OPS; i++ )
6        do_something_that_takes_a_while();
7   }

This code defines its own limit, ensuring that the same numeric value is used regardless of any move between platforms.