Skip to content

Commit e4dcd9f

Browse files
authored
Merge pull request #271 from Icinga:fix/StackOverflowException
Fix: StackOverflowException on invalid ThresholdInterval By using an invalid `-ThresholdInterval` argument, PowerShell will terminate itself with `StackOverflowException`, caused by a loop while creating default threshold values.
2 parents 16d2677 + 2009fa9 commit e4dcd9f

File tree

3 files changed

+19
-10
lines changed

3 files changed

+19
-10
lines changed

lib/icinga/plugin/Compare-IcingaPluginThresholds.psm1

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ function Compare-IcingaPluginThresholds()
6363
$TimeInterval,
6464
$MinuteInterval
6565
);
66+
67+
return $IcingaThresholds;
6668
}
6769
} <#else {
6870
# The symbol splitting our threshold from the time index value
@@ -130,12 +132,14 @@ function Compare-IcingaPluginThresholds()
130132
}
131133

132134
# Calculate % value from base value of set
133-
if ($null -ne $BaseValue -And $IcingaThresholds.Unit -eq '%') {
135+
if ([string]::IsNullOrEmpty($BaseValue) -eq $FALSE -And $BaseValue -ne 0 -And $IcingaThresholds.Unit -eq '%') {
134136
$InputValue = $InputValue / $BaseValue * 100;
135137
$UseDynamicPercentage = $TRUE;
136-
} elseif ($null -eq $BaseValue -And $IcingaThresholds.Unit -eq '%') {
138+
} elseif (([string]::IsNullOrEmpty($BaseValue) -eq $TRUE -Or $BaseValue -eq 0) -And $IcingaThresholds.Unit -eq '%') {
137139
$IcingaThresholds.HasError = $TRUE;
138140
$IcingaThresholds.ErrorMessage = 'This argument does not support the % unit';
141+
142+
return $IcingaThresholds;
139143
}
140144

141145
# Always override our InputValue, case we might have change it
@@ -386,6 +390,8 @@ function Compare-IcingaPluginThresholds()
386390
$Threshold
387391
);
388392
$IcingaThresholds.HasError = $TRUE;
393+
394+
return $IcingaThresholds;
389395
}
390396
}
391397
}

lib/icinga/plugin/New-IcingaCheck.psm1

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ function New-IcingaCheck()
3333
$IcingaCheck | Add-Member -MemberType NoteProperty -Name '__ThresholdObject' -Value $null;
3434
$IcingaCheck | Add-Member -MemberType NoteProperty -Name '__TimeInterval' -Value $null;
3535

36+
$IcingaCheck | Add-Member -MemberType ScriptMethod -Force -Name 'Compile' -Value {
37+
$this.__ValidateThresholdInput();
38+
if ($null -eq $this.__ThresholdObject) {
39+
$this.__CreateDefaultThresholdObject();
40+
}
41+
$this.__SetCheckOutput();
42+
}
43+
3644
$IcingaCheck | Add-Member -MemberType ScriptMethod -Force -Name '__SetInternalTimeInterval' -Value {
3745
$CallStack = Get-PSCallStack;
3846
[bool]$FoundInterval = $FALSE;
@@ -82,8 +90,8 @@ function New-IcingaCheck()
8290

8391
$IcingaCheck | Add-Member -MemberType ScriptMethod -Force -Name '__CreateDefaultThresholdObject' -Value {
8492
[hashtable]$ThresholdArguments = $this.__GetBaseThresholdArguments();
85-
$ThresholdObject = Compare-IcingaPluginThresholds @ThresholdArguments;
86-
$this.__SetCheckState($ThresholdObject, $IcingaEnums.IcingaExitCode.Ok);
93+
$this.__ThresholdObject = Compare-IcingaPluginThresholds @ThresholdArguments;
94+
$this.__SetCheckState($this.__ThresholdObject, $IcingaEnums.IcingaExitCode.Ok);
8795
}
8896

8997
# Override shared function
@@ -94,10 +102,6 @@ function New-IcingaCheck()
94102
return;
95103
}
96104

97-
if ($null -eq $this.__ThresholdObject) {
98-
$this.__CreateDefaultThresholdObject();
99-
}
100-
101105
$PluginThresholds = '';
102106
$TimeSpan = '';
103107
$PluginThresholds = $this.__ThresholdObject.FullMessage;

lib/icinga/plugin/New-IcingaCheckBaseObject.psm1

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,8 @@ function New-IcingaCheckBaseObject()
112112
);
113113
}
114114

115+
# Shared function
115116
$IcingaCheckBaseObject | Add-Member -MemberType ScriptMethod -Name 'Compile' -Value {
116-
$this.__ValidateThresholdInput();
117-
$this.__SetCheckOutput();
118117
}
119118

120119
$IcingaCheckBaseObject | Add-Member -MemberType ScriptMethod -Name '__SetVerbosity' -Value {

0 commit comments

Comments
 (0)