Description
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 Impact
Other, Integrity
Unexpected State, Quality Degradation
Demonstrative Examples
int i = (int) 33457.8f;$floatVal = 1.8345;$intVal = 3;$result = (int)$floatVal + $intVal;unsigned int readdata () {int amount = 0;...if (result == ERROR)amount = -1;...return amount;}unsigned int readdata () {int amount = 0;...amount = accessmainframe();...return amount;}Mitigations & Prevention
Avoid making conversion between numeric types. Always check for the allowed ranges.
Detection Methods
- Automated Static Analysis High — Automated static analysis, commonly referred to as Static Application Security Testing (SAST), can find some instances of this weakness by analyzing source code (or binary/compiled code) without having to execute it. Typically, this is done by building a model of data flow and control flow, then sea
Real-World CVE Examples
| CVE ID | Description |
|---|---|
| CVE-2022-2639 | Chain: integer coercion error (CWE-192) prevents a return value from indicating an error, leading to out-of-bounds write (CWE-787) |
| CVE-2021-43537 | Chain: in a web browser, an unsigned 64-bit integer is forcibly cast to a 32-bit integer (CWE-681) and potentially leading to an integer overflow (CWE-190). If an integer overflow occurs, this can cau |
| CVE-2007-4268 | Chain: integer signedness error (CWE-195) passes signed comparison, leading to heap overflow (CWE-122) |
| CVE-2007-4988 | Chain: signed short width value in image processor is sign extended during conversion to unsigned int, which leads to integer overflow and heap-based buffer overflow. |
| CVE-2009-0231 | Integer truncation of length value leads to heap-based buffer overflow. |
| CVE-2008-3282 | Size of a particular type changes for 64-bit platforms, leading to an integer truncation in document processor causes incorrect index to be generated. |
Related Weaknesses
Taxonomy Mappings
- CERT C Secure Coding: FLP34-C — Ensure that floating point conversions are within range of the new type
- CERT C Secure Coding: INT15-C — Use intmax_t or uintmax_t for formatted IO on programmer-defined integer types
- CERT C Secure Coding: INT31-C — Ensure that integer conversions do not result in lost or misinterpreted data
- CERT C Secure Coding: INT35-C — Evaluate integer expressions in a larger size before comparing or assigning to that size
- The CERT Oracle Secure Coding Standard for Java (2011): NUM12-J — Ensure conversions of numeric types to narrower types do not result in lost or misinterpreted data
- Software Fault Patterns: SFP1 — Glitch in computation
- OMG ASCSM: ASCSM-CWE-681 —
Frequently Asked Questions
What is CWE-681?
CWE-681 (Incorrect Conversion between Numeric Types) is a software weakness identified by MITRE's Common Weakness Enumeration. It is classified as a Base-level weakness. 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 cont...
How can CWE-681 be exploited?
Attackers can exploit CWE-681 (Incorrect Conversion between Numeric Types) to unexpected state, quality degradation. This weakness is typically introduced during the Implementation phase of software development.
How do I prevent CWE-681?
Key mitigations include: Avoid making conversion between numeric types. Always check for the allowed ranges.
What is the severity of CWE-681?
CWE-681 is classified as a Base-level weakness (Medium abstraction). It has been observed in 6 real-world CVEs.