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

MISRA.ETYPE.COMP.CAST.EXPL.WIDER.2012

MISRA C 2012 Rule 10.8: The value of a composite expression shall not be cast to a different essential type category or a wider essential type

Category: Required

Analysis: Decidable, Single Translation Unit

Applies to: C90, C99

Rationale

The rationale is described in the introduction on composite operators and expressions (see Section 8.10.3).

Casting to a wider type is not permitted as the result may vary between implementations. Consider the following:
( uint32_t ) ( u16a + u16b );

On a 16-bit machine the addition will be performed in 16 bits with the result wrapping modulo-2 before it is cast to 32 bits. However, on a 32-bit machine the addition will take place in 32 bits and would preserve high-order bits that would have been lost on a 16-bit machine.

Casting to a narrower type with the same essential type category is acceptable as the explicit truncation of the result always leads to the same loss of information.

Example

( uint16_t ) ( u32a + u32b )   /* Compliant                            */
( uint16_t ) ( s32a + s32b )   /* Non-compliant - different essential
                                * type category                        */
( uint16_t ) s32a              /* Compliant - s32a is not composite    */
( uint32_t ) ( u16a + u16b )   /* Non-compliant - cast to wider
                                * essential type                       */