Versions Compared

Key

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

Content by Label
showLabelsfalse
maxResults99
label+int,+rule,-void
showSpacefalse
sorttitle
spacecom.atlassian.confluence.content.render.xhtml.model.resource.identifiers.SpaceResourceIdentifier@3bbaf8c
cqllabel = "rule" and label = "int" and label != "void" and space = currentSpace()

Info

Information for Editors
To have a new guideline automatically listed above be sure to label it int and rule.

Risk Assessment Summary

Rule

Severity

Likelihood

Detectable

Repairable

Priority

Level

INT30-CHighLikelyNoNo

P9

L2

INT31-CHighProbableNoYes

P12

L1

INT32-CHighLikelyNoYes

P18

L1

INT33-CLowLikelyNoYes

P6

L2

INT34-CLowUnlikelyNoYes

P2

L3

INT35-CLowUnlikelyNoNo

P1

L3

INT36-CLowProbableYesNo

P4

L3

Related Rules and Recommendations

Navigation Map
integer
integer
cellWidth700
wrapAfter1
cellHeight15

...

Image Added Image Added Image Added

The runtime behavior of integers in C is not always well-understood by programmers, leading to exploitable vulnerabilities.  For example, assume the following code is compiled and executed on IA-32:

Code Block

signed char sc = SCHAR_MAX;
unsigned char uc = UCHAR_MAX;
signed long long sll = sc + uc;

Both the signed char sc and the unsigned char uc are subject to integer promotions in this example. Because all values of the original types can be represented as int, both values are automatically converted to int as part of the integer promotions. Further conversions are possible, if the types of these variables are not equivalent as a result of the "usual arithmetic conversions".  The actual addition operation in this case takes place between the two 32-bit int values. This operation is not influenced by the resulting value is stored in a signed long long integer. The 32-bit value resulting from the addition is simply sign-extended to 64-bits after the addition operation has concluded.

Assuming that the precision of signed char is 7 bits, and the precision of unsigned char is 8 bits, this operation is perfectly safe. However, if the compiler represents the signed char and unsigned char types using 31 and 32 bit precision (respectively), the variable uc would need be converted to unsigned int instead of signed int. As a result of the usual arithmetic conversions, the signed int is converted to unsigned and the addition takes place between the two unsigned int values. Also, because uc is equal to UCHAR_MAX which is equal to UINT_MAX in this example, the addition will result in an overflow. The resulting value is then zero-extended to fit into the 64-bit storage allocated by sll.

Recommendations

Use size_t for all integer values representing the size of an object

Rules

Provide adequate range checking

All Integer operations must be guaranteed not to result in overflow, truncation, or sign error

Do not make assumptions about the type of a bit-field when used in an expression

Guarantee that integer conversions do not result in lost or misinterpreted data

Guarantee that integer operations do not result in an overflow