1451
Example 2:
The following code intends to fork a process, then have both the parent and child processes print a
single line.
Example Language: C (Bad)
static void print (char * string) {
char * word;
int counter;
for (word = string; counter = *word++; ) {
putc(counter, stdout);
fflush(stdout);
/* Make timing window a little larger... */
sleep(1);
}
}
int main(void) {
pid_t pid;
pid = fork();
if (pid == -1) {
exit(-2);
}
else if (pid == 0) {
print("child\n");
}
else {
print("PARENT\n");
}
exit(0);
}
One might expect the code to print out something like:
PARENT
child
However, because the parent and child are executing concurrently, and stdout is flushed each time
a character is printed, the output might be mixed together, such as:
PcAhRiElNdT
[blank line]
[blank line]
Observed Examples
Reference Description
CVE-2021-1782 Chain: improper locking (CWE-667) leads to race condition (CWE-362), as
exploited in the wild per CISA KEV.
https://www.cve.org/CVERecord?id=CVE-2021-1782
CVE-2009-0935 Attacker provides invalid address to a memory-reading function, causing a
mutex to be unlocked twice
https://www.cve.org/CVERecord?id=CVE-2009-0935
MemberOf Relationships
This MemberOf relationships table shows additional CWE Catgeories and Views that reference this
weakness as a member. This information is often useful in understanding where a weakness fits
within the context of external information sources.
Nature Type ID Name Page
MemberOf 745 CERT C Secure Coding Standard (2008) Chapter 12 -
Signals (SIG)
734 2349
MemberOf 852 The CERT Oracle Secure Coding Standard for Java
(2011) Chapter 9 - Visibility and Atomicity (VNA)
844 2366
CWE Version 4.14
CWE-663: Use of a Non-reentrant Function in a Concurrent Context
CWE-663: Use of a Non-reentrant Function in a Concurrent Context