Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Changed the severity, minor edits; reviewed

...

Code Block
bgColor#FFCCCC
langc
#include <stdlib.h>
 
int f(void) {
  enum { BUFFER_SIZE = 32 };
  char *text_buffer = (char *)malloc(BUFSIZ(BUFFER_SIZE); 
  if (text_buffer == NULL) {
    return -1;
  }
  return 0;
}

Compliant Solution (free())

In this compliant solution, the pointer is deallocated with a call to free()

Code Block
bgColor#ccccff
langc
#include <stdlib.h>
 
int f(void) {
  enum { BUFFER_SIZE = 32 };
  char *text_buffer = (char *)malloc(BUFFER_SIZE); 
  if (text_buffer == NULL) {
    return -1;
  }
 
  free(text_buffer);
  return 0;
}

Compliant Solution (static storage duration)

In this compliant solution, the pointer object that stores the return value from malloc() is stored in a variable of static storage duration.

Code Block
bgColor#ccccff
langc
#include <stdlib.h>

char *text_buffer;
 
int f(void) {
  enum { BUFFER_SIZE = 32 };  
  static char *text_buffer = (char *)malloc(BUFSIZBUFFER_SIZE); 
  if (text_buffer == NULL) {
    return -1;
  }
  return 0;
}

Risk Assessment

Freeing Failing to free memory multiple times can result in an attacker executing arbitrary code with the permissions of the vulnerable processthe exhaustion of system memory resources, which can lead to a denial-of-service attack.

Rule

Severity

Likelihood

Remediation Cost

Priority

Level

MEM31-C

HighMedium

Probable

Medium

P12P6

L1L2

Automated Detection

Tool

Version

Checker

Description

Compass/ROSE

Coverity

Include Page
Coverity_V
Coverity_V

RESOURCE_LEAK

Finds resource leaks from variables that go out of scope while owning a resource

Fortify SCA

5.0

  

Klocwork

Include Page
Klocwork_V
Klocwork_V

MLK
UFM.FFM

 

LDRA tool suite

Include Page
LDRA_V
LDRA_V

484 S

Fully implemented

Splint

Include Page
Splint_V
Splint_V
  

...