ci2_mem
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
/* - | Copyright / About | ----------------------------------------------------
|
||||
Copyright (c) 2026 Randy Jordan
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
this software and associated documentation files (the "Software"), to deal in
|
||||
the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do
|
||||
so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
||||
* --------------------------------------------------------------------------*/
|
||||
#define DEBUG
|
||||
#define GUI_DEBUG_CLOSE
|
||||
#include "../include/ci2.h"
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
static int
|
||||
test_caught_exception()
|
||||
{
|
||||
size_t nbytes = CI2_GB(1024);
|
||||
void* buf = NULL;
|
||||
const char* str = "Hello World";
|
||||
buf = CI2_CALLOC(1, strlen(str + 1)); /* You have no enable asan options. */
|
||||
strncpy(buf, str, strlen(str));
|
||||
|
||||
char* opts = "ASAN_OPTIONS=allocator_may_fail=1";
|
||||
|
||||
printf("Attempting oom re-allocation of %zu at %p\n", nbytes, buf);
|
||||
printf("Adding %s to env....%d\n", opts, putenv(opts));
|
||||
|
||||
CI2_TRY
|
||||
{
|
||||
printf("Trying something dumb.\n");
|
||||
CI2_RESIZE(buf, nbytes);
|
||||
}
|
||||
CI2_EXCEPT(oom)
|
||||
{
|
||||
/* handle memory failure gracefully */
|
||||
printf("Caught: %s\n", oom.msg);
|
||||
return EXIT_SUCCESS; /* requested behavior */
|
||||
}
|
||||
CI2_FINALLY
|
||||
{
|
||||
/* cleanup if needed, runs always */
|
||||
printf("Have to free allocation of %zu at %p\n", strlen(buf), buf);
|
||||
if (buf)
|
||||
free(buf);
|
||||
}
|
||||
CI2_END_TRY;
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
static int
|
||||
test_uncaught_exception(void)
|
||||
{
|
||||
#ifdef CI2_EXCEPTION_CRASH
|
||||
printf("CI2_MSG — Deliberate Failure (expect a debug break)\n");
|
||||
printf(" Triggering ci2_raise_exception() now...\n");
|
||||
fflush(stdout);
|
||||
|
||||
struct CI2_Exception u = { EFAULT, "Deliberate uncaught exception test." };
|
||||
CI2_RAISE(u);
|
||||
printf(" [FAIL] Execution continued past a failing ASSERT_MSG!\n");
|
||||
return EXIT_FAILURE; // Shouldn't but still.
|
||||
#endif
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, ci2_sys_char* argv[])
|
||||
{
|
||||
UNUSED(argc);
|
||||
UNUSED(argv);
|
||||
int result = -1;
|
||||
result = test_uncaught_exception();
|
||||
result = test_caught_exception();
|
||||
|
||||
return result;
|
||||
}
|
||||
Reference in New Issue
Block a user