Fix: invalid use of destructor in dynamic pointer array
authorJérémie Galarneau <jeremie.galarneau@efficios.com>
Mon, 18 Nov 2019 19:43:06 +0000 (14:43 -0500)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Mon, 18 Nov 2019 19:43:06 +0000 (14:43 -0500)
A dynamic pointer array is built on top of a dynamic array and uses
the dynamic array's internal "destructor" field to store the
user-specified destructor.

lttng_dynamic_pointer_array_remove_pointer currently uses
the dynamic array's remove_element directly which causes the
user destructor to be called with the underlying storage of the
pointer rather than with the pointer itself.

This change re-uses the same pattern as
lttng_dynamic_pointer_array_reset(), namely using the destructor
explicitly and setting it to NULL for the duration of the call to
the dynamic array API.

Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
src/common/dynamic-array.c
src/common/dynamic-array.h

index ac2a6d5aacfae4b33e5cd0b2a0c7954bef7003d1..094e269a4fe6e9d8245f517ab1d9e692a5ba8d94 100644 (file)
@@ -95,6 +95,28 @@ void lttng_dynamic_pointer_array_init(
        lttng_dynamic_array_init(&array->array, sizeof(void *), destructor);
 }      
 
+LTTNG_HIDDEN
+int lttng_dynamic_pointer_array_remove_pointer(
+               struct lttng_dynamic_pointer_array *array, size_t index)
+{
+       int ret;
+       const lttng_dynamic_array_element_destructor destructor =
+                       array->array.destructor;
+
+       /*
+        * Prevent the destructor from being used by the underlying
+        * dynamic array.
+        */
+       array->array.destructor = NULL;
+       if (destructor) {
+               destructor(lttng_dynamic_pointer_array_get_pointer(array,
+                               index));
+       }
+       ret = lttng_dynamic_array_remove_element(&array->array, index);
+       array->array.destructor = destructor;
+       return ret;
+}
+
 /* Release any memory used by the dynamic array. */
 LTTNG_HIDDEN
 void lttng_dynamic_pointer_array_reset(
@@ -108,6 +130,10 @@ void lttng_dynamic_pointer_array_reset(
                                        array, i);
                        array->array.destructor(ptr);
                }
+               /*
+                * Prevent the destructor from being used by the underlying
+                * dynamic array.
+                */
                array->array.destructor = NULL;
        }
        lttng_dynamic_array_reset(&array->array);
index e97b51d5d0d1d0ff5eebf9e7cddd1fedfa9112f2..1fbc29b70ac06285a30fc4ac21c60a5f081165af 100644 (file)
@@ -146,12 +146,9 @@ int lttng_dynamic_pointer_array_add_pointer(
  * count is decreased by one and the following pointers are shifted to
  * take the place of the removed pointer (if applicable).
  */
-static inline
+LTTNG_HIDDEN
 int lttng_dynamic_pointer_array_remove_pointer(
-               struct lttng_dynamic_pointer_array *array, size_t index)
-{
-       return lttng_dynamic_array_remove_element(&array->array, index);
-}
+               struct lttng_dynamic_pointer_array *array, size_t index);
 
 /* Release any memory used by the dynamic array. */
 LTTNG_HIDDEN
This page took 0.027339 seconds and 5 git commands to generate.