Browse Source

Avoid undefined behavior for removing elements that were not in the tree.

Originally committed as revision 15368 to svn://svn.ffmpeg.org/ffmpeg/trunk
tags/v0.5
Michael Niedermayer 16 years ago
parent
commit
eed3607564
2 changed files with 7 additions and 6 deletions
  1. +6
    -4
      libavutil/tree.c
  2. +1
    -2
      libavutil/tree.h

+ 6
- 4
libavutil/tree.c View File

@@ -119,8 +119,11 @@ void *av_tree_insert(AVTreeNode **tp, void *key, int (*cmp)(void *key, const voi
return ret; return ret;
}else{ }else{
*tp= *next; *next= NULL; *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); av_tree_insert(&root, (void*)(j+1), cmp, &node);


j= (random()%86294); j= (random()%86294);
k= av_tree_find(root, (void*)(j+1), cmp, NULL);
if(k){
{
AVTreeNode *node2=NULL; AVTreeNode *node2=NULL;
av_log(NULL, AV_LOG_ERROR, "removing %4d\n", j); av_log(NULL, AV_LOG_ERROR, "removing %4d\n", j);
av_tree_insert(&root, (void*)(j+1), cmp, &node2); av_tree_insert(&root, (void*)(j+1), cmp, &node2);


+ 1
- 2
libavutil/tree.h View File

@@ -45,8 +45,7 @@ void *av_tree_find(const struct AVTreeNode *root, void *key, int (*cmp)(void *ke


/** /**
* Inserts or removes an element. * 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 * If *next is not NULL then the element supplied will be inserted, unless
* it already exists in the tree. * it already exists in the tree.
* @param rootp A pointer to a pointer to the root node of the tree. Note that * @param rootp A pointer to a pointer to the root node of the tree. Note that


Loading…
Cancel
Save