]> rtime.felk.cvut.cz Git - frescor/ffmpeg.git/commitdiff
Avoid undefined behavior for removing elements that where not in the tree.
authormichael <michael@9553f0bf-9b14-0410-a0b8-cfaf0461ba5b>
Fri, 19 Sep 2008 12:41:12 +0000 (12:41 +0000)
committermichael <michael@9553f0bf-9b14-0410-a0b8-cfaf0461ba5b>
Fri, 19 Sep 2008 12:41:12 +0000 (12:41 +0000)
git-svn-id: file:///var/local/repositories/ffmpeg/trunk@15368 9553f0bf-9b14-0410-a0b8-cfaf0461ba5b

libavutil/tree.c
libavutil/tree.h

index cb442ff4b07aae18472654b82c31e65fb058c483..64653aaccbc6d22cc8cc93e248bc918684d3f209 100644 (file)
@@ -119,8 +119,11 @@ void *av_tree_insert(AVTreeNode **tp, void *key, int (*cmp)(void *key, const voi
         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 @@ int main(void){
         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);
index c457234b09c4a249e297a28153897acf58b02725..ed6611c520d60550ad0e2a4666f2fbe15020d599 100644 (file)
@@ -45,8 +45,7 @@ void *av_tree_find(const struct AVTreeNode *root, void *key, int (*cmp)(void *ke
 
 /**
  * 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