Add code to protect the ptree from multiple insertions of the same node.

This commit is contained in:
matt 2012-07-14 18:16:54 +00:00
parent ecdcf3a9e9
commit 84b1adeab7
1 changed files with 15 additions and 3 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: ptree.c,v 1.7 2012/07/13 01:15:27 matt Exp $ */
/* $NetBSD: ptree.c,v 1.8 2012/07/14 18:16:54 matt Exp $ */
/*-
* Copyright (c) 2008 The NetBSD Foundation, Inc.
@ -40,7 +40,7 @@
#include <sys/types.h>
#include <sys/systm.h>
#include <lib/libkern/libkern.h>
__KERNEL_RCSID(0, "$NetBSD: ptree.c,v 1.7 2012/07/13 01:15:27 matt Exp $");
__KERNEL_RCSID(0, "$NetBSD: ptree.c,v 1.8 2012/07/14 18:16:54 matt Exp $");
#else
#include <stddef.h>
#include <stdint.h>
@ -53,7 +53,7 @@ __KERNEL_RCSID(0, "$NetBSD: ptree.c,v 1.7 2012/07/13 01:15:27 matt Exp $");
#else
#define KASSERT(e) do { } while (/*CONSTCOND*/ 0)
#endif
__RCSID("$NetBSD: ptree.c,v 1.7 2012/07/13 01:15:27 matt Exp $");
__RCSID("$NetBSD: ptree.c,v 1.8 2012/07/14 18:16:54 matt Exp $");
#endif /* _KERNEL || _STANDALONE */
#ifdef _LIBC
@ -458,6 +458,12 @@ ptree_insert_node_common(pt_tree_t *pt, void *item)
pt_insertfunc_t insertfunc;
pt_insertdata_t id;
/*
* If this node already exists in the tree, return failure.
*/
if (target == PT_NODE(pt->pt_root))
return false;
/*
* We need a leaf so we can match against. Until we get a leaf
* we having nothing to test against.
@ -479,6 +485,12 @@ ptree_insert_node_common(pt_tree_t *pt, void *item)
pt_node_t * const ptn = PT_NODE(*id.id_insertp);
id.id_node = *id.id_insertp;
/*
* If this node already exists in the tree, return failure.
*/
if (target == ptn)
return false;
/*
* If we hit a leaf, try to insert target at leaf. We could
* have inlined ptree_insert_leaf here but that would have