From 86feda786f115cb4468957f00b8968b986ed7c54 Mon Sep 17 00:00:00 2001 From: peter Date: Thu, 20 Aug 2026 20:54:43 +0000 Subject: Get full coverage of aplwc-repl. This required wrapping malloc so that it can fail under our control. FossilOrigin-Name: a27ccb5f02994393a518419bae15fd26ff0016c734d96350ae2137a1d3c0937c --- src/bin/aplwc_repl.h | 23 +++++++++++++++++++++++ src/bin/main.c | 19 +++++++++---------- src/bin/util.c | 17 +++++++++++++++++ src/bin/wrappers.c | 40 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 89 insertions(+), 10 deletions(-) create mode 100644 src/bin/aplwc_repl.h create mode 100644 src/bin/util.c create mode 100644 src/bin/wrappers.c (limited to 'src/bin') diff --git a/src/bin/aplwc_repl.h b/src/bin/aplwc_repl.h new file mode 100644 index 0000000..c189b77 --- /dev/null +++ b/src/bin/aplwc_repl.h @@ -0,0 +1,23 @@ +#ifndef APLWC_REPL_H +#define APLWC_REPL_H + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* util.c */ +unsigned long getenv_ulong(const char *, unsigned long); + +/* wrappers.c */ +void *wrapped_malloc(size_t); +void wrapped_free(void *); +#define malloc(size) wrapped_malloc(size) +#define free(ptr) wrapped_free(ptr) + +#endif /* APLWC_REPL_H */ diff --git a/src/bin/main.c b/src/bin/main.c index 2538248..80b2ee0 100644 --- a/src/bin/main.c +++ b/src/bin/main.c @@ -1,6 +1,5 @@ #include -#include -#include +#include "aplwc_repl.h" int main(void) @@ -8,24 +7,24 @@ main(void) struct aplwc *aplwc; uint8_t *data; int ret; + size_t memsize; - data = malloc(APLWC_MIN_MEM); + memsize = getenv_ulong("APLWC_MEM", APLWC_MIN_MEM); + + data = malloc(memsize); if(data == NULL){ fprintf(stderr, "Couldn't allocate memory\n"); return 1; } - ret = aplwc_new(&aplwc, data, APLWC_MIN_MEM); + ret = aplwc_new(&aplwc, data, memsize); if(ret){ - fprintf(stderr, "Couldn't create new aplwc instance: %s\n", aplwc_errstr(aplwc)); + fprintf(stderr, "Couldn't create new aplwc instance: %s\n", aplwc_errstr_rc(ret)); + free(data); return 1; } - ret = aplwc_destroy(aplwc); - if(ret){ - fprintf(stderr, "Couldn't destroy the aplwc instance: %s\n", aplwc_errstr(aplwc)); - return 1; - } + aplwc_destroy(aplwc); free(data); diff --git a/src/bin/util.c b/src/bin/util.c new file mode 100644 index 0000000..c8218fd --- /dev/null +++ b/src/bin/util.c @@ -0,0 +1,17 @@ +#include "aplwc_repl.h" + +unsigned long +getenv_ulong(const char *name, unsigned long def) +{ + unsigned long ret, parsed; + char *env, *p; + + ret = def; + env = getenv(name); + if((env != NULL) && (strlen(env) > 0)){ + parsed = strtoul(env, &p, 10); + if((*p) == 0) + ret = parsed; + } + return ret; +} 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); +} -- cgit v1.2.3