Fix bug 8920, null dereference
[Samba/gebeck_regimport.git] / lib / ccan / list / list.c
blob380212ace7cb117011c53437b31b216fae1c4551
1 /* Licensed under LGPLv2.1+ - see LICENSE file for details */
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include "list.h"
6 static void *corrupt(const char *abortstr,
7 const struct ccan_list_node *head,
8 const struct ccan_list_node *node,
9 unsigned int count)
11 if (abortstr) {
12 fprintf(stderr,
13 "%s: prev corrupt in node %p (%u) of %p\n",
14 abortstr, node, count, head);
15 abort();
17 return NULL;
20 struct ccan_list_node *ccan_list_check_node(const struct ccan_list_node *node,
21 const char *abortstr)
23 const struct ccan_list_node *p, *n;
24 int count = 0;
26 for (p = node, n = node->next; n != node; p = n, n = n->next) {
27 count++;
28 if (n->prev != p)
29 return corrupt(abortstr, node, n, count);
31 /* Check prev on head node. */
32 if (node->prev != p)
33 return corrupt(abortstr, node, node, 0);
35 return (struct ccan_list_node *)node;
38 struct ccan_list_head *ccan_list_check(const struct ccan_list_head *h, const char *abortstr)
40 if (!ccan_list_check_node(&h->n, abortstr))
41 return NULL;
42 return (struct ccan_list_head *)h;