Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
bgColor#ccccff
langc
#include <limits.h>
#include <stddef.h>
#include <inttypes.h>
 
extern size_t popcount(uintmax_t);
#define PRECISION(x) popcount(x)
 
void func(signed long si_a, signed long si_b) {
  signed long result;
  if ((si_a < 0) || (si_b < 0) ||
      (si_b >= PRECISION(ULONG_MAX)) ||
      (si_a > (LONG_MAX >> si_b))) {
    /* Handle error */
  } else {
    result = si_a << si_b;
  }
  /* ... */
}

 


Noncompliant Code Example (Right Shift)

...

Although shifting a negative number of bits or shifting a number of bits greater than or equal to the width of the promoted left operand is undefined behavior in C, the risk is generally low because processors frequently reduce the shift amount modulo the width of the type.

Rule

Severity

Likelihood

Remediation Cost

Priority

Level

INT34-C

Low

Unlikely

Medium

P2

L3

Automated Detection

Tool

Version

Checker

Description

Astrée
Include Page
Astrée_V
Astrée_V

precision-shift-width
precision-shift-width-constant

Fully checked
Axivion Bauhaus Suite

Include Page
Axivion Bauhaus Suite_V
Axivion Bauhaus Suite_V

CertC-INT34Can detect shifts by a negative or an excessive number of bits and right shifts on negative values.
CodeSonar
Include Page
CodeSonar_V
CodeSonar_V

LANG.ARITH.BIGSHIFT
LANG.ARITH.NEGSHIFT

Shift amount exceeds bit width
Negative shift amount

Compass/ROSE

 

 



Can detect violations of this rule. Unsigned operands are detected when checking for INT13-C. Use bitwise operators only on unsigned operands

Coverity
Include Page
Coverity_V
Coverity_V

BAD_SHIFT

Implemented
Cppcheck
Include Page
Cppcheck_V
Cppcheck_V
shiftNegative, shiftTooManyBits

Context sensitive analysis
Warns whenever Cppcheck sees a negative shift for a POD expression
(The warning for shifting too many bits is written only if Cppcheck has sufficient type information and you use --platform to specify the sizes of the standard types.)

Cppcheck Premium

Include Page
Cppcheck Premium_V
Cppcheck Premium_V

shiftNegative, shiftTooManyBits

premium-cert-int34-c

Context sensitive analysis
Warns whenever Cppcheck sees a negative shift for a POD expression
(The warning for shifting too many bits is written only if Cppcheck has sufficient type information and you use --platform to specify the sizes of the standard types.)
ECLAIR
Include Page
ECLAIR_V
ECLAIR_V
CC2.INT34Partially implemented
Fortify SCA

5.0

 

Helix QAC

Include Page
Helix QAC_V
Helix QAC_V

C0499, C2790, 

C++2790,  C++3003

DF2791, DF2792, DF2793


Klocwork

Include Page
Klocwork_V
Klocwork_V

MISRA.SHIFT.RANGE.2012

Can detect violations of this rule with CERT C Rule Pack


LDRA tool suite
Include Page
LDRA_V
LDRA_V

51 S, 403 S, 479 S

Partially implemented

Parasoft C/C++test
9.5MISRA-038
Include Page
Parasoft_V
Parasoft_V
CERT_C-INT34-a

Avoid incorrect shift operations

Polyspace Bug Finder

Include Page
Polyspace Bug Finder_V

 

Polyspace Bug Finder

R2016a

_V

CERT C: Rule INT34-C


Checks for:

  • Shift of a negative value
,
  • Shift operation overflow

Shift operator on negative value

Overflow from shifting operation

PRQA QA-C

Rule partially covered.

PVS-Studio

Include Page

PRQA QA

PVS-

C

Studio_

v

V

PRQA QA

PVS-

C

Studio_

v

V

V610
0499

RuleChecker
2790
2791 (D)
2792 (A)
2793 (S)Partially implementedCppcheck Include PageCppcheck_VCppcheck_VshiftNegative, shiftTooManyBitsContext sensitive analysis
Warns whenever Cppcheck sees a negative shift for a POD expression
(The warning for shifting too many bits is written only if Cppcheck has sufficient type information and you use --platform to specify the sizes of the standard types.)

Include Page
RuleChecker_V
RuleChecker_V

precision-shift-width-constant

Partially checked
TrustInSoft Analyzer

Include Page
TrustInSoft Analyzer_V
TrustInSoft Analyzer_V

shift

Exhaustively verified (see one compliant and one non-compliant example).

Related Vulnerabilities

Search for vulnerabilities resulting from the violation of this rule on the CERT website.

Related Guidelines

Key here (explains table format and definitions)

SEI

Taxonomy

Taxonomy item

Relationship

CERT C
Coding Standard
INT13-C. Use bitwise operators only on unsigned operandsPrior to 2018-01-12: CERT: Unspecified Relationship
CERT CINT35-C. Use correct integer precisionsPrior to 2018-01-12: CERT: Unspecified Relationship
CERT CINT32-C. Ensure that operations on signed integers do not result in overflowPrior to 2018-01-12: CERT: Unspecified Relationship
ISO/IEC TR 24772:2013Arithmetic Wrap-Around Error [FIF]Prior to 2018-01-12: CERT: Unspecified Relationship
CWE 2.11CWE-6822017-07-07: CERT: Rule subset of CWE
CWE 2.11CWE-7582017-07-07: CERT: Rule subset of CWE

CERT-CWE Mapping Notes

Key here for mapping notes

CWE-758 and INT34-C

Independent( INT34-C, INT36-C, MEM30-C, MSC37-C, FLP32-C, EXP33-C, EXP30-C, ERR34-C, ARR32-C)

CWE-758 = Union( INT34-C, list) where list =


  • Undefined behavior that results from anything other than incorrect bit shifting


CWE-682 and INT34-C

Independent( INT34-C, FLP32-C, INT33-C) CWE-682 = Union( INT34-C, list) where list =


  • Incorrect calculations that do not involve out-of-range bit shifts


Bibliography

[C99 Rationale 2003]6.5.7, "Bitwise Shift Operators"
[Dowd 2006]Chapter 6, "C Language Issues"
[Seacord 2013b]Chapter 5, "Integer Security"
[Viega 2005]Section 5.2.7, "Integer Overflow"

...


...

Image Modified Image Modified Image Modified