From e0625388d090665c9a514903f500b13ea93dcd20 Mon Sep 17 00:00:00 2001 From: Guillaume Desmottes Date: Wed, 26 Feb 2014 15:18:11 +0100 Subject: [PATCH 03/11] message: change string getter to _dup_ When switching to GVariant we won't be able to guarantee the lifetime of the returned string so best to dup them. --- .../telepathy-glib/telepathy-glib-sections.txt | 6 +-- telepathy-glib/message.c | 43 ++++++++++++---------- telepathy-glib/message.h | 6 +-- telepathy-logger/text-channel.c | 25 ++++++++++--- tests/dbus/cm-message.c | 15 +++++--- 5 files changed, 57 insertions(+), 38 deletions(-) diff --git a/docs/reference/telepathy-glib/telepathy-glib-sections.txt b/docs/reference/telepathy-glib/telepathy-glib-sections.txt index 76643f2..2ef4f3b 100644 --- a/docs/reference/telepathy-glib/telepathy-glib-sections.txt +++ b/docs/reference/telepathy-glib/telepathy-glib-sections.txt @@ -2107,9 +2107,9 @@ tp_message_to_text tp_message_get_message_type tp_message_get_received_timestamp tp_message_get_sent_timestamp -tp_message_get_specific_to_interface -tp_message_get_supersedes -tp_message_get_token +tp_message_dup_specific_to_interface +tp_message_dup_supersedes +tp_message_dup_token tp_message_is_delivery_report tp_message_is_rescued tp_message_is_scrollback diff --git a/telepathy-glib/message.c b/telepathy-glib/message.c index 2d6a3bb..ab80365 100644 --- a/telepathy-glib/message.c +++ b/telepathy-glib/message.c @@ -736,7 +736,7 @@ tp_message_is_mutable (TpMessage *self) } /** - * tp_message_get_token: + * tp_message_dup_token: * @self: a message * * Return this message's identifier in the underlying protocol. This is @@ -746,12 +746,12 @@ tp_message_is_mutable (TpMessage *self) * * If there is no suitable token, return %NULL. * - * Returns: (transfer none): a non-empty opaque identifier, or %NULL if none + * Returns: (transfer full): a non-empty opaque identifier, or %NULL if none * * Since: 0.13.9 */ -const gchar * -tp_message_get_token (TpMessage *self) +gchar * +tp_message_dup_token (TpMessage *self) { const gchar *token; @@ -762,7 +762,7 @@ tp_message_get_token (TpMessage *self) if (tp_str_empty (token)) return NULL; else - return token; + return g_strdup (token); } /** @@ -869,21 +869,19 @@ tp_message_is_rescued (TpMessage *self) } /** - * tp_message_get_supersedes: + * tp_message_dup_supersedes: * @self: a message * * If this message replaces a previous message, return the value of - * tp_message_get_token() for that previous message. Otherwise, return %NULL. + * tp_message_dup_token() for that previous message. Otherwise, return %NULL. * * For instance, a user interface could replace the superseded * message with this message, or grey out the superseded message. * - * Returns: (transfer none): a non-empty opaque identifier, or %NULL if none - * - * Since: 0.13.9 + * Returns: (transfer full): a non-empty opaque identifier, or %NULL if none */ -const gchar * -tp_message_get_supersedes (TpMessage *self) +gchar * +tp_message_dup_supersedes (TpMessage *self) { const gchar *token; @@ -894,11 +892,11 @@ tp_message_get_supersedes (TpMessage *self) if (tp_str_empty (token)) return NULL; else - return token; + return g_strdup (token); } /** - * tp_message_get_specific_to_interface: + * tp_message_dup_specific_to_interface: * @self: a message * * If this message is specific to a particular D-Bus interface and should @@ -907,16 +905,21 @@ tp_message_get_supersedes (TpMessage *self) * * If this message is an ordinary message or delivery report, return %NULL. * - * Returns: (transfer none): a D-Bus interface name, or %NULL for ordinary + * Returns: (transfer full): a D-Bus interface name, or %NULL for ordinary * messages and delivery reports - * - * Since: 0.13.9 */ -const gchar * -tp_message_get_specific_to_interface (TpMessage *self) +gchar * +tp_message_dup_specific_to_interface (TpMessage *self) { + const gchar *interface; + g_return_val_if_fail (TP_IS_MESSAGE (self), NULL); - return tp_asv_get_string (tp_message_peek (self, 0), "interface"); + + interface = tp_asv_get_string (tp_message_peek (self, 0), "interface"); + if (interface == NULL) + return NULL; + + return g_strdup (interface); } /** diff --git a/telepathy-glib/message.h b/telepathy-glib/message.h index e3a767f..608fa6a 100644 --- a/telepathy-glib/message.h +++ b/telepathy-glib/message.h @@ -82,13 +82,13 @@ gchar * tp_message_to_text (TpMessage *message) G_GNUC_WARN_UNUSED_RESULT; gboolean tp_message_is_mutable (TpMessage *self); TpChannelTextMessageType tp_message_get_message_type (TpMessage *self); -const gchar *tp_message_get_token (TpMessage *self); +gchar *tp_message_dup_token (TpMessage *self); gint64 tp_message_get_sent_timestamp (TpMessage *self); gint64 tp_message_get_received_timestamp (TpMessage *self); gboolean tp_message_is_scrollback (TpMessage *self); gboolean tp_message_is_rescued (TpMessage *self); -const gchar *tp_message_get_supersedes (TpMessage *self); -const gchar *tp_message_get_specific_to_interface (TpMessage *self); +gchar *tp_message_dup_supersedes (TpMessage *self); +gchar *tp_message_dup_specific_to_interface (TpMessage *self); gboolean tp_message_is_delivery_report (TpMessage *self); _TP_AVAILABLE_IN_0_16 guint32 tp_message_get_pending_message_id (TpMessage *self, diff --git a/telepathy-logger/text-channel.c b/telepathy-logger/text-channel.c index 7600d94..afffeb8 100644 --- a/telepathy-logger/text-channel.c +++ b/telepathy-logger/text-channel.c @@ -169,10 +169,18 @@ get_network_timestamp (TpMessage *message) static gint64 get_message_edit_timestamp (TpMessage *message) { - if (tp_message_get_supersedes (message) != NULL) - return get_network_timestamp (message); + gchar *supersedes; + + supersedes = tp_message_dup_supersedes (message); + if (supersedes != NULL) + { + g_free (supersedes); + return get_network_timestamp (message); + } else - return 0; + { + return 0; + } } @@ -200,7 +208,7 @@ tpl_text_channel_store_message (TplTextChannel *self, const gchar *direction; TpChannelTextMessageType type; gint64 timestamp; - gchar *text; + gchar *text, *token, *supersedes; TplTextEvent *event; TplLogManager *logmanager; GError *error = NULL; @@ -251,6 +259,9 @@ tpl_text_channel_store_message (TplTextChannel *self, tpl_entity_get_identifier (sender)); + token = tp_message_dup_token (message); + supersedes = tp_message_dup_supersedes (message); + /* Initialise TplTextEvent */ event = g_object_new (TPL_TYPE_TEXT_EVENT, /* TplEvent */ @@ -259,14 +270,16 @@ tpl_text_channel_store_message (TplTextChannel *self, "receiver", receiver, "sender", sender, "timestamp", timestamp, - "message-token", tp_message_get_token (message), - "supersedes-token", tp_message_get_supersedes (message), + "message-token", token, + "supersedes-token", supersedes, "edit-timestamp", get_message_edit_timestamp (message), /* TplTextEvent */ "message-type", type, "message", text, NULL); + g_free (token); + /* Store sent event */ logmanager = tpl_log_manager_dup_singleton (); _tpl_log_manager_add_event (logmanager, TPL_EVENT (event), &error); diff --git a/tests/dbus/cm-message.c b/tests/dbus/cm-message.c index 7ddc5ea..f014fd1 100644 --- a/tests/dbus/cm-message.c +++ b/tests/dbus/cm-message.c @@ -53,6 +53,7 @@ test_new_from_parts (Test *test, GVariant *part_vardict; gboolean valid; const gchar *s; + gchar *token; parts = g_ptr_array_new_full (2, (GDestroyNotify) g_hash_table_unref); @@ -108,13 +109,15 @@ test_new_from_parts (Test *test, g_assert_cmpuint (tp_message_get_message_type (msg), ==, TP_CHANNEL_TEXT_MESSAGE_TYPE_NOTICE); g_assert_cmpuint (tp_cm_message_get_sender (msg), ==, sender); - g_assert_cmpstr (tp_message_get_token (msg), ==, "token"); + token = tp_message_dup_token (msg); + g_assert_cmpstr (token, ==, "token"); + g_free (token); g_assert_cmpint ((gint) tp_message_get_sent_timestamp (msg), ==, 42); g_assert_cmpint ((gint) tp_message_get_received_timestamp (msg), ==, 666); g_assert_cmpint (tp_message_is_scrollback (msg), ==, TRUE); g_assert_cmpint (tp_message_is_rescued (msg), ==, FALSE); - g_assert_cmpstr (tp_message_get_supersedes (msg), ==, NULL); - g_assert_cmpstr (tp_message_get_specific_to_interface (msg), ==, NULL); + g_assert_cmpstr (tp_message_dup_supersedes (msg), ==, NULL); + g_assert_cmpstr (tp_message_dup_specific_to_interface (msg), ==, NULL); g_assert_cmpint (tp_message_is_delivery_report (msg), ==, FALSE); g_assert_cmpuint (tp_message_get_pending_message_id (msg, &valid), ==, 666); g_assert (valid); @@ -156,13 +159,13 @@ test_new_text (Test *test, g_assert_cmpuint (tp_message_get_message_type (msg), ==, TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION); g_assert_cmpuint (tp_cm_message_get_sender (msg), ==, sender); - g_assert_cmpstr (tp_message_get_token (msg), ==, NULL); + g_assert_cmpstr (tp_message_dup_token (msg), ==, NULL); g_assert_cmpint ((gint) tp_message_get_sent_timestamp (msg), ==, 0); g_assert_cmpint ((gint) tp_message_get_received_timestamp (msg), ==, 0); g_assert_cmpint (tp_message_is_scrollback (msg), ==, FALSE); g_assert_cmpint (tp_message_is_rescued (msg), ==, FALSE); - g_assert_cmpstr (tp_message_get_supersedes (msg), ==, NULL); - g_assert_cmpstr (tp_message_get_specific_to_interface (msg), ==, NULL); + g_assert_cmpstr (tp_message_dup_supersedes (msg), ==, NULL); + g_assert_cmpstr (tp_message_dup_specific_to_interface (msg), ==, NULL); g_assert_cmpint (tp_message_is_delivery_report (msg), ==, FALSE); g_object_unref (msg); -- 1.8.5.3