annotate src/lib/test-imem.c @ 23007:36e01285b5b8

lib: buffer - Improve header comment for buffer_insert() and buffer_delete().
author Stephan Bosch <stephan.bosch@dovecot.fi>
date Mon, 18 Mar 2019 00:52:37 +0100
parents cb108f786fb4
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
22713
cb108f786fb4 Updated copyright notices to include the year 2018.
Stephan Bosch <stephan.bosch@dovecot.fi>
parents: 22138
diff changeset
1 /* Copyright (c) 2017-2018 Dovecot authors, see the included COPYING file */
22138
54260e47d2e1 lib: Add i_realloc_type() for i_realloc() that checks for overflows
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff changeset
2
54260e47d2e1 lib: Add i_realloc_type() for i_realloc() that checks for overflows
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff changeset
3 #include "test-lib.h"
54260e47d2e1 lib: Add i_realloc_type() for i_realloc() that checks for overflows
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff changeset
4
54260e47d2e1 lib: Add i_realloc_type() for i_realloc() that checks for overflows
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff changeset
5 struct test_struct {
54260e47d2e1 lib: Add i_realloc_type() for i_realloc() that checks for overflows
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff changeset
6 uint32_t num[10];
54260e47d2e1 lib: Add i_realloc_type() for i_realloc() that checks for overflows
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff changeset
7 };
54260e47d2e1 lib: Add i_realloc_type() for i_realloc() that checks for overflows
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff changeset
8
54260e47d2e1 lib: Add i_realloc_type() for i_realloc() that checks for overflows
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff changeset
9 static void test_imem_alloc(void)
54260e47d2e1 lib: Add i_realloc_type() for i_realloc() that checks for overflows
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff changeset
10 {
54260e47d2e1 lib: Add i_realloc_type() for i_realloc() that checks for overflows
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff changeset
11 struct test_struct ab, bc, cd, de;
54260e47d2e1 lib: Add i_realloc_type() for i_realloc() that checks for overflows
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff changeset
12
54260e47d2e1 lib: Add i_realloc_type() for i_realloc() that checks for overflows
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff changeset
13 test_begin("imem allocs");
54260e47d2e1 lib: Add i_realloc_type() for i_realloc() that checks for overflows
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff changeset
14
54260e47d2e1 lib: Add i_realloc_type() for i_realloc() that checks for overflows
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff changeset
15 memset(ab.num, 0xab, sizeof(ab.num));
54260e47d2e1 lib: Add i_realloc_type() for i_realloc() that checks for overflows
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff changeset
16 memset(bc.num, 0xbc, sizeof(bc.num));
54260e47d2e1 lib: Add i_realloc_type() for i_realloc() that checks for overflows
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff changeset
17 memset(cd.num, 0xcd, sizeof(cd.num));
54260e47d2e1 lib: Add i_realloc_type() for i_realloc() that checks for overflows
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff changeset
18 memset(de.num, 0xde, sizeof(de.num));
54260e47d2e1 lib: Add i_realloc_type() for i_realloc() that checks for overflows
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff changeset
19
54260e47d2e1 lib: Add i_realloc_type() for i_realloc() that checks for overflows
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff changeset
20 /* regular alloc */
54260e47d2e1 lib: Add i_realloc_type() for i_realloc() that checks for overflows
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff changeset
21 struct test_struct *s1 = i_new(struct test_struct, 2);
54260e47d2e1 lib: Add i_realloc_type() for i_realloc() that checks for overflows
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff changeset
22 struct test_struct *s2 = i_malloc(sizeof(struct test_struct) * 2);
54260e47d2e1 lib: Add i_realloc_type() for i_realloc() that checks for overflows
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff changeset
23 s1[0] = ab; s2[0] = ab;
54260e47d2e1 lib: Add i_realloc_type() for i_realloc() that checks for overflows
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff changeset
24 s1[1] = bc; s2[1] = bc;
54260e47d2e1 lib: Add i_realloc_type() for i_realloc() that checks for overflows
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff changeset
25 test_assert(memcmp(s1, s2, sizeof(struct test_struct) * 2) == 0);
54260e47d2e1 lib: Add i_realloc_type() for i_realloc() that checks for overflows
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff changeset
26
54260e47d2e1 lib: Add i_realloc_type() for i_realloc() that checks for overflows
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff changeset
27 /* realloc */
54260e47d2e1 lib: Add i_realloc_type() for i_realloc() that checks for overflows
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff changeset
28 s1 = i_realloc_type(s1, struct test_struct, 2, 4);
54260e47d2e1 lib: Add i_realloc_type() for i_realloc() that checks for overflows
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff changeset
29 s2 = i_realloc(s2, sizeof(struct test_struct) * 2,
54260e47d2e1 lib: Add i_realloc_type() for i_realloc() that checks for overflows
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff changeset
30 sizeof(struct test_struct) * 4);
54260e47d2e1 lib: Add i_realloc_type() for i_realloc() that checks for overflows
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff changeset
31 s1[2] = cd; s2[2] = cd;
54260e47d2e1 lib: Add i_realloc_type() for i_realloc() that checks for overflows
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff changeset
32 s1[3] = de; s2[3] = de;
54260e47d2e1 lib: Add i_realloc_type() for i_realloc() that checks for overflows
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff changeset
33 test_assert(memcmp(&s1[0], &ab, sizeof(ab)) == 0);
54260e47d2e1 lib: Add i_realloc_type() for i_realloc() that checks for overflows
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff changeset
34 test_assert(memcmp(&s1[1], &bc, sizeof(bc)) == 0);
54260e47d2e1 lib: Add i_realloc_type() for i_realloc() that checks for overflows
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff changeset
35 test_assert(memcmp(&s1[2], &cd, sizeof(cd)) == 0);
54260e47d2e1 lib: Add i_realloc_type() for i_realloc() that checks for overflows
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff changeset
36 test_assert(memcmp(&s1[3], &de, sizeof(de)) == 0);
54260e47d2e1 lib: Add i_realloc_type() for i_realloc() that checks for overflows
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff changeset
37 test_assert(memcmp(s1, s2, sizeof(struct test_struct) * 4) == 0);
54260e47d2e1 lib: Add i_realloc_type() for i_realloc() that checks for overflows
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff changeset
38
54260e47d2e1 lib: Add i_realloc_type() for i_realloc() that checks for overflows
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff changeset
39 /* freeing realloced memory */
54260e47d2e1 lib: Add i_realloc_type() for i_realloc() that checks for overflows
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff changeset
40 i_free(s1);
54260e47d2e1 lib: Add i_realloc_type() for i_realloc() that checks for overflows
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff changeset
41 i_free(s2);
54260e47d2e1 lib: Add i_realloc_type() for i_realloc() that checks for overflows
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff changeset
42 test_assert(s1 == NULL);
54260e47d2e1 lib: Add i_realloc_type() for i_realloc() that checks for overflows
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff changeset
43 test_assert(s2 == NULL);
54260e47d2e1 lib: Add i_realloc_type() for i_realloc() that checks for overflows
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff changeset
44
54260e47d2e1 lib: Add i_realloc_type() for i_realloc() that checks for overflows
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff changeset
45 /* allcating new memory with realloc */
54260e47d2e1 lib: Add i_realloc_type() for i_realloc() that checks for overflows
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff changeset
46 s1 = i_realloc_type(NULL, struct test_struct, 0, 2);
54260e47d2e1 lib: Add i_realloc_type() for i_realloc() that checks for overflows
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff changeset
47 s2 = i_realloc(NULL, 0, sizeof(struct test_struct) * 2);
54260e47d2e1 lib: Add i_realloc_type() for i_realloc() that checks for overflows
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff changeset
48 s1[0] = ab; s2[0] = ab;
54260e47d2e1 lib: Add i_realloc_type() for i_realloc() that checks for overflows
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff changeset
49 s1[1] = bc; s2[1] = bc;
54260e47d2e1 lib: Add i_realloc_type() for i_realloc() that checks for overflows
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff changeset
50 test_assert(memcmp(s1, s2, sizeof(struct test_struct) * 2) == 0);
54260e47d2e1 lib: Add i_realloc_type() for i_realloc() that checks for overflows
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff changeset
51
54260e47d2e1 lib: Add i_realloc_type() for i_realloc() that checks for overflows
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff changeset
52 i_free(s1);
54260e47d2e1 lib: Add i_realloc_type() for i_realloc() that checks for overflows
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff changeset
53 i_free(s2);
54260e47d2e1 lib: Add i_realloc_type() for i_realloc() that checks for overflows
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff changeset
54
54260e47d2e1 lib: Add i_realloc_type() for i_realloc() that checks for overflows
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff changeset
55 test_end();
54260e47d2e1 lib: Add i_realloc_type() for i_realloc() that checks for overflows
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff changeset
56 }
54260e47d2e1 lib: Add i_realloc_type() for i_realloc() that checks for overflows
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff changeset
57
54260e47d2e1 lib: Add i_realloc_type() for i_realloc() that checks for overflows
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff changeset
58 void test_imem(void)
54260e47d2e1 lib: Add i_realloc_type() for i_realloc() that checks for overflows
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff changeset
59 {
54260e47d2e1 lib: Add i_realloc_type() for i_realloc() that checks for overflows
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff changeset
60 test_imem_alloc();
54260e47d2e1 lib: Add i_realloc_type() for i_realloc() that checks for overflows
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff changeset
61 }