Integer coercion refers to a set of flaws pertaining to the type casting, extension, or truncation of primitive data types.
View on MITRESeveral flaws fall under the category of integer coercion errors. For the most part, these errors in and of themselves result only in availability and data integrity issues. However, in some circumstances, they may result in other, more complicated security related flaws, such as buffer overflow conditions.
Integer coercion often leads to undefined states of execution resulting in infinite loops or crashes.
In some cases, integer coercion errors can lead to exploitable buffer overflow conditions, resulting in the execution of arbitrary code.
Integer coercion errors result in an incorrect value being stored for the variable in question.
A language which throws exceptions on ambiguous data casts might be chosen.
Design objects and program flow such that multiple or complex casts are unnecessary
Ensure that any data type casting that you must used is entirely understood in order to reduce the plausibility of error in use.
No detection method information available for this CWE.
The following code is intended to read an incoming packet from a socket and extract one or more headers.
The code performs a check to make sure that the packet does not contain too many headers. However, numHeaders is defined as a signed int, so it could be negative. If the incoming packet specifies a value such as -3, then the malloc calculation will generate a negative number (say, -300 if each header can be a maximum of 100 bytes). When this result is provided to malloc(), it is first converted to a size_t type. This conversion then produces a large value such as 4294966996, which may cause malloc() to fail or to allocate an extremely large amount of memory (CWE-195). With the appropriate negative numbers, an attacker could trick malloc() into using a very small positive number, which then allocates a buffer that is much smaller than expected, potentially leading to a buffer overflow.
The following code reads a maximum size and performs validation on that size. It then performs a strncpy, assuming it will not exceed the boundaries of the array. While the use of "short s" is forced in this particular example, short int's are frequently used within real-world code, such as code that processes structured data.
This code first exhibits an example of CWE-839, allowing "s" to be a negative number. When the negative short "s" is converted to an unsigned integer, it becomes an extremely large positive integer. When this converted integer is used by strncpy() it will lead to a buffer overflow (CWE-119).
Chain: integer coercion error (CWE-192) prevents a return value from indicating an error, leading to out-of-bounds write (CWE-787)
View DetailsNo relationship information available for this CWE.
CWE-192: Integer Coercion Error is a Common Weakness Enumeration (CWE) entry maintained by MITRE. Integer coercion refers to a set of flaws pertaining to the type casting, extension, or truncation of primitive data types. Several flaws fall under the category of integer coercion errors. For the most part, these errors in and of themselves result only in availability and data integrity issues. However, in some circumstances, they may result in other, more complicated security related flaws, such as buffer overflow conditions.
If exploited, CWE-192 (Integer Coercion Error) it can compromise Availability, Integrity, Confidentiality and Other, leading to outcomes such as DoS: Resource Consumption (CPU), DoS: Resource Consumption (Memory), DoS: Crash, Exit, or Restart, Execute Unauthorized Code or Commands and Other.
Recommended mitigations for CWE-192 include: A language which throws exceptions on ambiguous data casts might be chosen. Design objects and program flow such that multiple or complex casts are unnecessary Ensure that any data type casting that you must used is entirely understood in order to reduce the plausibility of error in use.
CWE-192 commonly affects C, C++, Java and C#. Note that weaknesses are often language-agnostic patterns, so secure coding practices apply broadly.
MITRE documents real CVEs mapped to CWE-192, including CVE-2022-2639. You can look up the full details of each CVE, including CVSS scores and remediation guidance, on our CVE Lookup tool.
A CWE (Common Weakness Enumeration) like CWE-192 describes a category of software weakness — the underlying flaw type. A CVE (Common Vulnerabilities and Exposures) identifies a specific, real-world vulnerability in a particular product. In short, a CWE is the kind of mistake, and a CVE is an instance of that mistake being found in software.