Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

procstat: Fix bug with procstat compartments #2323

Merged
merged 1 commit into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that if you initialized inbuf to NULL at the start of the function you could consolidate this down to a single label (but as a followup commit if you wanted to do that).

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 @@
xo_emit("{T:/%5s %-19s %4s %-40s}\n", "PID", "COMM", "CID",
"CNAME");
if (procstat_getcompartments(procstat, kipp, &cccp, &ncomparts) != 0)
goto out;
return;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is the only real fix FWIW. It's not unusual to say that the value of arguments passed by pointer is unchanged/undefined if a function fails. Or put another way, it is only assigned a defined value if the function succeeds and it is an error to use the value if the function fails.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good to me to just leave the inout pointers untouched if there's an error. The diff is also smaller this way.

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);
}

Check warning on line 62 in usr.bin/procstat/procstat_compartments.c

View workflow job for this annotation

GitHub Actions / Style Checker

Missing Signed-off-by: line