From 2c260bb46d539082a3c339adeff13bda0f6da291 Mon Sep 17 00:00:00 2001 From: Andreas Bachlechner <62039342+bachandi@users.noreply.github.com> Date: Mon, 20 Dec 2021 11:18:53 +0100 Subject: [PATCH] Fixes a wrong array size check. It is not wise to check the end of an array by looking at its content. A "0" is otherwise used as invalid array entry. Replaced this usage in nvidiagpu.pm and amgpu.pm by comparing indices with the size directly. --- lib/amdgpu.pm | 8 ++++---- lib/nvidiagpu.pm | 10 +++++----- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/amdgpu.pm b/lib/amdgpu.pm index 50c6d9d..11493f6 100644 --- a/lib/amdgpu.pm +++ b/lib/amdgpu.pm @@ -53,7 +53,7 @@ sub amdgpu_init { # values delimitted by ", " (comma + space) my @gpu_group = split(', ', $amdgpu->{list}->{$k}); for(my $n = 0; $n < $max_number_of_gpus; $n++) { - if($gpu_group[$n]) { + if($n < scalar(@gpu_group)) { my $d = trim($gpu_group[$n]); $d =~ s/^\"//; $d =~ s/\"$//; @@ -543,7 +543,7 @@ sub amdgpu_cgi { push(@tmp, "COMMENT: \\n"); } for($n = 0; $n < $max_number_of_gpus; $n += 1) { - if($d[$n]) { + if($n < scalar(@d)) { my $dstr = trim($d[$n]); my $base = ""; $dstr =~ s/^\"//; @@ -573,7 +573,7 @@ sub amdgpu_cgi { my $value_name = "gpu" . $n . "_val" . $n_sensor; my $value_name2; - push(@tmp, "LINE2:trans_" . $value_name . $LC[$n] . ":$str" . ($n_plot < $main_sensor_plots ? "" : ( $show_current_values ? "\\: \\g" : (($n%2 || !$d[$n+1]) ? "\\n" : "")))); + push(@tmp, "LINE2:trans_" . $value_name . $LC[$n] . ":$str" . ($n_plot < $main_sensor_plots ? "" : ( $show_current_values ? "\\: \\g" : (($n%2 || ($n+1 == scalar(@d))) ? "\\n" : "")))); push(@tmpz, "LINE2:trans_" . $value_name . $LC[$n] . ":$dstr"); if ($n_sensor2) { @@ -612,7 +612,7 @@ sub amdgpu_cgi { push(@tmp, "GPRINT:trans_" . $value_name . ":LAST:" . $legend_labels_per_sensor[$n_sensor] . "\\g"); push(@tmp, "GPRINT:trans_" . $value_name2 . ":LAST: /" . $legend_labels_per_sensor[$n_sensor2] . " (actual/limit)\\n"); } else { - push(@tmp, "GPRINT:trans_" . $value_name . ":LAST:" . $legend_labels_per_sensor[$n_sensor] . (($n%2 || !$d[$n+1]) ? "\\n" : "")); + push(@tmp, "GPRINT:trans_" . $value_name . ":LAST:" . $legend_labels_per_sensor[$n_sensor] . (($n%2 || ($n+1 == scalar(@d))) ? "\\n" : "")); } } } diff --git a/lib/nvidiagpu.pm b/lib/nvidiagpu.pm index 0bb1b73..8c1607e 100644 --- a/lib/nvidiagpu.pm +++ b/lib/nvidiagpu.pm @@ -182,8 +182,8 @@ sub nvidiagpu_update { for($n = 0; $n < $max_number_of_gpus; $n++) { @sensors = ($use_nan_for_missing_data ? (0+"nan") : 0) x $number_of_values_per_gpu_in_rrd; - if($gpu_group[$n]) { - my $str = trim($gpu_group[$n] || ""); + if($n < scalar(@gpu_group)) { + my $str = trim($gpu_group[$n]); open(IN, "nvidia-smi --format=csv,noheader,nounits -i $str --query-gpu=clocks.current.graphics,clocks.current.memory,utilization.gpu,utilization.memory,temperature.gpu,temperature.memory,fan.speed,pstate,power.draw,power.limit,memory.used,memory.total |"); while() { @@ -512,7 +512,7 @@ sub nvidiagpu_cgi { push(@tmp, "COMMENT: \\n"); } for($n = 0; $n < $max_number_of_gpus; $n += 1) { - if($d[$n]) { + if($n < scalar(@d)) { my $dstr = trim($d[$n]); my $base = ""; $dstr =~ s/^\"//; @@ -542,7 +542,7 @@ sub nvidiagpu_cgi { my $value_name = "gpu" . $n . "_val" . $n_sensor; my $value_name2; - push(@tmp, "LINE2:trans_" . $value_name . $LC[$n] . ":$str" . ($n_plot < $main_sensor_plots ? "" : ( $show_current_values ? "\\: \\g" : (($n%2 || !$d[$n+1]) ? "\\n" : "")))); + push(@tmp, "LINE2:trans_" . $value_name . $LC[$n] . ":$str" . ($n_plot < $main_sensor_plots ? "" : ( $show_current_values ? "\\: \\g" : (($n%2 || ($n+1 == scalar(@d))) ? "\\n" : "")))); push(@tmpz, "LINE2:trans_" . $value_name . $LC[$n] . ":$dstr"); if ($n_sensor2) { @@ -566,7 +566,7 @@ sub nvidiagpu_cgi { push(@tmp, "GPRINT:trans_" . $value_name . ":LAST:" . $legend_labels_per_sensor[$n_sensor] . "\\g"); push(@tmp, "GPRINT:trans_" . $value_name2 . ":LAST: /" . $legend_labels_per_sensor[$n_sensor2] . " (actual/limit)\\n"); } else { - push(@tmp, "GPRINT:trans_" . $value_name . ":LAST:" . $legend_labels_per_sensor[$n_sensor] . (($n%2 || !$d[$n+1]) ? "\\n" : "")); + push(@tmp, "GPRINT:trans_" . $value_name . ":LAST:" . $legend_labels_per_sensor[$n_sensor] . (($n%2 || ($n+1 == scalar(@d))) ? "\\n" : "")); } } }