1159
Example 2:
The following C/C++ example shows a simple implementation of a stack that includes methods for
adding and removing integer values from the stack. The example uses pointers to add and remove
integer values to the stack array variable.
Example Language: C (Bad)
#define SIZE 50
int *tos, *p1, stack[SIZE];
void push(int i) {
p1++;
if(p1==(tos+SIZE)) {
// Print stack overflow error message and exit
}
*p1 == i;
}
int pop(void) {
if(p1==tos) {
// Print stack underflow error message and exit
}
p1--;
return *(p1+1);
}
int main(int argc, char *argv[]) {
// initialize tos and p1 to point to the top of stack
tos = stack;
p1 = stack;
// code to add and remove items from stack
...
return 0;
}
The push method includes an expression to assign the integer value to the location in the stack
pointed to by the pointer variable.
However, this expression uses the comparison operator "==" rather than the assignment operator
"=". The result of using the comparison operator instead of the assignment operator causes
erroneous values to be entered into the stack and can cause unexpected results.
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 747 CERT C Secure Coding Standard (2008) Chapter 14 -
Miscellaneous (MSC)
734 2350
MemberOf 883 CERT C++ Secure Coding Section 49 - Miscellaneous
(MSC)
868 2381
MemberOf 886 SFP Primary Cluster: Unused entities 888 2382
MemberOf 1410 Comprehensive Categorization: Insufficient Control
Flow Management
1400 2536
Taxonomy Mappings
Mapped Taxonomy Name Node ID Fit Mapped Node Name
CLASP Comparing instead of assigning
Software Fault Patterns SFP2 Unused Entities
References
[REF-18]Secure Software, Inc.. "The CLASP Application Security Process". 2005. < https://
cwe.mitre.org/documents/sources/TheCLASPApplicationSecurityProcess.pdf >.
CWE Version 4.14
CWE-483: Incorrect Block Delimitation
CWE-483: Incorrect Block Delimitation