From f62d1f558a69c03abae7ae054e135e4478655bf9 Mon Sep 17 00:00:00 2001 From: Philippe Proulx Date: Mon, 1 Feb 2016 08:21:00 -0500 Subject: [PATCH] BT_MOVE(): call bt_put(_dst) MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit It makes no semantical sense to move a reference from a variable to another without first putting the destination, if it has one. Otherwise this would be a lost reference, unless it was moved elsewhere or put manually using bt_put() previously, which is why BT_MOVE() should always be used for move semantics and BT_PUT() is preferred to bt_put() to avoid this situation. Signed-off-by: Philippe Proulx Signed-off-by: Jérémie Galarneau --- include/babeltrace/ref.h | 4 ++++ tests/lib/test_bt_values.c | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/include/babeltrace/ref.h b/include/babeltrace/ref.h index f03f8c9b0..68f872cf9 100644 --- a/include/babeltrace/ref.h +++ b/include/babeltrace/ref.h @@ -53,10 +53,14 @@ * then sets _src to NULL, effectively moving the ownership of an * object from one variable to the other. * + * Before assigning _src to _dst, it puts _dst. Therefore it is not safe to + * call this function with an uninitialized value of _dst. + * * @param obj Babeltrace object. */ #define BT_MOVE(_dst, _src) \ do { \ + bt_put(_dst); \ (_dst) = (_src);\ (_src) = NULL; \ } while (0) diff --git a/tests/lib/test_bt_values.c b/tests/lib/test_bt_values.c index 34fbc82ce..d10ae0cf3 100644 --- a/tests/lib/test_bt_values.c +++ b/tests/lib/test_bt_values.c @@ -1112,7 +1112,7 @@ void test_macros(void) { struct bt_value *obj = bt_value_bool_create(); struct bt_value *src; - struct bt_value *dst; + struct bt_value *dst = NULL; assert(obj); BT_PUT(obj); -- 2.34.1