Incorrect Conversion between Numeric Types
Weakness ID: 681 (Weakness Base)Status: Draft
+ Description

Description Summary

When converting from one data type to another, such as long to integer, data can be omitted or translated in a way that produces unexpected values. If the resulting values are used in a sensitive context, then dangerous behaviors may occur.
+ Time of Introduction
  • Implementation
+ Applicable Platforms

Languages

Language-Independent

+ Likelihood of Exploit

Medium to High

+ Demonstrative Examples

Example 1

In the following Java example, a float literal is cast to an integer, thus causing a loss of precision.

(Bad Code)
Example Language: Java 
int i = (int) 33457.8f
+ Potential Mitigations

Phase: Implementation

Avoid making conversion between numeric types. Always check for the allowed ranges.

+ Relationships
NatureTypeIDNameView(s) this relationship pertains toView(s)
ChildOfCategoryCategory136Type Errors
Development Concepts (primary)699
ChildOfCategoryCategory189Numeric Errors
Development Concepts699
ChildOfWeakness ClassWeakness Class704Incorrect Type Conversion or Cast
Research Concepts (primary)1000
ChildOfCategoryCategory738CERT C Secure Coding Section 04 - Integers (INT)
Weaknesses Addressed by the CERT C Secure Coding Standard (primary)734
ChildOfCategoryCategory739CERT C Secure Coding Section 05 - Floating Point (FLP)
Weaknesses Addressed by the CERT C Secure Coding Standard734
ChildOfCategoryCategory8082010 Top 25 - Weaknesses On the Cusp
Weaknesses in the 2010 CWE/SANS Top 25 Most Dangerous Programming Errors (primary)800
CanPrecedeWeakness ClassWeakness Class682Incorrect Calculation
Research Concepts1000
ParentOfCategoryCategory192Integer Coercion Error
Research Concepts (primary)1000
ParentOfWeakness BaseWeakness Base194Unexpected Sign Extension
Development Concepts (primary)699
Research Concepts (primary)1000
ParentOfWeakness VariantWeakness Variant195Signed to Unsigned Conversion Error
Development Concepts (primary)699
Research Concepts (primary)1000
ParentOfWeakness VariantWeakness Variant196Unsigned to Signed Conversion Error
Development Concepts (primary)699
Research Concepts (primary)1000
ParentOfWeakness BaseWeakness Base197Numeric Truncation Error
Development Concepts (primary)699
Research Concepts (primary)1000
+ Taxonomy Mappings
Mapped Taxonomy NameNode IDFitMapped Node Name
CERT C Secure CodingFLP33-CConvert integers to floating point for floating point operations
CERT C Secure CodingFLP34-CEnsure that floating point conversions are within range of the new type
CERT C Secure CodingINT15-CUse intmax t or uintmax t for formatted IO on programmer-defined integer types
CERT C Secure CodingINT31-CEnsure that integer conversions do not result in lost or misinterpreted data
CERT C Secure CodingINT35-CEvaluate integer expressions in a larger size before comparing or assigning to that size
+ Content History
Modifications
Modification DateModifierOrganizationSource
2008-07-01Sean EidemillerCigitalExternal
added/updated demonstrative examples
2008-07-01Eric DalciCigitalExternal
updated Potential Mitigations, Time of Introduction
2008-09-08CWE Content TeamMITREInternal
updated Relationships
2008-11-24CWE Content TeamMITREInternal
updated Description, Relationships, Taxonomy Mappings
2009-12-28CWE Content TeamMITREInternal
updated Applicable Platforms, Likelihood of Exploit, Potential Mitigations