Merge branch '3592-dbus-serial-overflow' into 'main'

gdbusconnection: Prevent sending a serial of zero on overflow

Closes #3592

See merge request GNOME/glib!4470
This commit is contained in:
Philip Withnall 2025-02-04 11:09:45 +00:00
commit eec7ba6ef3
2 changed files with 23 additions and 3 deletions

View File

@ -1763,9 +1763,22 @@ g_dbus_connection_send_message_unlocked (GDBusConnection *connection,
return FALSE; return FALSE;
if (flags & G_DBUS_SEND_MESSAGE_FLAGS_PRESERVE_SERIAL) if (flags & G_DBUS_SEND_MESSAGE_FLAGS_PRESERVE_SERIAL)
serial_to_use = g_dbus_message_get_serial (message); {
serial_to_use = g_dbus_message_get_serial (message);
}
else else
serial_to_use = ++connection->last_serial; /* TODO: handle overflow */ {
/* The serial_to_use must not be zero, as per
* https://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-messages. */
if (connection->last_serial == G_MAXUINT32)
connection->last_serial = 1;
else
connection->last_serial++;
serial_to_use = connection->last_serial;
}
g_assert (serial_to_use != 0);
switch (blob[0]) switch (blob[0])
{ {

View File

@ -984,10 +984,13 @@ g_dbus_message_get_serial (GDBusMessage *message)
/** /**
* g_dbus_message_set_serial: * g_dbus_message_set_serial:
* @message: A #GDBusMessage. * @message: A #GDBusMessage.
* @serial: A #guint32. * @serial: A #guint32, which must not be zero.
* *
* Sets the serial for @message. * Sets the serial for @message.
* *
* The [D-Bus specification](https://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-messages)
* does not allow the @serial to be zero.
*
* Since: 2.26 * Since: 2.26
*/ */
void void
@ -996,6 +999,10 @@ g_dbus_message_set_serial (GDBusMessage *message,
{ {
g_return_if_fail (G_IS_DBUS_MESSAGE (message)); g_return_if_fail (G_IS_DBUS_MESSAGE (message));
/* As per https://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-messages,
* this must not be zero. */
g_return_if_fail (serial != 0);
if (message->locked) if (message->locked)
{ {
g_warning ("%s: Attempted to modify a locked message", G_STRFUNC); g_warning ("%s: Attempted to modify a locked message", G_STRFUNC);