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

procstat: Fix bug with procstat compartments #2323

merged 1 commit into from
Feb 12, 2025

Conversation

dpgao
Copy link
Contributor

@dpgao dpgao commented Feb 12, 2025

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.

@dpgao dpgao requested a review from bsdjhb February 12, 2025 13:40
@@ -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;
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.

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.
@dpgao dpgao force-pushed the c18n-procstat-fix branch from d4d5110 to a03d4c0 Compare February 12, 2025 15:58
@@ -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).

@dpgao dpgao merged commit 6d295c1 into dev Feb 12, 2025
29 checks passed
@dpgao dpgao deleted the c18n-procstat-fix branch February 12, 2025 18:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants