decoder_create returns NULL when allocation fails. That path is part of the contract, so it needs a test. But on a modern desktop, malloc of a few dozen bytes essentially never fails, and making it fail for real, by exhausting memory, would take down the test machine with it. Untested failure paths are where cleanup bugs hide: a leak on the error branch, a half-initialized object returned as if it were fine, a double free in the recovery code.
The project makes allocation failure a test input instead of an accident. decoder_create does not call malloc directly. It takes a TelemetryAllocator:
typedef void *(*AllocateFn)(void *context, size_t bytes);
typedef void (*DeallocateFn)(void *context, void *storage);
typedef struct { AllocateFn allocate; DeallocateFn deallocate; void *context; } TelemetryAllocator;Production code passes telemetry_default_allocator(), which wraps malloc and free. A test passes its own callbacks and a pointer to a probe: a small struct that counts calls, tracks how many blocks are live, and can be told to fail. The decoder stores the allocator inside its handle and uses the same deallocate at destroy, so the probe sees both ends of every allocation.
Predict and run
Read the program before pressing Run. Predict the three numbers and explain why the call count is 2 even though one call failed.
#include <stdio.h>
typedef struct { size_t calls; int fail; } Probe;
int allow(void *context) {
Probe *probe = context;
++probe->calls;
return !probe->fail;
}
int main(void) {
Probe probe = {0,1};
int first = allow(&probe);
probe.fail = 0;
int second = allow(&probe);
printf("%d %d %zu\n", first,second,probe.calls);
return 0;
}Check your prediction
0 1 2The probe counts every attempt, successful or not, before deciding the result. That is what makes the count useful later: if any code path tries to allocate, even one that fails, the count moves. The void *context parameter is converted back to Probe * inside the callback. Converting an object pointer to void * and back to its original type is always well defined in C, which is why callback APIs use void * for their user data.
A context beats a global
The probe could be a global variable, and many test suites start that way. It breaks as soon as two decoders exist in one test: both would share one counter, so a leak in one could be hidden by an extra allocation in the other. A global also cannot be used safely if tests ever run on several threads. Passing the probe as context gives each decoder its own counters, and nothing about the library depends on test-only state. The cost is a lifetime rule: the probe must outlive the decoder, because destroy calls back into it.
Count, then compare
The key test pattern has four steps. Create with failure on, and check nothing is live. Create for real, and record the call count. Do arbitrary work, then check the count has not moved. Destroy, and check nothing is live. The program below runs that pattern against a small decoder with the project's allocator shape:
#include <stdio.h>
#include <stdlib.h>
typedef struct { size_t calls, live; int fail; } Probe;
static void *probe_alloc(void *context, size_t bytes) {
Probe *p = context;
++p->calls;
if (p->fail) { return NULL; }
void *block = malloc(bytes);
if (block != NULL) { ++p->live; }
return block;
}
static void probe_free(void *context, void *block) {
Probe *p = context;
if (block != NULL) { --p->live; free(block); }
}
typedef struct { void *(*allocate)(void *, size_t); void (*deallocate)(void *, void *); void *context; } Allocator;
typedef struct { unsigned char frame[22]; size_t used; Allocator allocator; } Decoder;
static Decoder *create(Allocator a) {
Decoder *d = a.allocate(a.context, sizeof *d);
if (d == NULL) { return NULL; }
d->used = 0; d->allocator = a;
return d;
}
static void feed_byte(Decoder *d, unsigned char byte) { d->frame[d->used] = byte; d->used = (d->used + 1) % 10; }
static void destroy(Decoder *d) { if (d != NULL) { d->allocator.deallocate(d->allocator.context, d); } }
int main(void) {
Probe probe = {0, 0, 1};
Allocator a = {probe_alloc, probe_free, &probe};
Decoder *failed = create(a);
printf("failed create: %s, live %zu\n", failed == NULL ? "NULL" : "handle", probe.live);
probe.fail = 0;
Decoder *d = create(a);
if (d == NULL) { return 1; }
size_t after_create = probe.calls;
for (int i = 0; i < 1000; ++i) { feed_byte(d, (unsigned char)i); }
printf("calls after create %zu, after 1000 feeds %zu, live %zu\n", after_create, probe.calls, probe.live);
destroy(d);
printf("after destroy: live %zu\n", probe.live);
return 0;
}failed create: NULL, live 0
calls after create 2, after 1000 feeds 2, live 1
after destroy: live 0"Live 1 before destroy, live 0 after" proves that destroy released the block create acquired. "Calls unchanged after 1000 feeds" proves feeding never allocates, a guarantee no amount of reading the code can give you as cheaply. The project's suite asserts exactly these facts around its whole test run.
Sweeping every failure point
Day 1's fail_at counter already reached each allocation of label_create by hand. Here it moves into the probe's context, where the library cannot see it, and the test stops naming failure points at all: it raises fail_at from 1 until a create succeeds. A handle that gains a third allocation next year is then swept without anyone editing the test.
Practice
Your turn
Write Session *session_create(Allocator a) for a handle that owns three blocks: the Session itself, a 5-octet header buffer and a 16-octet payload buffer, all taken from a. Store the allocator in the handle, and write session_destroy, which accepts NULL and returns every block through the stored allocator. Then, in main, write the sweep: for fail_at = 1, 2, 3 and so on, reset probe.calls, try a create, and print the failure with the live count; stop at the first create that succeeds and report how many failure points were swept. Do not write the number 3 anywhere in the test. The program must print:
fail at call 1: NULL, live 0
fail at call 2: NULL, live 0
fail at call 3: NULL, live 0
fail at call 4: created, live 3
after destroy: live 0
failure points swept: 3#include <stdio.h>
#include <stdlib.h>
typedef struct { void *(*allocate)(void *, size_t); void (*deallocate)(void *, void *); void *context; } Allocator;
/* The test's probe: fails call number fail_at (0: never) and counts live blocks. */
typedef struct { size_t calls, live, fail_at; } Probe;
static void *probe_alloc(void *context, size_t bytes) {
Probe *p = context;
++p->calls;
if (p->calls == p->fail_at) { return NULL; }
void *block = malloc(bytes);
if (block != NULL) { ++p->live; }
return block;
}
static void probe_free(void *context, void *block) {
Probe *p = context;
if (block != NULL) { --p->live; free(block); }
}
/* A handle with three owned blocks: itself, a header buffer and a payload buffer. */
typedef struct {
unsigned char *header;
unsigned char *payload;
Allocator allocator;
} Session;
/* Write session_create(Allocator a) and session_destroy(Session *s) here.
Every block comes from a.allocate and goes back through the stored allocator. */
int main(void) {
Probe probe = {0, 0, 0};
Allocator a = {probe_alloc, probe_free, &probe};
/* Sweep: raise fail_at from 1 until a create succeeds, checking live after each failure. */
(void)a;
return 0;
}Hint 1: the idea
The test does not know how many allocations create makes; it finds out. A create that fails at call N must have released calls 1 to N-1 itself, so live is 0 after every failure, and the first fail_at that create never reaches is the one where it succeeds.
Hint 2: a first step
Write the sweep loop first, against a session_create that makes only the struct allocation. Run it: it should report one failure point and live 1 on success. Then add the header buffer and run again before adding the payload. Each new allocation should add exactly one line to the sweep without any change to main.
Show one solution
#include <stdio.h>
#include <stdlib.h>
typedef struct { void *(*allocate)(void *, size_t); void (*deallocate)(void *, void *); void *context; } Allocator;
/* The test's probe: fails call number fail_at (0: never) and counts live blocks. */
typedef struct { size_t calls, live, fail_at; } Probe;
static void *probe_alloc(void *context, size_t bytes) {
Probe *p = context;
++p->calls;
if (p->calls == p->fail_at) { return NULL; }
void *block = malloc(bytes);
if (block != NULL) { ++p->live; }
return block;
}
static void probe_free(void *context, void *block) {
Probe *p = context;
if (block != NULL) { --p->live; free(block); }
}
/* A handle with three owned blocks: itself, a header buffer and a payload buffer. */
typedef struct {
unsigned char *header;
unsigned char *payload;
Allocator allocator;
} Session;
Session *session_create(Allocator a) {
Session *s = a.allocate(a.context, sizeof *s);
if (s == NULL) { return NULL; }
s->allocator = a;
s->header = a.allocate(a.context, 5);
if (s->header == NULL) { a.deallocate(a.context, s); return NULL; }
s->payload = a.allocate(a.context, 16);
if (s->payload == NULL) {
a.deallocate(a.context, s->header);
a.deallocate(a.context, s);
return NULL;
}
return s;
}
void session_destroy(Session *s) {
if (s == NULL) { return; }
Allocator a = s->allocator;
a.deallocate(a.context, s->payload);
a.deallocate(a.context, s->header);
a.deallocate(a.context, s);
}
int main(void) {
Probe probe = {0, 0, 0};
Allocator a = {probe_alloc, probe_free, &probe};
Session *s = NULL;
size_t at = 1;
for (;; ++at) {
probe.calls = 0;
probe.fail_at = at;
s = session_create(a);
if (s != NULL) { break; }
printf("fail at call %zu: NULL, live %zu\n", at, probe.live);
if (probe.live != 0) { return 1; }
}
printf("fail at call %zu: created, live %zu\n", at, probe.live);
session_destroy(s);
printf("after destroy: live %zu\n", probe.live);
printf("failure points swept: %zu\n", at - 1);
return 0;
}fail at call 1: NULL, live 0
fail at call 2: NULL, live 0
fail at call 3: NULL, live 0
fail at call 4: created, live 3
after destroy: live 0
failure points swept: 3The sweep stops at call 4 because create makes three allocations, and it learned that by running, not from the test. The test also returns 1 the moment a failure leaves anything live, so a leak stops the run on the line that caused it. Destroy copies the allocator out of the handle before releasing the handle itself, because s->allocator is unreadable once s is gone.
Prove it can fail
Delete the release of the header on the payload's failure path. The sweep stops with fail at call 3: NULL, live 1 and exit status 1: the header leaked, and only the third failure point could reveal it. Restore it, then add a fourth allocation to create, released on every path. Without touching main, the sweep reports four failure points and succeeds at call 5.
Completion is saved in this browser only. It does not measure mastery.