...
Code Block |
---|
|
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 |
---|
|
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 |
---|
|
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
...