summaryrefslogtreecommitdiffstats
path: root/usr/klibc/tests/malloctest3.c
blob: d9d2ca9cd2377cb1ba19ed7f10235b8b7fc821ab (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
46
47
48
49
50
51
52
53
54
55
56
57
#include <assert.h>
#include <errno.h>
#include <limits.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
	void *p;

	/* Our implementation should always return NULL */
	errno = 0;
	p = malloc(0);
	assert(p == NULL);
	assert(errno == 0);

	/* These sizes won't fit in memory, so should always fail */
	errno = 0;
	p = malloc(SIZE_MAX);
	assert(p == NULL);
	assert(errno == ENOMEM);
	errno = 0;
	p = malloc(SIZE_MAX - 0x10000);
	assert(p == NULL);
	assert(errno == ENOMEM);

#if SIZE_MAX > 0x100000000
	/* We should be able to allocate 4 GB + 1 */
	p = malloc(0x100000001);
	assert(p != NULL);
	((volatile char *)p)[0x100000000] = 1;
	free(p);

	/* calloc() should detect multiplication overflow */
	errno = 0;
	p = calloc(0x100000000, 0x100000000);
	assert(p == NULL);
	assert(errno == ENOMEM);
	errno = 0;
	p = calloc(0x100000001, 0x100000001);
	assert(p == NULL);
	assert(errno == ENOMEM);
#else
	/* calloc() should detect multiplication overflow */
	errno = 0;
	p = calloc(0x10000, 0x10000);
	assert(p == NULL);
	assert(errno == ENOMEM);
	errno = 0;
	p = calloc(0x10001, 0x10001);
	assert(p == NULL);
	assert(errno == ENOMEM);
#endif

	return 0;
}