607
Many functions will return some value about the success of their actions. This will alert the program
whether or not to handle any errors caused by that function.
Likelihood Of Exploit
Low
Common Consequences
Scope Impact Likelihood
Availability
Integrity
Unexpected State
DoS: Crash, Exit, or Restart
An unexpected return value could place the system in
a state that could lead to a crash or other unintended
behaviors.
Detection Methods
Automated Static Analysis
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 searching for potentially-vulnerable patterns that connect "sources" (origins of input)
with "sinks" (destinations where the data interacts with external components, a lower layer such
as the OS, etc.)
Effectiveness = High
Potential Mitigations
Phase: Implementation
Check the results of all functions that return a value and verify that the value is expected.
Effectiveness = High
Checking the return value of the function will typically be sufficient, however beware of race
conditions (CWE-362) in a concurrent environment.
Phase: Implementation
Ensure that you account for all possible return values from the function.
Phase: Implementation
When designing a function, make sure you return a value or throw an exception in case of an
error.
Demonstrative Examples
Example 1:
Consider the following code segment:
Example Language: C (Bad)
char buf[10], cp_buf[10];
fgets(buf, 10, stdin);
strcpy(cp_buf, buf);
The programmer expects that when fgets() returns, buf will contain a null-terminated string of length
9 or less. But if an I/O error occurs, fgets() will not null-terminate buf. Furthermore, if the end of the
file is reached before any characters are read, fgets() returns without writing anything to buf. In both
of these situations, fgets() signals that something unusual has happened by returning NULL, but in
this code, the warning will not be noticed. The lack of a null terminator in buf can result in a buffer
overflow in the subsequent call to strcpy().
CWE Version 4.14
CWE-252: Unchecked Return Value
CWE-252: Unchecked Return Value