Skip to content

Commit

Permalink
procstat: Fix bug with procstat compartments
Browse files Browse the repository at this point in the history
libprocstat is responsible for allocating the array of compartment
information that is returned to the procstat command-line tool, which is
responsible for freeing it. But procstat should not attempt to free the
array if libprocstat returns an error.

Also fixes a bug that might cause NULL to be dereferenced in
libprocstat.
  • Loading branch information
dpgao committed Feb 12, 2025
1 parent 93a7ecb commit 6d295c1
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 3 deletions.
1 change: 0 additions & 1 deletion lib/libprocstat/libprocstat.c
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,6 @@ procstat_getcompartments(struct procstat *procstat, struct kinfo_proc *kp,
out_free:
free(inbuf);
out:
*ncompartsp = 0;
return (-1);
}

Expand Down
3 changes: 1 addition & 2 deletions usr.bin/procstat/procstat_compartments.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,13 @@ procstat_compartments(struct procstat *procstat, struct kinfo_proc *kipp)
xo_emit("{T:/%5s %-19s %4s %-40s}\n", "PID", "COMM", "CID",
"CNAME");
if (procstat_getcompartments(procstat, kipp, &cccp, &ncomparts) != 0)
goto out;
return;
for (size_t i = 0; i < ncomparts; ++i) {
xo_emit("{k:process_id/%5d/%d}", kipp->ki_pid);
xo_emit(" {:command/%-19s/%s}", kipp->ki_comm);
xo_emit(" {:cid/%4d/%zu}", cccp[i].ccc_id);
xo_emit(" {:cname/%-40s/%s}", cccp[i].ccc_name);
xo_emit("\n");
}
out:
free(cccp);
}

0 comments on commit 6d295c1

Please sign in to comment.