diff options
author | Aleksander Morgado <aleksander@aleksander.es> | 2021-06-24 15:40:21 +0200 |
---|---|---|
committer | Aleksander Morgado <aleksander@aleksander.es> | 2021-06-24 15:51:04 +0200 |
commit | dfeaec99ceb3ec5b6fd6ec7add1ce81978d68cfb (patch) | |
tree | c85f49b344ba0d305d9d919636109b83a635121c /libmm-glib | |
parent | 059f13ffd33d6cf8c981119dd40e63e7abc2fe59 (diff) |
libmm-glib,messaging: avoid array cleanup in get_supported_storages()
If we return a reference to the internal array in
ensure_internal_supported_storages() and then we free the array
unconditionally in the caller, the next time we call it, the array
would be valid, but it would be fully empty.
Diffstat (limited to 'libmm-glib')
-rw-r--r-- | libmm-glib/mm-modem-messaging.c | 44 |
1 files changed, 26 insertions, 18 deletions
diff --git a/libmm-glib/mm-modem-messaging.c b/libmm-glib/mm-modem-messaging.c index cc912a05..2f23f8a1 100644 --- a/libmm-glib/mm-modem-messaging.c +++ b/libmm-glib/mm-modem-messaging.c @@ -115,10 +115,13 @@ supported_storages_updated (MMModemMessaging *self, g_mutex_unlock (&self->priv->supported_storages_mutex); } -static void -ensure_internal_supported_storages (MMModemMessaging *self, - GArray **dup) +static gboolean +ensure_internal_supported_storages (MMModemMessaging *self, + MMSmsStorage **dup_storages, + guint *dup_storages_n) { + gboolean ret; + g_mutex_lock (&self->priv->supported_storages_mutex); { /* If this is the first time ever asking for the array, setup the @@ -140,10 +143,24 @@ ensure_internal_supported_storages (MMModemMessaging *self, NULL); } - if (dup && self->priv->supported_storages) - *dup = g_array_ref (self->priv->supported_storages); + if (!self->priv->supported_storages) + ret = FALSE; + else { + ret = TRUE; + + if (dup_storages && dup_storages_n) { + *dup_storages_n = self->priv->supported_storages->len; + if (self->priv->supported_storages->len > 0) { + *dup_storages = g_malloc (sizeof (MMSmsStorage) * self->priv->supported_storages->len); + memcpy (*dup_storages, self->priv->supported_storages->data, sizeof (MMSmsStorage) * self->priv->supported_storages->len); + } else + *dup_storages = NULL; + } + } } g_mutex_unlock (&self->priv->supported_storages_mutex); + + return ret; } /** @@ -165,19 +182,11 @@ mm_modem_messaging_get_supported_storages (MMModemMessaging *self, MMSmsStorage **storages, guint *n_storages) { - GArray *array = NULL; - g_return_val_if_fail (MM_IS_MODEM_MESSAGING (self), FALSE); g_return_val_if_fail (storages != NULL, FALSE); g_return_val_if_fail (n_storages != NULL, FALSE); - ensure_internal_supported_storages (self, &array); - if (!array) - return FALSE; - - *n_storages = array->len; - *storages = (MMSmsStorage *)g_array_free (array, FALSE); - return TRUE; + return ensure_internal_supported_storages (self, storages, n_storages); } /** @@ -194,16 +203,15 @@ mm_modem_messaging_get_supported_storages (MMModemMessaging *self, * Since: 1.0 */ gboolean -mm_modem_messaging_peek_supported_storages (MMModemMessaging *self, +mm_modem_messaging_peek_supported_storages (MMModemMessaging *self, const MMSmsStorage **storages, - guint *n_storages) + guint *n_storages) { g_return_val_if_fail (MM_IS_MODEM_MESSAGING (self), FALSE); g_return_val_if_fail (storages != NULL, FALSE); g_return_val_if_fail (n_storages != NULL, FALSE); - ensure_internal_supported_storages (self, NULL); - if (!self->priv->supported_storages) + if (!ensure_internal_supported_storages (self, NULL, NULL)) return FALSE; *n_storages = self->priv->supported_storages->len; |