CWE-366 单线程内的竞争条件

Race Condition within a Thread

结构: Simple

Abstraction: Base

状态: Draft

被利用可能性: Medium


If two threads of execution use a resource simultaneously, there exists the possibility that resources may be used while invalid, in turn making the state of execution undefined.


  • cwe_Nature: ChildOf cwe_CWE_ID: 362 cwe_View_ID: 1000 cwe_Ordinal: Primary

  • cwe_Nature: ChildOf cwe_CWE_ID: 362 cwe_View_ID: 699 cwe_Ordinal: Primary


Language: [{'cwe_Name': 'C', 'cwe_Prevalence': 'Undetermined'}, {'cwe_Name': 'C++', 'cwe_Prevalence': 'Undetermined'}, {'cwe_Name': 'Java', 'cwe_Prevalence': 'Undetermined'}, {'cwe_Name': 'C#', 'cwe_Prevalence': 'Undetermined'}]


范围 影响 注释
['Integrity', 'Other'] ['Alter Execution Logic', 'Unexpected State'] The main problem is that -- if a lock is overcome -- data could be altered in a bad state.


Architecture and Design


Use locking functionality. This is the recommended solution. Implement some form of locking mechanism around code which alters or reads persistent data in a multithreaded environment.

Architecture and Design


Create resource-locking sanity checks. If no inherent locking mechanisms exist, use flags and signals to enforce your own blocking scheme when resources are being used by other threads of execution.


The following example demonstrates the weakness.

bad C

int foo = 0;
int storenum(int num) {
static int counter = 0;
if (num > foo) foo = num;
return foo;

bad Java

public classRace {
static int foo = 0;
public static void main() {

new Threader().start();
foo = 1;
public static class Threader extends Thread {

public void run() {


映射的分类名 ImNode ID Fit Mapped Node Name
CLASP Race condition within a thread
CERT C Secure Coding CON32-C CWE More Abstract Prevent data races when accessing bit-fields from multiple threads
CERT C Secure Coding CON40-C CWE More Abstract Do not refer to an atomic variable twice in an expression
CERT C Secure Coding CON43-C Exact Do not allow data races in multithreaded code
The CERT Oracle Secure Coding Standard for Java (2011) VNA02-J Ensure that compound operations on shared variables are atomic
The CERT Oracle Secure Coding Standard for Java (2011) VNA03-J Do not assume that a group of calls to independently atomic methods is atomic
Software Fault Patterns SFP19 Missing Lock


  • CAPEC-26
  • CAPEC-29