CWE
Home > CWE List > CWE- Individual Dictionary Definition (1.1)  
Search by ID:

CWE-681: Incorrect Conversion between Numeric Types

Individual Definition in a New Window
Incorrect Conversion between Numeric Types
Status: Draft
Weakness ID: 681 (Weakness Base)
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.

Potential Mitigations

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

Demonstrative Examples

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

Java Example:
int i = (int) 33457.8f
Relationships
NatureTypeIDNameView(s) this relationship pertains toView(s)
ChildOfWeakness ClassWeakness ClassWeakness Class704Incorrect Type Conversion or Cast
Research Concepts (primary)1000
CanPrecedeWeakness ClassWeakness ClassWeakness Class682Incorrect Calculation
Research Concepts (primary)1000
ChildOfCategoryCategory189Numeric Errors
Development Concepts699
ChildOfCategoryCategory136Type Errors
Development Concepts (primary)699
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
ParentOfCategoryCategory192Integer Coercion Error
Research Concepts (primary)1000
ParentOfWeakness BaseWeakness BaseWeakness Base194Unexpected Sign Extension
Development Concepts (primary)699
Research Concepts (primary)1000
ParentOfWeakness VariantWeakness VariantWeakness Variant195Signed to Unsigned Conversion Error
Development Concepts (primary)699
Research Concepts (primary)1000
ParentOfWeakness VariantWeakness VariantWeakness Variant196Unsigned to Signed Conversion Error
Development Concepts (primary)699
Research Concepts (primary)1000
ParentOfWeakness BaseWeakness BaseWeakness Base197Numeric Truncation Error
Development Concepts (primary)699
Research Concepts (primary)1000
Taxonomy Mappings
Mapped Taxonomy NameNode IDMapped 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
Time of Introduction
* Implementation
Content History
Modifications
Sean Eidemiller. Cigital. 2008-07-01. (External)
added/updated demonstrative examples
Eric Dalci. Cigital. 2008-07-01. (External)
updated Potential_Mitigations, Time_of_Introduction
CWE Content Team. MITRE. 2008-09-08. (Internal)
updated Relationships
CWE Content Team. MITRE. 2008-11-24. (Internal)
updated Description, Relationships, Taxonomy_Mappings
Page Last Updated: November 24, 2008