[PATCH rtems-libbsd 6-freebsd-12 3/5] rtemsbsd/rtems: Don't leak memory on error
Kinsey Moore
kinsey.moore at oarcorp.com
Tue Jan 23 21:54:54 UTC 2024
---
rtemsbsd/rtems/rtems-bsd-mountroot.c | 4 ++--
rtemsbsd/rtems/rtems-bsd-rc-conf.c | 14 ++++++++++++++
rtemsbsd/rtems/rtems-routes.c | 4 +++-
3 files changed, 19 insertions(+), 3 deletions(-)
diff --git a/rtemsbsd/rtems/rtems-bsd-mountroot.c b/rtemsbsd/rtems/rtems-bsd-mountroot.c
index 132a08ff..d913e12f 100644
--- a/rtemsbsd/rtems/rtems-bsd-mountroot.c
+++ b/rtemsbsd/rtems/rtems-bsd-mountroot.c
@@ -94,8 +94,6 @@ bsd_mountroot(const char *fstype)
if (vfsp != NULL) {
mp = vfs_mount_alloc(NULLVP, vfsp, "/", cred);
- crfree(cred);
-
error = VFS_MOUNT(mp);
if (error != 0)
panic("Cannot mount root file system: %d", error);
@@ -114,6 +112,8 @@ bsd_mountroot(const char *fstype)
set_rootvnode(mp);
}
+
+ crfree(cred);
}
static void
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