From: Philippe Proulx Date: Mon, 1 Feb 2016 13:21:00 +0000 (-0500) Subject: BT_MOVE(): call bt_put(_dst) X-Git-Url: http://git.efficios.com/?a=commitdiff_plain;h=f62d1f558a69c03abae7ae054e135e4478655bf9;p=babeltrace.git BT_MOVE(): call bt_put(_dst) 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 --- diff --git a/include/babeltrace/ref.h b/include/babeltrace/ref.h index f03f8c9b..68f872cf 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 34fbc82c..d10ae0cf 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);