Files
ci2/tests/03_mem_exception.c

34 lines
813 B
C

#define DEBUG
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include "../include/ci2_mem.h"
__attribute__((constructor)) static void setup_asan(void) {
setenv("ASAN_OPTIONS", "allocator_may_return_null=1", 1);
}
int main(void){
size_t nbytes = MEM_GB(1024);
void *buf;
TRY {
buf = ALLOC(nbytes); /* try 1GB */
if (!buf)
RAISE(oom);
free(buf);
}
EXCEPT(oom) {
/* handle memory failure gracefully */
fprintf(stderr, "Caught: %s Exception! Now we can return success.\n", oom.reason);
return EXIT_SUCCESS; /* requested behavior */
}
FINALLY {
/* cleanup if needed, runs always */
if (buf) free(buf);
}
END_TRY;
return EXIT_FAILURE; /* shouldn't reach here for this example */
}