From 2bde480334d701470681df16582ed92f56353bc9 Mon Sep 17 00:00:00 2001 From: otto <> Date: Tue, 15 Jul 2003 10:06:31 +0000 Subject: Test if malloc() sets errno correctly if it returns NULL. ok tdeval@ henning@ --- src/regress/lib/libc/malloc/malloc_errno/Makefile | 5 +++ .../lib/libc/malloc/malloc_errno/malloc_errno.c | 45 ++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 src/regress/lib/libc/malloc/malloc_errno/Makefile create mode 100644 src/regress/lib/libc/malloc/malloc_errno/malloc_errno.c diff --git a/src/regress/lib/libc/malloc/malloc_errno/Makefile b/src/regress/lib/libc/malloc/malloc_errno/Makefile new file mode 100644 index 0000000000..73ebe37491 --- /dev/null +++ b/src/regress/lib/libc/malloc/malloc_errno/Makefile @@ -0,0 +1,5 @@ +# $OpenBSD: Makefile,v 1.1 2003/07/15 10:06:31 otto Exp $ + +PROG= malloc_errno + +.include diff --git a/src/regress/lib/libc/malloc/malloc_errno/malloc_errno.c b/src/regress/lib/libc/malloc/malloc_errno/malloc_errno.c new file mode 100644 index 0000000000..61d85e5d41 --- /dev/null +++ b/src/regress/lib/libc/malloc/malloc_errno/malloc_errno.c @@ -0,0 +1,45 @@ +/* $OpenBSD: malloc_errno.c,v 1.1 2003/07/15 10:06:31 otto Exp $ */ +/* + * Public domain. 2003, Otto Moerbeek + */ +#include +#include +#include +#include + +static void +testerrno(size_t sz) +{ + void *p; + + errno = -1; + p = malloc(sz); + + if (p == NULL && errno != ENOMEM) + errx(1, "fail: %lx %p %d\n", (unsigned long)sz, p, errno); + + /* if alloc succeeded, test if errno did not change */ + if (p != NULL && errno != -1) + errx(1, "fail: %lx %p %d\n", (unsigned long)sz, p, errno); + + free(p); +} + +/* + * Provide some (silly) arguments to malloc(), and check if ERRNO is set + * correctly. + */ +int +main() +{ + size_t i; + + testerrno(1); + testerrno(100000); + testerrno(-1); + testerrno(-1000); + testerrno(-10000); + for (i = 0; i < 0x10; i++) + testerrno(i * 0x10000000); + return 0; +} -- cgit v1.2.3-55-g6feb