Skip to content

Commit 51d4246

Browse files
authored
Fix out of bounds context for short scripts (#58)
* Limit pre and post context by calculating number of lines available in either direction Add test case coverage for issue * Update CHANGELOG.md
1 parent 4b92bbe commit 51d4246

File tree

4 files changed

+47
-2
lines changed

4 files changed

+47
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### Fixes
66

77
- StackTrace parsing on Windows Powershell 5.1 ([#50](https://github.com/getsentry/sentry-powershell/pull/50))
8+
- Fix out of bounds context for short scripts ([#58](https://github.com/getsentry/sentry-powershell/pull/58))
89

910
### Dependencies
1011

modules/Sentry/private/StackTraceProcessor.ps1

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,15 +291,17 @@ class StackTraceProcessor : SentryEventProcessor
291291
{
292292
$sentryFrame.ContextLine = $lines[$sentryFrame.LineNumber - 1]
293293
}
294+
$preContextCount = [math]::Min(5, $sentryFrame.LineNumber - 1)
295+
$postContextCount = [math]::Min(5, $lines.Count - $sentryFrame.LineNumber)
294296
if ($sentryFrame.LineNumber -gt 6)
295297
{
296298
$lines = $lines | Select-Object -Skip ($sentryFrame.LineNumber - 6)
297299
}
298300
# Note: these are read-only in sentry-dotnet so we just update the underlying lists instead of replacing.
299301
$sentryFrame.PreContext.Clear()
300-
$lines | Select-Object -First 5 | ForEach-Object { $sentryFrame.PreContext.Add($_) }
302+
$lines | Select-Object -First $preContextCount | ForEach-Object { $sentryFrame.PreContext.Add($_) }
301303
$sentryFrame.PostContext.Clear()
302-
$lines | Select-Object -Last 5 | ForEach-Object { $sentryFrame.PostContext.Add($_) }
304+
$lines | Select-Object -Last $postContextCount | ForEach-Object { $sentryFrame.PostContext.Add($_) }
303305
}
304306
catch
305307
{

tests/stacktrace.tests.ps1

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
BeforeAll {
22
. "$PSScriptRoot/utils.ps1"
33
. "$PSScriptRoot/throwing.ps1"
4+
. "$PSScriptRoot/throwingshort.ps1"
45
$events = [System.Collections.Generic.List[Sentry.SentryEvent]]::new();
56
$transport = [RecordingTransport]::new()
67
StartSentryForEventTests ([ref] $events) ([ref] $transport)
@@ -77,6 +78,31 @@ BeforeAll {
7778
# A module-based frame should be in-app=false
7879
$frames | Where-Object -Property Module | Select-Object -First 1 -ExpandProperty 'InApp' | Should -Be $false
7980
}
81+
82+
$checkShortErrorRecord = {
83+
$events.Count | Should -Be 1
84+
[Sentry.SentryEvent]$event = $events.ToArray()[0]
85+
$event.SentryExceptions.Count | Should -Be 2
86+
87+
$event.SentryExceptions[1].Type | Should -Be 'Short context test'
88+
$event.SentryExceptions[1].Value | Should -Be 'Short context test'
89+
$event.SentryExceptions[1].Module | Should -BeNullOrEmpty
90+
[Sentry.SentryStackFrame[]] $frames = $event.SentryExceptions[1].Stacktrace.Frames
91+
$frames.Count | Should -BeGreaterThan 1
92+
93+
$frame = GetListItem $frames -1
94+
95+
$frame.Function | Should -Be "funcC"
96+
$frame.AbsolutePath | Should -Be (Join-Path $PSScriptRoot 'throwingshort.ps1')
97+
$frame.LineNumber | Should -BeGreaterThan 0
98+
$frame.InApp | Should -Be $true
99+
100+
$frame.PreContext | Should -Be @('function funcC {')
101+
$frame.PreContext.Count | Should -Be 1
102+
$frame.ContextLine | Should -Be ' throw "Short context test"'
103+
$frame.PostContext | Should -Be @('}')
104+
$frame.PostContext.Count | Should -Be 1
105+
}
80106
}
81107

82108
AfterAll {
@@ -119,6 +145,19 @@ Describe 'Out-Sentry' {
119145
@($null) | ForEach-Object $checkErrorRecord
120146
}
121147

148+
It 'captures short context' {
149+
try
150+
{
151+
funcC
152+
}
153+
catch
154+
{
155+
$_ | Out-Sentry
156+
}
157+
158+
@($null) | ForEach-Object $checkShortErrorRecord
159+
}
160+
122161
It 'captures exception' {
123162
try
124163
{

tests/throwingshort.ps1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
function funcC {
2+
throw "Short context test"
3+
}

0 commit comments

Comments
 (0)