Skip to content

Commit

Permalink
devinfo: Check for malloc errors
Browse files Browse the repository at this point in the history
Correct allocation size where null terminator was not accounted for and use strlcpy.
  • Loading branch information
ktullavik committed Oct 20, 2024
1 parent c4005d2 commit 72fe367
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions usr.sbin/devinfo/devinfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,10 @@ static void
print_kvlist(char *s)
{
char *kv;
char *copy = strdup(s);
char *copy;

if ((copy = strdup(s)) == NULL)
xo_err(1, "No memory!");

while ((kv = strsep(&copy, " ")) != NULL) {
char* k = strsep(&kv, "=");
Expand Down Expand Up @@ -128,7 +131,8 @@ print_resource(struct devinfo_res *res)
end = res->dr_start + res->dr_size - 1;

len = strlen(rman->dm_desc) + 1;
safe_desc = malloc(len);
if ((safe_desc = malloc(len)) == NULL)
xo_err(1, "No memory!");
strlcpy(safe_desc, rman->dm_desc, len);
xml_safe_string(safe_desc);

Expand Down Expand Up @@ -183,6 +187,7 @@ print_device_rman_resources(struct devinfo_rman *rman, void *arg)
struct indent_arg *ia = (struct indent_arg *)arg;
int indent;
char *safe_desc;
size_t len;

indent = ia->indent;

Expand All @@ -193,8 +198,11 @@ print_device_rman_resources(struct devinfo_rman *rman, void *arg)

/* there are, print header */

safe_desc = malloc(strlen(rman->dm_desc));
strcpy(safe_desc, rman->dm_desc);
len = strlen(rman->dm_desc) + 1;
if ((safe_desc = malloc(len)) == NULL) {
xo_err(1, "No memory!");
}
strlcpy(safe_desc, rman->dm_desc, len);
xml_safe_string(safe_desc);

print_indent(indent);
Expand Down Expand Up @@ -357,9 +365,13 @@ int
print_rman(struct devinfo_rman *rman, void *arg __unused)
{
char* safe_desc;
size_t len;

safe_desc = malloc(strlen(rman->dm_desc));
strcpy(safe_desc, rman->dm_desc);
len = strlen(rman->dm_desc) + 1;
if ((safe_desc = malloc(len)) == NULL) {
xo_err(1, "No memory!");
}
strlcpy(safe_desc, rman->dm_desc, len);
xml_safe_string(safe_desc);

xo_emit("{d:%s}:\n", rman->dm_desc);
Expand Down

0 comments on commit 72fe367

Please sign in to comment.