From 0d081f6067c2d6240f1652a98ce5808feb5ea861 Mon Sep 17 00:00:00 2001 From: Shin-ichi MORITA Date: Sat, 17 Jun 2017 20:35:30 +0900 Subject: [PATCH] Fix dbus_message_unref() is not called dbus_message_unref() is not called when blocking pending calls after disconnected. --- dbus/dbus-connection.c | 1 + test/name-test/Makefile.am | 4 +- test/name-test/test-pending-call-disconnected.c | 87 +++++++++++++++++++++++++ 3 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 test/name-test/test-pending-call-disconnected.c diff --git a/dbus/dbus-connection.c b/dbus/dbus-connection.c index f3a18dd7..c55be6fe 100644 --- a/dbus/dbus-connection.c +++ b/dbus/dbus-connection.c @@ -2476,6 +2476,7 @@ _dbus_connection_block_pending_call (DBusPendingCall *pending) /* on OOM error_msg is set to NULL */ complete_pending_call_and_unlock (connection, pending, error_msg); + dbus_message_unref (error_msg); dbus_pending_call_unref (pending); return; } diff --git a/test/name-test/Makefile.am b/test/name-test/Makefile.am index 2ce6bc4b..10468d73 100644 --- a/test/name-test/Makefile.am +++ b/test/name-test/Makefile.am @@ -59,6 +59,7 @@ TESTS += \ test-ids \ test-pending-call-dispatch \ test-pending-call-timeout \ + test-pending-call-disconnected \ test-privserver-client \ test-shutdown \ test-threads-init \ @@ -72,10 +73,11 @@ if DBUS_ENABLE_EMBEDDED_TESTS ## we use noinst_PROGRAMS not check_PROGRAMS for TESTS so that we ## build even when not doing "make check" -noinst_PROGRAMS=test-pending-call-dispatch test-pending-call-timeout test-threads-init test-ids test-shutdown test-privserver test-privserver-client test-autolaunch +noinst_PROGRAMS=test-pending-call-dispatch test-pending-call-timeout test-pending-call-disconnected test-threads-init test-ids test-shutdown test-privserver test-privserver-client test-autolaunch test_pending_call_dispatch_LDADD=$(top_builddir)/dbus/libdbus-1.la test_pending_call_timeout_LDADD=$(top_builddir)/dbus/libdbus-1.la +test_pending_call_disconnected_LDADD=$(top_builddir)/dbus/libdbus-1.la test_threads_init_LDADD=$(top_builddir)/dbus/libdbus-1.la test_ids_LDADD=$(top_builddir)/dbus/libdbus-1.la diff --git a/test/name-test/test-pending-call-disconnected.c b/test/name-test/test-pending-call-disconnected.c new file mode 100644 index 00000000..8de0069e --- /dev/null +++ b/test/name-test/test-pending-call-disconnected.c @@ -0,0 +1,87 @@ +/** +* Test to make sure that pending calls unref error messages +* when blocked after disconnected. +**/ + +#include +#include +#include +#include +#include + +static size_t count = 0; + +static void +free_data (void *data) +{ + --count; + printf ("# Freed: %s\n", (const char*)data); +} + +/* This test outputs TAP syntax: http://testanything.org/ */ +int +main (int argc, char *argv[]) +{ + dbus_int32_t slot_connection = -1; + dbus_int32_t slot_message = -1; + dbus_int32_t slot_pending = -1; + DBusError error; + DBusConnection *conn; + DBusMessage *method; + DBusPendingCall *pending; + DBusMessage *reply; + + printf ("# Testing pending call error\n"); + + dbus_connection_allocate_data_slot (&slot_connection); + dbus_message_allocate_data_slot (&slot_message); + dbus_pending_call_allocate_data_slot (&slot_pending); + + dbus_error_init (&error); + conn = dbus_bus_get_private (DBUS_BUS_SESSION, &error); + dbus_connection_set_data (conn, slot_connection, (void*)"connection", free_data); + ++count; + dbus_connection_set_exit_on_disconnect (conn, FALSE); + + method = dbus_message_new_method_call ("org.freedesktop.TestSuiteEchoService", + "/org/freedesktop/TestSuite", + "org.freedesktop.TestSuite", + "Exit"); + dbus_message_set_data (method, slot_message, (void*)"method", free_data); + ++count; + + dbus_connection_send_with_reply (conn, method, &pending, -1); + dbus_message_unref (method); + dbus_pending_call_set_data (pending, slot_pending, (void*)"pending", free_data); + ++count; + + dbus_connection_close (conn); + + dbus_pending_call_block (pending); + reply = dbus_pending_call_steal_reply (pending); + dbus_pending_call_unref (pending); + if (reply == NULL) + { + printf ("Bail out! Reply is NULL ***\n"); + exit (1); + } + dbus_message_set_data (reply, slot_message, (void*)"reply", free_data); + ++count; + if (dbus_message_get_type (reply) != DBUS_MESSAGE_TYPE_ERROR) + { + printf ("Bail out! Reply is not error ***\n"); + exit (1); + } + dbus_message_unref (reply); + + dbus_connection_unref (conn); + + dbus_connection_free_data_slot (&slot_connection); + dbus_message_free_data_slot (&slot_message); + dbus_pending_call_free_data_slot (&slot_pending); + + printf (count == 0 ? "ok\n" : "not ok # Not all refs were unrefed ***\n"); + + printf ("# Testing completed\n1..1\n"); + exit (0); +} -- 2.13.1