coverity: add model for lvm_register_segtype to fix resource leak warnings
authorZdenek Kabelac <zkabelac@redhat.com>
Fri, 7 Nov 2025 12:08:35 +0000 (13:08 +0100)
committerZdenek Kabelac <zkabelac@redhat.com>
Fri, 7 Nov 2025 12:21:31 +0000 (13:21 +0100)
Add Coverity model for lvm_register_segtype() function to properly track
resource ownership. The model informs Coverity that:

- On failure (returns 0): the function calls segtype->ops->destroy(segtype)
  which frees both segtype->dso field and the segtype structure itself
- On success (returns 1): ownership of segtype is transferred to internal
  list and the resource escapes

This eliminates false positive RESOURCE_LEAK warnings where Coverity
assumed segtype and segtype->dso were leaked when lvm_register_segtype()
failed.

Also added partial definition of struct segment_type with name and dso
fields needed for the model to compile.

Co-Authored-By: Claude <noreply@anthropic.com>
coverity/coverity_model.c

index 340fe9c79757d6dd30dbea7160a14500b55c15a3..df2b794c29aba6f34b6cd872cf245d445bb103cc 100644 (file)
 /* Forward declarations */
 struct lv_segment;
 struct logical_volume;
-struct segment_type;
 struct cmd_context;
 struct profile;
 struct dm_pool;
 struct dm_list;
 
+/* Partial definition of segment_type - only fields we need for modeling */
+struct segment_type {
+       const char *name;
+       const char *dso;
+       /* other fields omitted */
+};
+
 /*
  * These functions never return NULL for valid LVs with segments
  */
@@ -313,3 +319,42 @@ void dm_list_add(struct dm_list *head, struct dm_list *elem)
        __coverity_writeall__(elem);
        __coverity_escape__(elem);
 }
+
+struct segtype_library;
+
+/* lvm_register_segtype - registers a segment type
+ * Returns 1 on success, 0 on failure
+ * On failure, it calls segtype->ops->destroy(segtype) which frees the resource
+ * On success, the segtype is added to the list and ownership is transferred
+ */
+int lvm_register_segtype(struct segtype_library *seglib,
+                        struct segment_type *segtype)
+{
+       int success;
+       char *dso;
+
+       if (!seglib || !segtype) {
+               __coverity_panic__();
+       }
+
+       __coverity_read_pointee__(seglib);
+       __coverity_read_pointee__(segtype);
+
+       if (!success) {
+               /* On failure, the function calls segtype->ops->destroy(segtype)
+                * which frees both segtype->dso and segtype itself */
+
+               /* Read dso field and mark it as freed if it exists */
+               dso = segtype->dso;
+               if (dso)
+                       __coverity_free__(dso);
+
+               /* Then free the segtype structure itself */
+               __coverity_free__(segtype);
+               return 0;
+       }
+
+       /* On success, segtype is added to the list and ownership transfers */
+       __coverity_escape__(segtype);
+       return 1;
+}
This page took 0.090329 seconds and 5 git commands to generate.