1482
#define SUCCESS 1
#define ERROR -1
#define MAX_MESSAGE_SIZE 32
int processMessage(char **message)
{
int result = SUCCESS;
int length = getMessageLength(message[0]);
char *messageBody;
if ((length > 0) && (length < MAX_MESSAGE_SIZE)) {
messageBody = (char*)malloc(length*sizeof(char));
messageBody = &message[1][0];
int success = processMessageBody(messageBody);
if (success == ERROR) {
result = ERROR;
free(messageBody);
}
}
else {
printf("Unable to process message; invalid message length");
result = FAIL;
}
if (result == ERROR) {
logError("Error processing message", messageBody);
}
return result;
}
However, the call to the method logError includes the messageBody after the memory for
messageBody has been released using the free method. This can cause unexpected results and
may lead to system crashes. A variable should never be used after its memory resources have
been released.
Example Language: C (Good)
...
messageBody = (char*)malloc(length*sizeof(char));
messageBody = &message[1][0];
int success = processMessageBody(messageBody);
if (success == ERROR) {
result = ERROR;
logError("Error processing message", messageBody);
free(messageBody);
}
...
Observed Examples
Reference Description
CVE-2009-3547 Chain: race condition (CWE-362) might allow resource to be released before
operating on it, leading to NULL dereference (CWE-476)
https://www.cve.org/CVERecord?id=CVE-2009-3547
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 808 2010 Top 25 - Weaknesses On the Cusp 800 2355
MemberOf 884 CWE Cross-section 884 2567
MemberOf 983 SFP Secondary Cluster: Faulty Resource Use 888 2410
MemberOf 1003 Weaknesses for Simplified Mapping of Published
Vulnerabilities
1003 2576
CWE Version 4.14
CWE-673: External Influence of Sphere Definition
CWE-673: External Influence of Sphere Definition