CWE
Home > CWE List > CWE-195 Individual Dictionary Definition (Draft 9)   View the CWE List

CWE-195 Individual Dictionary Definition (Draft 9)

Signed to Unsigned Conversion Error
Weakness ID
Status: Draft

195 (Weakness Variant)

Description

Summary

A signed-to-unsigned conversion error takes place when a signed primitive is used as an unsigned value, usually as a size variable.

Common Consequences

Conversion between signed and unsigned values can lead to a variety of errors, but from a security standpoint is most commonly associated with integer overflow and buffer overflow vulnerabilities.

Demonstrative
Examples

In this example the variable amount can hold a negative value when it is returned. Because the function is declared to return an unsigned int, amount will be implicitly converted to unsigned.

unsigned int readdata () {
  int amount = 0;
  ...
  if (result == ERROR)
    amount = -1;
  ...
  return amount;
}

If the error condition in the code above is met, then the return value of readdata() will be 4,294,967,295 on a system uses 32-bit integers.


In this example, depending on the return value of accecssmainframe(), the variable amount can hold a negative value when it is returned. Because the function is declared to return an unsigned value, amount will be implicitly cast to an unsigned number.

unsigned int readdata () {
  int amount = 0;
  ...
  amount = accessmainframe();
  ...
  return amount;
}

If the return value of accessmainframe() is -1, then the return value of readdata() will be 4,294,967,295 on a system that uses 32-bit integers.

Observed Examples
ReferenceDescription
CVE-2007-4268Chain: integer signedness passes signed comparison, leads to heap overflow
Context Notes

It is dangerous to rely on implicit casts between signed and unsigned numbers because the result can take on an unexpected value and violate weak assumptions made elsewhere in the program.

Relationships
NatureTypeIDName
ChildOfWeakness BaseWeakness BaseWeakness Base681Incorrect Conversion between Numeric Types
ChildOfCategoryCategory189Numeric Errors
CanPrecedeWeakness VariantWeakness VariantWeakness Variant122Heap-based Buffer Overflow
CanAlsoBeCategoryCategory192Integer Coercion Error
CanAlsoBeWeakness BaseWeakness BaseWeakness Base197Numeric Truncation Error
Source Taxonomies

CLASP - Signed to unsigned conversion error

Applicable Platforms

C

C++

Time of Introduction

Implementation

Page Last Updated: April 22, 2008