diff options
author | Simon McVittie <smcv@collabora.com> | 2017-07-03 17:59:10 +0100 |
---|---|---|
committer | Simon McVittie <smcv@collabora.com> | 2017-11-24 12:17:17 +0000 |
commit | e49a21e357ef9de4c87cd6ab014d207a55bdb4e5 (patch) | |
tree | 4d320020d78af22da65a9d0211f6fca556fa0ecb /dbus/dbus-string-util.c | |
parent | 608a453b73a9f5b165d20f08cfb0f17a3147f514 (diff) | |
download | dbus-e49a21e357ef9de4c87cd6ab014d207a55bdb4e5.tar.gz |
DBusString: Add _DBUS_STRING_INIT_INVALID and allow "freeing" it
This means we can finally use patterns like this:
DBusString buffer = _DBUS_STRING_INIT_INVALID;
dbus_bool_t ret = FALSE;
... some long setup ...
if (!_dbus_string_init (&buffer))
goto out;
... some long operation ...
ret = TRUE;
out:
... free things ...
_dbus_string_free (&buffer);
... free more things ...
return ret;
without having to have a separate boolean to track whether buffer has
been initialized.
One observable difference is that if s is a "const" (borrowed pointer)
string, _dbus_string_free (&s) now sets it to be invalid. Previously,
it would have kept its (borrowed pointer) contents, which seems like
a violation of least-astonishment.
Signed-off-by: Simon McVittie <smcv@collabora.com>
Reviewed-by: Philip Withnall <withnall@endlessm.com>
Bug: https://bugs.freedesktop.org/show_bug.cgi?id=89104
Diffstat (limited to 'dbus/dbus-string-util.c')
-rw-r--r-- | dbus/dbus-string-util.c | 10 |
1 files changed, 9 insertions, 1 deletions
diff --git a/dbus/dbus-string-util.c b/dbus/dbus-string-util.c index 08e0e917..a1390fea 100644 --- a/dbus/dbus-string-util.c +++ b/dbus/dbus-string-util.c @@ -228,7 +228,7 @@ _DBUS_STRING_DEFINE_STATIC (test_static_string, "hello"); dbus_bool_t _dbus_string_test (void) { - DBusString str; + DBusString str = _DBUS_STRING_INIT_INVALID; DBusString other; int i, a, end; long v; @@ -245,6 +245,11 @@ _dbus_string_test (void) _dbus_assert (real_test_static_string->valid); _dbus_assert (real_test_static_string->align_offset == 0); + /* Test that _DBUS_STRING_INIT_INVALID has the desired effect */ + _dbus_string_free (&str); + _dbus_string_free (&str); + _dbus_string_free (&str); + /* Test shortening and setting length */ i = 0; while (i < _DBUS_N_ELEMENTS (lens)) @@ -270,6 +275,9 @@ _dbus_string_test (void) } _dbus_string_free (&str); + /* Test that a cleared string is effectively _DBUS_STRING_INIT_INVALID */ + _dbus_string_free (&str); + _dbus_string_free (&str); ++i; } |