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.
Extended Description It is dangerous to rely on implicit casts between signed and unsigned numbers because the result can take on an unexpected value and violate assumptions made by the program.
Example 1 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. (Bad Code) C 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. Example 2 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. (Bad Code) C 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.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Page Last Updated:
October 29, 2009
|
|
CWE is a Software Assurance strategic initiative sponsored by the National Cyber Security Division of the U.S. Department of Homeland Security. This Web site is hosted by The MITRE Corporation. Contact cwe@mitre.org for more information. |
|||
