594167
From 6fcc3befd0ca9897071af071287e8758a30046f0 Mon Sep 17 00:00:00 2001
594167
From: Yu Watanabe <watanabe.yu+github@gmail.com>
594167
Date: Sun, 17 Apr 2022 07:20:16 +0900
594167
Subject: [PATCH] sd-bus: introduce ref/unref function for track_item
594167
594167
(cherry picked from commit c2d7dd35d2a8cda439384a385b0c1bec804b9b79)
594167
594167
Related: #2087652
594167
---
594167
 src/libsystemd/sd-bus/bus-track.c | 35 ++++++++++++++-----------------
594167
 1 file changed, 16 insertions(+), 19 deletions(-)
594167
594167
diff --git a/src/libsystemd/sd-bus/bus-track.c b/src/libsystemd/sd-bus/bus-track.c
594167
index 891fd0c899..135dfddc5f 100644
594167
--- a/src/libsystemd/sd-bus/bus-track.c
594167
+++ b/src/libsystemd/sd-bus/bus-track.c
594167
@@ -40,7 +40,6 @@ struct sd_bus_track {
594167
                  "arg0='", name, "'")
594167
 
594167
 static struct track_item* track_item_free(struct track_item *i) {
594167
-
594167
         if (!i)
594167
                 return NULL;
594167
 
594167
@@ -49,7 +48,8 @@ static struct track_item* track_item_free(struct track_item *i) {
594167
         return mfree(i);
594167
 }
594167
 
594167
-DEFINE_TRIVIAL_CLEANUP_FUNC(struct track_item*, track_item_free);
594167
+DEFINE_PRIVATE_TRIVIAL_REF_UNREF_FUNC(struct track_item, track_item, track_item_free);
594167
+DEFINE_TRIVIAL_CLEANUP_FUNC(struct track_item*, track_item_unref);
594167
 DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(track_item_hash_ops, char, string_hash_func, string_compare_func,
594167
                                               struct track_item, track_item_free);
594167
 
594167
@@ -180,7 +180,7 @@ static int on_name_owner_changed(sd_bus_message *message, void *userdata, sd_bus
594167
 }
594167
 
594167
 _public_ int sd_bus_track_add_name(sd_bus_track *track, const char *name) {
594167
-        _cleanup_(track_item_freep) struct track_item *n = NULL;
594167
+        _cleanup_(track_item_unrefp) struct track_item *n = NULL;
594167
         struct track_item *i;
594167
         const char *match;
594167
         int r;
594167
@@ -190,14 +190,8 @@ _public_ int sd_bus_track_add_name(sd_bus_track *track, const char *name) {
594167
 
594167
         i = hashmap_get(track->names, name);
594167
         if (i) {
594167
-                if (track->recursive) {
594167
-                        unsigned k = i->n_ref + 1;
594167
-
594167
-                        if (k < i->n_ref) /* Check for overflow */
594167
-                                return -EOVERFLOW;
594167
-
594167
-                        i->n_ref = k;
594167
-                }
594167
+                if (track->recursive)
594167
+                        track_item_ref(i);
594167
 
594167
                 bus_track_remove_from_queue(track);
594167
                 return 0;
594167
@@ -207,9 +201,14 @@ _public_ int sd_bus_track_add_name(sd_bus_track *track, const char *name) {
594167
         if (r < 0)
594167
                 return r;
594167
 
594167
-        n = new0(struct track_item, 1);
594167
+        n = new(struct track_item, 1);
594167
         if (!n)
594167
                 return -ENOMEM;
594167
+
594167
+        *n = (struct track_item) {
594167
+                .n_ref = 1,
594167
+        };
594167
+
594167
         n->name = strdup(name);
594167
         if (!n->name)
594167
                 return -ENOMEM;
594167
@@ -241,8 +240,7 @@ _public_ int sd_bus_track_add_name(sd_bus_track *track, const char *name) {
594167
                 return r;
594167
         }
594167
 
594167
-        n->n_ref = 1;
594167
-        n = NULL;
594167
+        TAKE_PTR(n);
594167
 
594167
         bus_track_remove_from_queue(track);
594167
         track->modified = true;
594167
@@ -264,14 +262,13 @@ _public_ int sd_bus_track_remove_name(sd_bus_track *track, const char *name) {
594167
         i = hashmap_get(track->names, name);
594167
         if (!i)
594167
                 return -EUNATCH;
594167
-        if (i->n_ref <= 0)
594167
-                return -EUNATCH;
594167
 
594167
-        i->n_ref--;
594167
-
594167
-        if (i->n_ref <= 0)
594167
+        assert(i->n_ref >= 1);
594167
+        if (i->n_ref <= 1)
594167
                 return bus_track_remove_name_fully(track, name);
594167
 
594167
+        track_item_unref(i);
594167
+
594167
         return 1;
594167
 }
594167