[monitoring-plugins] check_load: Bound top process output to available ...
GitHub
git at monitoring-plugins.org
Wed Oct 21 13:20:13 CEST 2026
Module: monitoring-plugins
Branch: master
Commit: 0293a9c97308e4dbe82ebbeaee1359031d75f0cf
Author: sean0407 <sean0407 at gmail.com>
Committer: GitHub <noreply at github.com>
Date: Wed Oct 21 19:13:59 2026 +0800
URL: https://www.monitoring-plugins.org/repositories/monitoring-plugins/commit/?id=0293a9c9
check_load: Bound top process output to available rows (#2377)
check_load: Avoid out-of-bounds top process output - #2377
Track how many top-process rows were collected and only format those rows, including the header, instead of iterating to the requested count.
This fixes an issue where the requested number of processes is higher than the actual amount, which lead to out-of-bounds memory accesses.
Co-authored-by: Sean <sean at SeandeMac-mini.local>
Co-authored-by: Lorenz Kästle <12514511+RincewindsHat at users.noreply.github.com>
---
plugins/check_load.c | 6 ++++--
plugins/t/check_load.t | 6 +++++-
2 files changed, 9 insertions(+), 3 deletions(-)
diff --git a/plugins/check_load.c b/plugins/check_load.c
index 12427e7b..daede37c 100644
--- a/plugins/check_load.c
+++ b/plugins/check_load.c
@@ -63,6 +63,7 @@ void print_help(void);
void print_usage(void);
typedef struct {
int errorcode;
+ size_t lines;
char **top_processes;
} top_processes_result;
static top_processes_result get_top_consuming_processes(unsigned long n_procs_to_show);
@@ -303,8 +304,8 @@ int main(int argc, char **argv) {
config.n_procs_to_show);
if (top_proc.errorcode == OK) {
- // +1 here since the string list contains the header line
- for (unsigned long i = 0; i < config.n_procs_to_show + 1; i++) {
+ // Include the header and only the process lines returned.
+ for (size_t i = 0; i < top_proc.lines; i++) {
mopl_utils_xasprintf(&top_proc_sc.output, "%s\n%s", top_proc_sc.output,
top_proc.top_processes[i]);
}
@@ -530,6 +531,7 @@ static top_processes_result get_top_consuming_processes(unsigned long n_procs_to
result.errorcode = ERROR;
return result;
}
+ result.lines = lines_to_show;
for (unsigned long i = 0; i < lines_to_show; i += 1) {
mopl_utils_xasprintf(&result.top_processes[i], "%s", chld_out.line[i]);
diff --git a/plugins/t/check_load.t b/plugins/t/check_load.t
index fc26bb35..78c4c235 100644
--- a/plugins/t/check_load.t
+++ b/plugins/t/check_load.t
@@ -16,7 +16,7 @@ my $successScaledOutput = "/^LOAD OK - scaled load average: $loadValue, $loadVal
my $failureOutput = "/^LOAD CRITICAL - total load average: $loadValue, $loadValue, $loadValue/";
my $failurScaledOutput = "/^LOAD CRITICAL - scaled load average: $loadValue, $loadValue, $loadValue - total load average: $loadValue, $loadValue, $loadValue/";
-plan tests => 8;
+plan tests => 10;
$res = NPTest->testCmd( "./check_load -w 100,100,100 -c 100,100,100" );
cmp_ok( $res->return_code, 'eq', 0, "load not over 100");
@@ -41,3 +41,7 @@ like( $res->perf_output, "/'load15'=$loadValue;~:100.0+;~:110.0+/", "Test handli
$res = NPTest->testCmd( "./check_load -w 100,100,100 -c 100,100,100 -r" );
cmp_ok( $res->return_code, 'eq', 0, "load not over 100");
# like( $res->output, $successScaledOutput, "Output OK");
+
+$res = NPTest->testCmd( "./check_load -n 100000 -w 1000000 -c 1000000" );
+cmp_ok( $res->return_code, 'eq', 0, "check_load handles fewer processes than requested");
+like( $res->output, qr/Top 100000 CPU time consuming processes/, "Top process heading is included");
More information about the Commits
mailing list