TODO: talloc: talloc_free() should remove the reference to the current owner
[metze/samba/wip.git] / lib / talloc / talloc.c
index 60a48ad8112593754fe1729f2dc3e243924fc76d..1a316d6d58ded63c0af672ae80a9784db95d9554 100644 (file)
@@ -510,9 +510,13 @@ static inline int _talloc_free(void *ptr)
                 * pointer.
                 */
                is_child = talloc_is_parent(tc->refs, ptr);
-               _talloc_free(tc->refs);
                if (is_child) {
+                       _talloc_free(tc->refs);
                        return _talloc_free(ptr);
+               } else {
+                       /* the first reference becomes the owner */
+                       _talloc_steal(talloc_parent(tc->refs), ptr);
+                       _talloc_free(tc->refs);
                }
                return -1;
        }
@@ -1008,6 +1012,11 @@ void *_talloc_realloc(const void *context, void *ptr, size_t size, const char *n
                return NULL;
        }
 
+       /* don't let anybody try to realloc a talloc_pool */
+       if (unlikely(tc->flags & TALLOC_FLAG_POOL)) {
+               return NULL;
+       }
+
        /* don't shrink if we have less than 1k to gain */
        if ((size < tc->size) && ((tc->size - size) < 1024)) {
                tc->size = size;