]> git.ozlabs.org Git - petitboot/blob - test/lib/list-test.c
test/lib: Avoid array overflow of child_argv[]
[petitboot] / test / lib / list-test.c
1 /*
2  *  Copyright Geoff Levand <geoff@infradead.org>
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; version 2 of the License.
7  *
8  *  This program is distributed in the hope that it will be useful,
9  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  *  GNU General Public License for more details.
12  *
13  *  You should have received a copy of the GNU General Public License
14  *  along with this program; if not, write to the Free Software
15  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16  */
17
18 #if defined(HAVE_CONFIG_H)
19 #include "config.h"
20 #endif
21
22 #include <stdio.h>
23 #include <stdlib.h>
24
25 #include <list/list.h>
26 #include <talloc/talloc.h>
27
28
29 int main(void)
30 {
31         struct item {
32                 struct list_item list;
33                 int value;
34         };
35         STATIC_LIST(tester);
36         struct item *item;
37         struct item *tmp;
38         void *ctx;
39         int i;
40
41         ctx = talloc_new(NULL);
42
43         for (i = 0; i < 5; i++) {
44                 struct item *item = talloc(ctx, struct item);
45
46                 item->value = i;
47
48                 list_add_tail(&tester, &item->list);
49         }
50         
51         i = 0;
52         fprintf(stderr, "-- list_for_each_entry --\n");
53         list_for_each_entry(&tester, item, list) {
54                 fprintf(stderr, "%d: %d: %p -> %p\n", i++, item->value, item, item->list.next);
55         }
56         
57         i = 0;
58         fprintf(stderr, "-- list_for_each_entry_safe --\n");
59         list_for_each_entry_safe(&tester, item, tmp, list) {
60                 fprintf(stderr, "pos: %d: %d: %p -> %p\n", i++, item->value, item, item->list.next);
61                 fprintf(stderr, "tmp:       %p -> %p\n", tmp, (tmp ? tmp->list.next : NULL));
62                 list_remove(&item->list);
63         }
64
65         /* we should see that the list is empty */
66         i = 0;
67         fprintf(stderr, "-- list_for_each_entry(empty) --\n");
68         list_for_each_entry(&tester, item, list) {
69                 fprintf(stderr, "%d: %d: %p -> %p\n", i++, item->value, item, item->list.next);
70         }
71
72         if (i) {
73                 fprintf(stderr, "Error: list should be empty\n");
74                 return EXIT_FAILURE;
75         }
76
77         i = 0;
78         fprintf(stderr, "-- list_for_each_entry_safe(empty) --\n");
79         list_for_each_entry_safe(&tester, item, tmp, list) {
80                 fprintf(stderr, "%d: %d: %p -> %p\n", i++, item->value, item, item->list.next);
81         }
82
83         if (i) {
84                 fprintf(stderr, "Error: list should be empty\n");
85                 return EXIT_FAILURE;
86         }
87         fprintf(stderr, "-- done --\n");
88
89         talloc_free(ctx);
90
91         return EXIT_SUCCESS;
92 }