Everything

-secure_malloc [Professional Edition only] [V2.05.00 or later]


< Library Generator Options / Library Options >

[Format]

-secure_malloc

[Description]

This option creates the calloc, free, malloc, and realloc functions to which a security facility for detecting illegal operations to storage areas has been added.

When one of the following operations is performed, the __heap_chk_fail function is called.

-

The pointer to an area other than that allocated by calloc, malloc, or realloc is passed to free or realloc.

-

The pointer to an area released by free is passed again to free or realloc.

-

A value is written to up to four bytes before and after the area allocated by calloc, malloc, or realloc and the pointer to that area is passed to free or realloc.

The same facility is also added to the new and delete operators in C++ programs.

The __heap_chk_fail function needs to be defined by the user and it describes the processing to be executed when an error occurs in management of dynamic memory.

Note the following points when defining the __heap_chk_fail function.

-

The only possible type of return value is void and the __heap_chk_fail function does not have formal parameters.

-

When defining the __heap_chk_fail function in a C++ program, add extern "C".

-

Corruption of heap space should not be detected recursively in the __heap_chk_fail function.

-

Do not define the function as static.

[Example]

#include <stdlib.h>
 
void sub(int *ip) {
    ...
    free(ip);
}
 
int func(void) {
    int *ip;
    if ((ip = malloc(40 * sizeof(int))) == NULL)
        if ((ip = malloc(10 * sizeof(int))) == NULL) return(1);
        else sub(ip); /* First appearance of free */
    else
        ...
    free(ip); /* Second appearance of free */
    return(0);
}
 
#ifdef __cplusplus
extern "C" {
#endif
void __heap_chk_fail(void) {
    /* Processing when corruption of heap memory area is detected */
}
#ifdef __cplusplus
}
#endif

[Remarks]

The calloc, malloc, and realloc functions for the security facility allocate four extra bytes before and after each allocated area for the purpose of detecting writing to addresses outside the allocated area. This consumes more heap memory area than with the usual functions. Using the new operators in C++ programs will also consume more heap memory area.