[PATCH rtems-libbsd master 3/3] rtemsbsd/rtems: Don't leak memory on error

Kinsey Moore kinsey.moore at oarcorp.com
Tue Jan 23 21:53:11 UTC 2024


---
 rtemsbsd/rtems/rtems-bsd-rc-conf.c | 14 ++++++++++++++
 rtemsbsd/rtems/rtems-routes.c      |  4 +++-
 2 files changed, 17 insertions(+), 1 deletion(-)

diff --git a/rtemsbsd/rtems/rtems-bsd-rc-conf.c b/rtemsbsd/rtems/rtems-bsd-rc-conf.c
index d34aafd9..88d98c3e 100644
--- a/rtemsbsd/rtems/rtems-bsd-rc-conf.c
+++ b/rtemsbsd/rtems/rtems-bsd-rc-conf.c
@@ -260,12 +260,14 @@ rc_conf_create(rtems_bsd_rc_conf** rc_conf,
    */
   length = strnlen(text, RTEMS_BSD_RC_CONF_MAX_SIZE);
   if (length == RTEMS_BSD_RC_CONF_MAX_SIZE) {
+    free(_rc_conf);
     errno = E2BIG;
     return -1;
   }
 
   copy = strdup(text);
   if (copy == NULL) {
+    free(_rc_conf);
     errno = ENOMEM;
     return -1;
   }
@@ -286,6 +288,7 @@ rc_conf_create(rtems_bsd_rc_conf** rc_conf,
   lines = malloc(sizeof(char*) * line_count);
   if (lines == NULL) {
     free(copy);
+    free(_rc_conf);
     errno = ENOMEM;
     return -1;
   }
@@ -335,6 +338,13 @@ rc_conf_create(rtems_bsd_rc_conf** rc_conf,
   if (timeout >= 0)
     _rc_conf->waiter = rtems_task_self();
 
+  if (_rc_conf->name == NULL) {
+    free((void*) _rc_conf->lines);
+    free((void*) _rc_conf->data);
+    free(_rc_conf);
+    return -1;
+  }
+
   /*
    * Create the lock.
    */
@@ -343,6 +353,7 @@ rc_conf_create(rtems_bsd_rc_conf** rc_conf,
     free((void*) _rc_conf->name);
     free((void*) _rc_conf->lines);
     free((void*) _rc_conf->data);
+    free(_rc_conf);
     return -1;
   }
 
@@ -796,6 +807,7 @@ rtems_bsd_run_rc_conf_script(const char* name,
   if (sc != RTEMS_SUCCESSFUL) {
     fprintf(stderr, "error: %s: get priority: %s\n",
             name, rtems_status_text(sc));
+    rc_conf_destroy(rc_conf);
     errno = EIO;
     return -1;
   }
@@ -808,6 +820,7 @@ rtems_bsd_run_rc_conf_script(const char* name,
                          &worker);
   if (sc != RTEMS_SUCCESSFUL) {
     fprintf (stderr, "error: worker create: %s", rtems_status_text(sc));
+    rc_conf_destroy(rc_conf);
     errno = EIO;
     return -1;
   }
@@ -817,6 +830,7 @@ rtems_bsd_run_rc_conf_script(const char* name,
                         (rtems_task_argument) rc_conf);
   if (sc != RTEMS_SUCCESSFUL) {
     fprintf (stderr, "error: worker start: %s", rtems_status_text(sc));
+    rc_conf_destroy(rc_conf);
     errno = EIO;
     return - 1;
   }
diff --git a/rtemsbsd/rtems/rtems-routes.c b/rtemsbsd/rtems/rtems-routes.c
index 6663e8d4..0b5250f0 100644
--- a/rtemsbsd/rtems/rtems-routes.c
+++ b/rtemsbsd/rtems/rtems-routes.c
@@ -85,8 +85,10 @@ int rtems_get_route(const struct sockaddr_in* sin, struct sockaddr** rti_info)
   }
 
   s = socket(AF_ROUTE, SOCK_RAW, AF_UNSPEC);
-  if (s < 0)
+  if (s < 0) {
+    free(buf);
     return -1;
+  }
 
   rtm = (struct rt_msghdr *) buf;
   rtm->rtm_msglen = sizeof(struct rt_msghdr) + sizeof(struct sockaddr_in);
-- 
2.39.2



More information about the devel mailing list