Race Condition in Switch |
Weakness ID: 365 (Weakness Base) | Status: Draft |
Description Summary
The code contains a switch statement in which the switched variable can be modified while the switch is still executing, resulting in unexpected behavior.
Example 1
(Bad Code)
Example Languages: C and C++
#include <sys/types.h>
#include <sys/stat.h>
int main(argc,argv){
struct stat *sb;
time_t timer;
lstat("bar.sh",sb);
printf("%d\n",sb->st_ctime);
switch(sb->st_ctime % 2){
case 0: printf("One option\n");
break;
case 1: printf("another option\n");
break;
default: printf("huh\n");
break;
}
return 0;
}
Phase: Implementation Variables that may be subject to race conditions should be locked for the duration of any switch statements. |
This issue is particularly important in the case of switch statements that involve fall-through style case statements -- ie., those which do not end with break. If the variable which we are switching on change in the course of execution, the actions carried out may place the state of the process in a contradictory state or even result in memory corruption. For this reason, it is important to ensure that all variables involved in switch statements are locked before the statement starts and are unlocked when the statement ends. |
Nature | Type | ID | Name | View(s) this relationship pertains to![]() |
---|---|---|---|---|
ChildOf | ![]() | 362 | Race Condition | Development Concepts (primary)699 Research Concepts (primary)1000 |
ChildOf | ![]() | 748 | CERT C Secure Coding Section 50 - POSIX (POS) | Weaknesses Addressed by the CERT C Secure Coding Standard (primary)734 |
PeerOf | ![]() | 364 | Signal Handler Race Condition | Research Concepts1000 |
PeerOf | ![]() | 366 | Race Condition within a Thread | Research Concepts1000 |
Mapped Taxonomy Name | Node ID | Fit | Mapped Node Name |
---|---|---|---|
CLASP | Race condition in switch | ||
CERT C Secure Coding | POS35-C | Avoid race conditions while checking for the existence of a symbolic link |
Submissions | ||||
---|---|---|---|---|
Submission Date | Submitter | Organization | Source | |
CLASP | Externally Mined | |||
Modifications | ||||
Modification Date | Modifier | Organization | Source | |
2008-07-01 | Eric Dalci | Cigital | External | |
updated Time of Introduction | ||||
2008-09-08 | CWE Content Team | MITRE | Internal | |
updated Applicable Platforms, Common Consequences, Relationships, Other Notes, Taxonomy Mappings | ||||
2008-11-24 | CWE Content Team | MITRE | Internal | |
updated Relationships, Taxonomy Mappings |