summaryrefslogtreecommitdiff
path: root/src/bin/wrappers.c
diff options
context:
space:
mode:
authorpeter <peter@pmikkelsen.com>2026-08-20 20:54:43 +0000
committerpeter <peter@pmikkelsen.com>2026-08-20 20:54:43 +0000
commit86feda786f115cb4468957f00b8968b986ed7c54 (patch)
tree31228cf73674178764cd6376d483e76212181707 /src/bin/wrappers.c
parentf5d445afa10ed7ba03b0ea9a5274f11904220700 (diff)
Get full coverage of aplwc-repl.
This required wrapping malloc so that it can fail under our control. FossilOrigin-Name: a27ccb5f02994393a518419bae15fd26ff0016c734d96350ae2137a1d3c0937c
Diffstat (limited to 'src/bin/wrappers.c')
-rw-r--r--src/bin/wrappers.c40
1 files changed, 40 insertions, 0 deletions
diff --git a/src/bin/wrappers.c b/src/bin/wrappers.c
new file mode 100644
index 0000000..18e179a
--- /dev/null
+++ b/src/bin/wrappers.c
@@ -0,0 +1,40 @@
+#include "aplwc_repl.h"
+
+static uint64_t malloc_count = 0;
+
+static uint64_t
+malloc_fail_after(void)
+{
+#if APLWC_BUILD_TYPE_RELEASE
+ return UINT64_MAX;
+#else
+ static int setup;
+ static uint64_t count, count_rec;
+
+ if(!setup){
+ setup = 1;
+ count = getenv_ulong("APLWC_MALLOC_FAIL_AFTER", ULONG_MAX);
+ count_rec = malloc_fail_after();
+ assert(ALWAYS(count == count_rec));
+ }
+
+ return count;
+#endif
+}
+
+#undef malloc
+void *
+wrapped_malloc(size_t size)
+{
+ if(malloc_count++ >= malloc_fail_after())
+ return NULL;
+ else
+ return malloc(size);
+}
+
+#undef free
+void
+wrapped_free(void *ptr)
+{
+ free(ptr);
+}