# HG changeset patch # User michael # Date 1221828072 0 # Node ID 5877edf05eb727a4b43126e7913dd023c0733100 # Parent 1d948bd9850c084b3d83c402876bcf4437db803e Avoid undefined behavior for removing elements that were not in the tree. diff -r 1d948bd9850c -r 5877edf05eb7 tree.c --- a/tree.c Mon Sep 15 22:10:28 2008 +0000 +++ b/tree.c Fri Sep 19 12:41:12 2008 +0000 @@ -119,8 +119,11 @@ return ret; }else{ *tp= *next; *next= NULL; - (*tp)->elem= key; - return NULL; + if(*tp){ + (*tp)->elem= key; + return NULL; + }else + return key; } } @@ -188,8 +191,7 @@ av_tree_insert(&root, (void*)(j+1), cmp, &node); j= (random()%86294); - k= av_tree_find(root, (void*)(j+1), cmp, NULL); - if(k){ + { AVTreeNode *node2=NULL; av_log(NULL, AV_LOG_ERROR, "removing %4d\n", j); av_tree_insert(&root, (void*)(j+1), cmp, &node2); diff -r 1d948bd9850c -r 5877edf05eb7 tree.h --- a/tree.h Mon Sep 15 22:10:28 2008 +0000 +++ b/tree.h Fri Sep 19 12:41:12 2008 +0000 @@ -45,8 +45,7 @@ /** * Inserts or removes an element. - * If *next is NULL then the element supplied will be removed, if no such - * element exists behavior is undefined. + * If *next is NULL then the element supplied will be removed if it exists. * If *next is not NULL then the element supplied will be inserted, unless * it already exists in the tree. * @param rootp A pointer to a pointer to the root node of the tree. Note that