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

CWARN.BITOP.SIZE

Operands of different size in bitwise operation

The CWARN.BITOP.SIZE checker looks for code in which bitwise operations (&=, |=, and ^=) have operands with different sizes. Both operands of a bitwise operation must normally be either 32-bit or 64-bit values, although the checker won't flag a 64-bit mask used on a 32-bit value.

Vulnerability and risk

When an unsigned 32-bit value is converted to a 64-bit type, the 32 higher bits are set to zero, which probably isn't the original design intent and can cause unexpected results.

Vulnerable code example

1  typedef unsigned int u32;
2  typedef unsigned long long u64;

3  u32 get_u32_value(void);
4  u64 get_u64_value(void);

5  void example(void) {
6    u32 mask32 = 0xff;
7    u64 mask64 = 0xff;

8    u32 value32 = get_u32_value();
9    u64 value64 = get_u64_value();

  ...

10   value64 &= ~mask32;
11  }

In this code, Klocwork flags line 10, in which a 32-bit mask is used with 64-bit data.