Hello,

While reviewing the code, I found several places where, it seems to me,
previously allocated memory was not being freed.

Thanks,

--
Mikhail Karpov
From 51cf0a29217611af01935a1b44ee501f9b374126 Mon Sep 17 00:00:00 2001
From: Mikhail Karpov <[email protected]>
Date: Tue, 10 Nov 2026 22:54:16 +0700
Subject: [PATCH 4/4] Freeing memory that had not been freed, in several places

---
 ftpfs/dir.c          |  6 +----
 ftpfs/fs.c           | 28 +++++++++++++---------
 libirqhelp/irqhelp.c | 11 +++++++--
 libpager/demuxer.c   | 30 +++++++++++++-----------
 libps/fmt.c          | 56 +++++++++++++++++++++++++-------------------
 libstore/make.c      |  8 +++----
 6 files changed, 79 insertions(+), 60 deletions(-)

diff --git a/ftpfs/dir.c b/ftpfs/dir.c
index 57ab433..81ad934 100644
--- a/ftpfs/dir.c
+++ b/ftpfs/dir.c
@@ -785,11 +785,7 @@ ftpfs_dir_create (struct ftpfs *fs, struct node *node, const char *rmt_path,
   struct ftpfs_dir *new = malloc (sizeof (struct ftpfs_dir));
 
   if (! new)
-    {
-      error_t err = errno;
-      free (new);
-      return err;
-    }
+    return errno;
 
   netfs_nref (node);
 
diff --git a/ftpfs/fs.c b/ftpfs/fs.c
index d3a9307..efbf8f8 100644
--- a/ftpfs/fs.c
+++ b/ftpfs/fs.c
@@ -44,7 +44,7 @@ ftpfs_create (char *rmt_path, int fsid,
   struct ftpfs *new = malloc (sizeof (struct ftpfs));
 
   if (! new)
-    return ENOMEM;
+    return errno;
 
   new->free_conns = 0;
   new->conns = 0;
@@ -66,21 +66,27 @@ ftpfs_create (char *rmt_path, int fsid,
 
   super_root = netfs_make_node (0);
   if (! super_root)
-    err = ENOMEM;
-  else
-    {
-      err = ftpfs_dir_create (new, super_root, rmt_path, &super_root_dir);
-      if (! err)
-	err = ftpfs_dir_null_lookup (super_root_dir, &new->root);
-    }
+    goto free_new;
+
+  err = ftpfs_dir_create (new, super_root, rmt_path, &super_root_dir);
+  if (err)
+    goto free_super_root;
 
+  err = ftpfs_dir_null_lookup (super_root_dir, &new->root);
   if (err)
     {
       hurd_ihash_destroy (&new->inode_mappings);
-      free (new);
+      goto free_super_root;
     }
-  else
-    *fs = new;
 
+  *fs = new;
+  return 0;
+
+ free_super_root:
+  free (super_root);
+
+ free_new:
+  free (new);
+  *fs = NULL;
   return err;
 }
diff --git a/libirqhelp/irqhelp.c b/libirqhelp/irqhelp.c
index e6eeafc..6852bd7 100644
--- a/libirqhelp/irqhelp.c
+++ b/libirqhelp/irqhelp.c
@@ -295,7 +295,7 @@ interrupt_register(int gsi,
   if (err)
     {
       log_error("cannot allocate mach port\n");
-      return NULL;
+      goto free_irq;
     }
 
   irq->port = delivery_port;
@@ -306,10 +306,17 @@ interrupt_register(int gsi,
   if (err)
     {
       log_error("device_intr_register failed with %d\n", err);
-      return NULL;
+      goto deallocate_delivery_port;
     }
 
   return irq;
+
+ deallocate_delivery_port:
+  mach_port_deallocate (mach_task_self (), delivery_port);
+
+ free_irq:
+  free (irq);
+  return NULL;
 }
 
 struct irq *
diff --git a/libpager/demuxer.c b/libpager/demuxer.c
index 4e3f5d1..c649700 100644
--- a/libpager/demuxer.c
+++ b/libpager/demuxer.c
@@ -330,8 +330,8 @@ pager_start_workers (struct port_bucket *pager_bucket,
   requests = malloc (sizeof *requests);
   if (requests == NULL)
     {
-      err = ENOMEM;
-      goto done;
+      err = errno;
+      goto error;
     }
 
   requests->bucket = pager_bucket;
@@ -340,8 +340,8 @@ pager_start_workers (struct port_bucket *pager_bucket,
   requests->queue_in = malloc (sizeof *requests->queue_in);
   if (requests->queue_in == NULL)
     {
-      err = ENOMEM;
-      goto done;
+      err = errno;
+      goto free_requests;
     }
   queue_init (requests->queue_in);
   /* Until the workers are inhibited, both queues are the same.  */
@@ -354,7 +354,7 @@ pager_start_workers (struct port_bucket *pager_bucket,
   /* Make a thread to service paging requests.  */
   err = pthread_create (&t, NULL, service_paging_requests, requests);
   if (err)
-    goto done;
+    goto free_queue;
   pthread_detach (t);
 
   for (i = 0; i < WORKER_COUNT; i++)
@@ -365,19 +365,21 @@ pager_start_workers (struct port_bucket *pager_bucket,
 
       err = pthread_create (&t, NULL, &worker_func, &requests->workers[i]);
       if (err)
-	goto done;
+	goto free_queue;
       pthread_detach (t);
     }
 
-done:
-  if (err)
-    {
-      free (requests);
-      *out_requests = NULL;
-    }
-  else
-    *out_requests = requests;
+  *out_requests = requests;
+  return 0;
+
+ free_queue:
+  free (requests->queue_in);
+
+ free_requests:
+  free (requests);
 
+ error:
+  *out_requests = NULL;
   return err;
 }
 
diff --git a/libps/fmt.c b/libps/fmt.c
index 0f419a5..ac443c7 100644
--- a/libps/fmt.c
+++ b/libps/fmt.c
@@ -39,6 +39,7 @@ _fmt_create (char *src, int posix, struct ps_fmt_specs *fmt_specs,
   struct ps_fmt *new_fmt;
   int needs = 0;
   int fields_alloced = 10;
+  error_t err;
   /* Initial values for CLR_FLAGS & INV_FLAGS, so the user may specify
      string-wide defaults.  */
   int global_clr_flags = 0, global_inv_flags = 0;
@@ -46,13 +47,13 @@ _fmt_create (char *src, int posix, struct ps_fmt_specs *fmt_specs,
   struct ps_fmt_field *field = fields; /* current last field */
 
   if (fields == NULL)
-    return ENOMEM;
+    return errno;
 
   new_fmt = NEW (struct ps_fmt);
   if (new_fmt == NULL)
     {
-      FREE (fields);
-      return ENOMEM;
+      err = errno;
+      goto free_fields;
     }
 
   /* Make a private copy of SRC so we can mutate it.  */
@@ -60,9 +61,8 @@ _fmt_create (char *src, int posix, struct ps_fmt_specs *fmt_specs,
   new_fmt->src = strdup (src);
   if (new_fmt->src == NULL)
     {
-      FREE (fields);
-      FREE (new_fmt);
-      return ENOMEM;
+      err = errno;
+      goto free_new_fmt;
     }
 
   src = new_fmt->src;
@@ -74,17 +74,18 @@ _fmt_create (char *src, int posix, struct ps_fmt_specs *fmt_specs,
 	/* Time to grow FIELDS to make room for more.  */
 	{
 	  int offs = field - fields;
+	  struct ps_fmt_field *new_fields;
 
 	  fields_alloced += 10;
-	  fields = GROWVEC (fields, struct ps_fmt_field, fields_alloced);
+	  new_fields = GROWVEC (fields, struct ps_fmt_field, fields_alloced);
 
-	  if (fields == NULL)
+	  if (new_fields == NULL)
 	    {
-	      FREE (new_fmt->src);
-	      FREE (new_fmt);
-	      return ENOMEM;
+	      err = errno;
+	      goto free_src;
 	    }
 
+	  fields = new_fields;
 	  field = fields + offs;
 	}
 
@@ -177,15 +178,14 @@ _fmt_create (char *src, int posix, struct ps_fmt_specs *fmt_specs,
 	      /* if we didn't use any chars, don't loop indefinitely */
 	      if (src == start)
 		{
-		  int err = 0;
 		  if (err_string)
 		    err = asprintf (err_string, "%s: Unknown format spec", src);
+		  if (err == -1)
+		    err = errno;
+		  else
+		    err = EINVAL;
 
-		  FREE (new_fmt->src);
-		  FREE (new_fmt);
-		  FREE (fields);
-
-		  return err != -1 ? EINVAL : ENOMEM;
+		  goto free_src;
 		}
 
 	      global_clr_flags = clr_flags;
@@ -247,15 +247,14 @@ _fmt_create (char *src, int posix, struct ps_fmt_specs *fmt_specs,
 	  if (! field->spec)
 	    /* Failed to find any named spec called NAME.  */
 	    {
-	      int err = 0;
 	      if (err_string)
 		err = asprintf (err_string, "%s: Unknown format spec", name);
+	      if (err == -1)
+		err = errno;
+	      else
+		err = EINVAL;
 
-	      FREE (new_fmt->src);
-	      FREE (fields);
-	      FREE (new_fmt);
-
-	      return err != -1 ? EINVAL : ENOMEM;
+	      goto free_src;
 	    }
 
 	  if (! field->title)
@@ -313,8 +312,17 @@ _fmt_create (char *src, int posix, struct ps_fmt_specs *fmt_specs,
   new_fmt->error = "?";
 
   *fmt = new_fmt;
-
   return 0;
+
+ free_src:
+  FREE (new_fmt->src);
+
+ free_new_fmt:
+  FREE (new_fmt);
+
+ free_fields:
+  FREE (fields);
+  return err;
 }
 
 /* Make a PS_FMT_T by parsing the string SRC, searching for any named
diff --git a/libstore/make.c b/libstore/make.c
index 8f28953..5a3370b 100644
--- a/libstore/make.c
+++ b/libstore/make.c
@@ -91,10 +91,10 @@ store_free (struct store *store)
   if (store->source != MACH_PORT_NULL)
     mach_port_deallocate (mach_task_self (), store->source);
 
-  if (store->name)
-    free (store->name);
-  if (store->runs)
-    free (store->runs);
+  free (store->name);
+  free (store->runs);
+  free (store->misc);
+  free (store->children);
 
   free (store);
 }
-- 
2.43.0

Reply via email to