Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
bgColor#ccccff
langc
png_charp chunkdata;

int f(void) {
  chunkdata = (png_charp)png_malloc(png_ptr, length + 1);
  if (chunkdata == NULL) {
    return -1;  /* Handle allocation error Indicate failure */
  }

/* ... */
  return 0;
}

Noncompliant Code Example

...

Code Block
bgColor#FFCCCC
langc
int f(void) {
  size_t size = strlen(input_str)+1;
  str = (char *)malloc(size);
  memcpy(str, input_str, size);
/* ... */
  free(str);
  str = NULL;

 
  return 0;
}

Compliant Solution

To correct this error, ensure the pointer returned by malloc() is not null. This practice also ensures compliance with MEM32-C. Detect and handle memory allocation errors.

Code Block
bgColor#ccccff
langc
int f(void) {
  size_t size = strlen(input_str)+1;
  str = (char *)malloc(size);
  if (str == NULL) {
    return -1; /* HandleIndicate allocation errorfailure */
  }
  memcpy(str, input_str, size);
/* ... */
  free(str);
  str = NULL;

/* ... */
  return 0;
}

Noncompliant Code Example

...