summaryrefslogtreecommitdiff
path: root/src/regress/lib/libc/malloc/malloc_errno/malloc_errno.c
blob: 7f77544665bd7e0c64bfd7a7d8aecbbd6c4524f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*	$OpenBSD: malloc_errno.c,v 1.3 2003/08/15 23:13:07 deraadt 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", (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", (unsigned long)sz, p, errno);

	free(p);
}

/*
 * Provide some (silly) arguments to malloc(), and check if ERRNO is set
 * correctly.
 */
int
main(int argc, char *argv[])
{
	size_t i;

	testerrno(1);
	testerrno(100000);
	testerrno(-1);
	testerrno(-1000);
	testerrno(-10000);
	for (i = 0; i < 0x10; i++)
		testerrno(i * 0x10000000);
	return 0;
}