Skip to content

Commit

Permalink
[Linux] Fix GAutoPtr deleter for GSource (#29841)
Browse files Browse the repository at this point in the history
* Fix FD leak in case of g_variant_lookup failure

* Fix GSource deleter
  • Loading branch information
arkq authored and pull[bot] committed Feb 29, 2024
1 parent 0974385 commit 484a8b1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
18 changes: 17 additions & 1 deletion src/platform/GLibTypeDeleter.h
Original file line number Diff line number Diff line change
Expand Up @@ -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); }
Expand Down Expand Up @@ -110,10 +120,16 @@ struct GAutoPtrDeleter<GError>
using deleter = GErrorDeleter;
};

template <>
struct GAutoPtrDeleter<GIOChannel>
{
using deleter = GIOChannelDeleter;
};

template <>
struct GAutoPtrDeleter<GSource>
{
using deleter = GObjectDeleter;
using deleter = GSourceDeleter;
};

template <>
Expand Down
10 changes: 5 additions & 5 deletions src/platform/Linux/bluez/BluezEndpoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand Down

0 comments on commit 484a8b1

Please sign in to comment.