1518
The following function attempts to acquire a lock in order to perform operations on a shared
resource.
Example Language: C (Bad)
void f(pthread_mutex_t *mutex) {
pthread_mutex_lock(mutex);
/* access shared resource */
pthread_mutex_unlock(mutex);
}
However, the code does not check the value returned by pthread_mutex_lock() for errors. If
pthread_mutex_lock() cannot acquire the mutex for any reason, the function may introduce a race
condition into the program and result in undefined behavior.
In order to avoid data races, correctly written programs must check the result of thread
synchronization functions and appropriately handle all errors, either by attempting to recover from
them or reporting them to higher levels.
Example Language: C (Good)
int f(pthread_mutex_t *mutex) {
int result;
result = pthread_mutex_lock(mutex);
if (0 != result)
return result;
/* access shared resource */
return pthread_mutex_unlock(mutex);
}
Example 2:
In this example, the programmer has indented the statements to call Do_X() and Do_Y(), as if the
intention is that these functions are only called when the condition is true. However, because there
are no braces to signify the block, Do_Y() will always be executed, even if the condition is false.
Example Language: C (Bad)
if (condition==true)
Do_X();
Do_Y();
This might not be what the programmer intended. When the condition is critical for security, such as
in making a security decision or detecting a critical error, this may produce a vulnerability.
Example 3:
This function prints the contents of a specified file requested by a user.
Example Language: PHP (Bad)
function printFile($username,$filename){
//read file into string
$file = file_get_contents($filename);
if ($file && isOwnerOf($username,$filename)){
echo $file;
return true;
}
else{
echo 'You are not authorized to view this file';
}
return false;
}
CWE Version 4.14
CWE-692: Incomplete Denylist to Cross-Site Scripting
CWE-692: Incomplete Denylist to Cross-Site Scripting