diff options
| author | Thomas Vanbesien <tvanbesi@proton.me> | 2026-02-22 12:31:46 +0100 |
|---|---|---|
| committer | Thomas Vanbesien <tvanbesi@proton.me> | 2026-02-22 12:42:48 +0100 |
| commit | 29b8c1556bf456596c6c067d413b65b51e17724e (patch) | |
| tree | b225381d762af049e9174cdf1459db1f91c9589a /tests/src/main.c | |
| download | malloc-29b8c1556bf456596c6c067d413b65b51e17724e.tar.gz malloc-29b8c1556bf456596c6c067d413b65b51e17724e.zip | |
Initial scaffold: Makefile, stub malloc/free/realloc, test harness
Build system produces libft_malloc_$HOSTTYPE.so shared library with
Libft (NOMALLOC=1) as dependency. Stub functions print their name
and return NULL. Test runner compares system malloc vs LD_PRELOAD
output using write(2) to avoid stdio interference.
Diffstat (limited to 'tests/src/main.c')
| -rw-r--r-- | tests/src/main.c | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/tests/src/main.c b/tests/src/main.c new file mode 100644 index 0000000..0ded605 --- /dev/null +++ b/tests/src/main.c @@ -0,0 +1,46 @@ +/** + * @file main.c + * @brief Smoke test — call malloc once and print the returned pointer. + * + * Uses write(2) instead of printf to avoid stdio calling malloc + * internally, which would pollute output under LD_PRELOAD. + */ + +#include <stdlib.h> +#include <unistd.h> + +/** @brief Write the hex representation of @p ptr to stdout. */ +static void +_s_put_ptr (void *ptr) +{ + const char hex[] = "0123456789abcdef"; + char buf[20]; + unsigned long v = (unsigned long)ptr; + int i; + + if (!ptr) + { + write (1, "(nil)", 5); + return; + } + i = (int)sizeof (buf); + while (v) + { + buf[--i] = hex[v % 16]; + v /= 16; + } + buf[--i] = 'x'; + buf[--i] = '0'; + write (1, buf + i, (size_t)(sizeof (buf) - i)); +} + +int +main (void) +{ + void *p = malloc (42); + write (1, "malloc(42) = ", 13); + _s_put_ptr (p); + write (1, "\n", 1); + free (p); + return (0); +} |
