summaryrefslogtreecommitdiff
path: root/regress/lib/libc
diff options
context:
space:
mode:
authorOtto Moerbeek <otto@cvs.openbsd.org>2003-07-15 10:06:32 +0000
committerOtto Moerbeek <otto@cvs.openbsd.org>2003-07-15 10:06:32 +0000
commitfe4f922d013b26678d4a7d14a3cea7ca3839e639 (patch)
tree8679635ea572842ba9099dc198c6990764bec5d4 /regress/lib/libc
parent38b21703fc471790d770c089b29fe82763af303c (diff)
Test if malloc() sets errno correctly if it returns NULL.
ok tdeval@ henning@
Diffstat (limited to 'regress/lib/libc')
-rw-r--r--regress/lib/libc/malloc/malloc_errno/Makefile5
-rw-r--r--regress/lib/libc/malloc/malloc_errno/malloc_errno.c45
2 files changed, 50 insertions, 0 deletions
diff --git a/regress/lib/libc/malloc/malloc_errno/Makefile b/regress/lib/libc/malloc/malloc_errno/Makefile
new file mode 100644
index 00000000000..73ebe374919
--- /dev/null
+++ b/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 <bsd.regress.mk>
diff --git a/regress/lib/libc/malloc/malloc_errno/malloc_errno.c b/regress/lib/libc/malloc/malloc_errno/malloc_errno.c
new file mode 100644
index 00000000000..61d85e5d410
--- /dev/null
+++ b/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 <err.h>
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+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;
+}