From 7311ada5bf1bdb10006e02d21985791ac1b300b3 Mon Sep 17 00:00:00 2001 From: Arkadiusz Bokowy Date: Mon, 16 Oct 2023 10:19:24 +0200 Subject: [PATCH 1/2] Fix FD leak in case of g_variant_lookup failure --- src/platform/Linux/bluez/BluezEndpoint.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) 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); From e41bf12409bd5b4df271a06363440bcfd1768af5 Mon Sep 17 00:00:00 2001 From: Arkadiusz Bokowy Date: Mon, 16 Oct 2023 21:09:15 +0200 Subject: [PATCH 2/2] Fix GSource deleter --- src/platform/GLibTypeDeleter.h | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) 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 <>