advertising: Fix dbus response for over-advertising
authorDaniel Winkler <danielwinkler@google.com>
Fri, 14 Aug 2020 22:58:18 +0000 (15:58 -0700)
committerAyush Garg <ayush.garg@samsung.com>
Mon, 12 Apr 2021 09:00:50 +0000 (14:30 +0530)
client_free would always send a dbus method_return to fix the case where
a request to Unregister occurred before the MGMT call returned. However,
in the code path where too many advertisements are registered, this
method_return prevents the failure from being sent properly. This patch
makes sure the reference to the register_advertisement DBusMessage is
not stored in the client structure until the end of
register_advertisement. This ensures that we only respond once, either
in register_advertisement or in client_free, not both.

It also changes the dbus response in the fast unregister_advertisement
case from a method_return to a btd_error_failed, since the registration
was never allowed to complete, and thus was not successful.

The patch was tested in the following ways:

To verify it did not break the segfault fix in
caff2b48ca54bbc57b5da3f63317767489aa5b48, I repro'd the failure by
quickly unregistering after registering, and verified that the segfault
is still fixed with this change.

Ran through our automated tests that register too many advertisements
and verify that the registration fails with the intended "Maximum
Advertisements Reached" error response.

Signed-off-by: Anuj Jain <anuj01.jain@samsung.com>
Signed-off-by: Ayush Garg <ayush.garg@samsung.com>
src/advertising.c

index 559abdd..fffd1ae 100644 (file)
@@ -114,9 +114,13 @@ static void client_free(void *data)
        }
 
        if (client->reg) {
-               g_dbus_send_message(btd_get_dbus_connection(),
-                               dbus_message_new_method_return(client->reg));
+               DBusMessage *reply;
+
+               reply = btd_error_failed(client->reg,
+                                       "Failed to complete registration");
+               g_dbus_send_message(btd_get_dbus_connection(), reply);
                dbus_message_unref(client->reg);
+               client->reg = NULL;
        }
 
        if (client->add_adv_id)
@@ -1147,8 +1151,6 @@ static struct btd_adv_client *client_create(struct btd_adv_manager *manager,
        g_dbus_client_set_proxy_handlers(client->client, client_proxy_added,
                                                        NULL, NULL, client);
 
-       client->reg = dbus_message_ref(msg);
-
        client->data = bt_ad_new();
        if (!client->data)
                goto fail;
@@ -1211,6 +1213,8 @@ static DBusMessage *register_advertisement(DBusConnection *conn,
 
        DBG("Registered advertisement at path %s", match.path);
 
+       client->reg = dbus_message_ref(msg);
+
        queue_push_tail(manager->clients, client);
 
        return NULL;