Skip to content
Merged
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
8 changes: 7 additions & 1 deletion src/libcrun/cgroup-utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,13 @@ rmdir_all_fd (int dfd)

child_dfd = openat (dfd, name, O_DIRECTORY | O_CLOEXEC);
if (child_dfd < 0)
return child_dfd;
{
int saved_errno = errno;
closedir (dir);
dir = NULL;
Comment on lines +384 to +385

Choose a reason for hiding this comment

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

medium

Closing the dir stream and explicitly setting it to NULL after use is important for robust resource management. This prevents potential issues like double-closing or use-after-free, especially if dir is a cleanup_dir variable.

errno = saved_errno;
Comment on lines +383 to +386

Choose a reason for hiding this comment

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

high

The introduction of saved_errno and its restoration after closedir is crucial. Without this, a failure in closedir could overwrite the original errno from openat, leading to misleading error reports for the rmdir_all_fd function's caller. This change ensures accurate error propagation.

return child_dfd;
}

ret = read_pids_cgroup (child_dfd, true, &pids, &n_pids, &allocated, &tmp_err);
if (UNLIKELY (ret < 0))
Expand Down
Loading