]> git.ipfire.org Git - thirdparty/dbus.git/commitdiff
userdb: Make lookups return a const pointer
authorSimon McVittie <smcv@collabora.com>
Tue, 30 Jun 2020 18:13:17 +0000 (19:13 +0100)
committerSimon McVittie <smcv@collabora.com>
Thu, 2 Jul 2020 09:09:05 +0000 (10:09 +0100)
This makes it more obvious that the returned pointer points to a
struct owned by the userdb, which must not be freed or have its
contents modified, and is only valid to dereference until the next
modification to the userdb's underlying hash tables (which in practice
means until the lock is released, because after that we have no
guarantees about what might be going on in another thread).

Signed-off-by: Simon McVittie <smcv@collabora.com>
(cherry picked from commit 6ee66ff7bcc91803111d950512f02651e664f74f)

dbus/dbus-userdb-util.c
dbus/dbus-userdb.c
dbus/dbus-userdb.h

index 888a23e9f53373c91ddf781853fd4514035fbfa2..8353e81f2d6bff9d39edeb3d7e69d108b8a9e1f7 100644 (file)
@@ -240,9 +240,9 @@ _dbus_get_user_id_and_primary_group (const DBusString  *username,
  * @param gid the group ID or #DBUS_GID_UNSET
  * @param groupname group name or #NULL 
  * @param error error to fill in
- * @returns the entry in the database
+ * @returns the entry in the database (borrowed, do not free)
  */
-DBusGroupInfo*
+const DBusGroupInfo *
 _dbus_user_database_lookup_group (DBusUserDatabase *db,
                                   dbus_gid_t        gid,
                                   const DBusString *groupname,
@@ -328,6 +328,8 @@ _dbus_user_database_lookup_group (DBusUserDatabase *db,
           return NULL;
         }
       
+      /* Return a borrowed reference to the DBusGroupInfo owned by the
+       * two hash tables */
       return info;
     }
 }
index 52f927a3d0fc7508ed0533422b54a08e51620c42..b4fa35c8243174909f1e308a3ec3777b48ebe438 100644 (file)
@@ -122,9 +122,9 @@ _dbus_is_a_number (const DBusString *str,
  * @param uid the user ID or #DBUS_UID_UNSET
  * @param username username or #NULL 
  * @param error error to fill in
- * @returns the entry in the database
+ * @returns the entry in the database (borrowed, do not free)
  */
-DBusUserInfo*
+const DBusUserInfo *
 _dbus_user_database_lookup (DBusUserDatabase *db,
                             dbus_uid_t        uid,
                             const DBusString *username,
@@ -211,6 +211,8 @@ _dbus_user_database_lookup (DBusUserDatabase *db,
           return NULL;
         }
       
+      /* Return a borrowed pointer to the DBusUserInfo owned by the
+       * hash tables */
       return info;
     }
 }
index 53fc90b58f4d5c5f6d9959e01bb80c90f49e3b15..9e9ed88af5efbb63cc4f1d10f5825e9c83f1c775 100644 (file)
@@ -76,15 +76,15 @@ dbus_bool_t       _dbus_user_database_get_groupname (DBusUserDatabase     *db,
                                                      DBusError            *error);
 
 DBUS_PRIVATE_EXPORT
-DBusUserInfo*  _dbus_user_database_lookup       (DBusUserDatabase *db,
+const DBusUserInfo *_dbus_user_database_lookup  (DBusUserDatabase *db,
                                                  dbus_uid_t        uid,
                                                  const DBusString *username,
                                                  DBusError        *error);
 DBUS_PRIVATE_EXPORT
-DBusGroupInfo* _dbus_user_database_lookup_group (DBusUserDatabase *db,
-                                                 dbus_gid_t        gid,
-                                                 const DBusString *groupname,
-                                                 DBusError        *error);
+const DBusGroupInfo* _dbus_user_database_lookup_group (DBusUserDatabase *db,
+                                                       dbus_gid_t        gid,
+                                                       const DBusString *groupname,
+                                                       DBusError        *error);
 DBUS_PRIVATE_EXPORT
 void           _dbus_user_info_free_allocated   (DBusUserInfo     *info);
 DBUS_PRIVATE_EXPORT