From 840a2b9c77d67111d7f57df1c901c58ac18d2324 Mon Sep 17 00:00:00 2001 From: Arkadiusz Bokowy Date: Thu, 19 Oct 2023 18:11:25 +0200 Subject: [PATCH] [Linux] Fix GAutoPtr deleter for GSource (#29841) * Fix FD leak in case of g_variant_lookup failure * Fix GSource deleter --- src/platform/GLibTypeDeleter.h | 18 +++++++++++++++++- src/platform/Linux/bluez/BluezEndpoint.cpp | 10 +++++----- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/platform/GLibTypeDeleter.h b/src/platform/GLibTypeDeleter.h index 565403566f60a8..93ff40f61c111d 100644 --- a/src/platform/GLibTypeDeleter.h +++ b/src/platform/GLibTypeDeleter.h @@ -60,6 +60,16 @@ struct GErrorDeleter void operator()(GError * object) { g_error_free(object); } }; +struct GIOChannelDeleter +{ + void operator()(GIOChannel * object) { g_io_channel_unref(object); } +}; + +struct GSourceDeleter +{ + void operator()(GSource * object) { g_source_unref(object); } +}; + struct GVariantDeleter { void operator()(GVariant * object) { g_variant_unref(object); } @@ -110,10 +120,16 @@ struct GAutoPtrDeleter using deleter = GErrorDeleter; }; +template <> +struct GAutoPtrDeleter +{ + using deleter = GIOChannelDeleter; +}; + template <> struct GAutoPtrDeleter { - using deleter = GObjectDeleter; + using deleter = GSourceDeleter; }; template <> diff --git a/src/platform/Linux/bluez/BluezEndpoint.cpp b/src/platform/Linux/bluez/BluezEndpoint.cpp index d6f3196a6b877c..236533c7371221 100644 --- a/src/platform/Linux/bluez/BluezEndpoint.cpp +++ b/src/platform/Linux/bluez/BluezEndpoint.cpp @@ -380,6 +380,11 @@ static gboolean BluezCharacteristicAcquireWrite(BluezGattCharacteristic1 * aChar ChipLogDetail(DeviceLayer, "BluezCharacteristicAcquireWrite is called, conn: %p", conn); + VerifyOrReturnValue( + g_variant_lookup(aOptions, "mtu", "q", &conn->mMtu), FALSE, + ChipLogError(DeviceLayer, "FAIL: No MTU in options in %s", __func__); + g_dbus_method_invocation_return_dbus_error(aInvocation, "org.bluez.Error.InvalidArguments", "MTU negotiation failed")); + if (socketpair(AF_UNIX, SOCK_SEQPACKET | SOCK_NONBLOCK | SOCK_CLOEXEC, 0, fds) < 0) { #if CHIP_ERROR_LOGGING @@ -390,11 +395,6 @@ static gboolean BluezCharacteristicAcquireWrite(BluezGattCharacteristic1 * aChar return FALSE; } - VerifyOrReturnValue( - g_variant_lookup(aOptions, "mtu", "q", &conn->mMtu), FALSE, - ChipLogError(DeviceLayer, "FAIL: No MTU in options in %s", __func__); - g_dbus_method_invocation_return_dbus_error(aInvocation, "org.bluez.Error.InvalidArguments", "MTU negotiation failed")); - channel = g_io_channel_unix_new(fds[0]); g_io_channel_set_encoding(channel, nullptr, nullptr); g_io_channel_set_close_on_unref(channel, TRUE);