Skip to content

system/critmon: fix FILE stream leak on critmon read failure - #4067

Open
Kaben123 wants to merge 2 commits into
apache:masterfrom
Kaben123:critmon-stream-leak
Open

Kaben123 wants to merge 2 commits into
apache:masterfrom
Kaben123:critmon-stream-leak

Conversation

@Kaben123

Copy link
Copy Markdown

Summary

In critmon_process_directory(), when the first fgets() on
/proc/<pid>/critmon returns NULL (the task can exit between fopen() and
the read), the error path jumped to errout_with_filepath and skipped
fclose(stream), leaking the FILE, its fd and its buffer each time. It now
jumps to errout_with_stream, like the success path.

The first commit fixes two existing nxstyle errors (missing blank lines after
declarations) in critmon.c, because the CI style check runs on the whole
touched file. It is whitespace only.

Impact

Bug fix only, no behavior change on the normal path.

Testing

Host: Ubuntu 22.04 x86_64, GCC 13.1.0. Base: apache/nuttx master 5c1746778e8, apache/nuttx-apps master 7f4e538.

sim:nsh + CONFIG_SCHED_CRITMONITOR=y, CONFIG_SYSTEM_CRITMONITOR=y:

nsh> critmon
PRE-EMPTION CALLER            CSECTION CALLER               RUN              TIME             PID   DESCRIPTION
None                          None                          ---------------- ---------------- ----  CPU 0
None                          None                          0.510081193      2.808088665      0     Idle_Task
None                          None                          0.000004938      0.000004938      1     sim_loop_wq
None                          None                          0.000039973      0.000067983      2     hpwork
None                          None                          0.000144388      0.000262198      4     init_main
None                          None                          0.000376213      0.000901651      7     sh
None                          None                          0.000000000      0.000000000      8     critmon

nsh> free

tools/checkpatch.sh -c -u -m -g passes.

yukangzhi added 2 commits November 17, 2026 14:15
Add the missing blank lines after declarations in critmon.c so that the
file passes the whole-file style check.

Whitespace only, no functional change.

Assisted-by: Kiro
Signed-off-by: yukangzhi <yukangzhi@xiaomi.com>
In critmon_process_directory(), when the first fgets() on
/proc/<pid>/critmon returns NULL (the task can exit between the fopen()
and the read, or the read itself can fail), the error path jumped to
errout_with_filepath, skipping fclose(stream) and leaking the open FILE
(and its underlying fd and libc buffer) on every such failure.

Jump to errout_with_stream instead so the stream is closed, matching
the success path which also falls through to errout_with_stream.

Assisted-by: Kiro
Signed-off-by: yukangzhi <yukangzhi@xiaomi.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants