Skip to content

Fix out of bounds context for short scripts #58

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Fixes

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

### Dependencies

Expand Down
6 changes: 4 additions & 2 deletions modules/Sentry/private/StackTraceProcessor.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -291,15 +291,17 @@ class StackTraceProcessor : SentryEventProcessor
{
$sentryFrame.ContextLine = $lines[$sentryFrame.LineNumber - 1]
}
$preContextCount = [math]::Min(5, $sentryFrame.LineNumber - 1)
$postContextCount = [math]::Min(5, $lines.Count - $sentryFrame.LineNumber)
if ($sentryFrame.LineNumber -gt 6)
{
$lines = $lines | Select-Object -Skip ($sentryFrame.LineNumber - 6)
}
# Note: these are read-only in sentry-dotnet so we just update the underlying lists instead of replacing.
$sentryFrame.PreContext.Clear()
$lines | Select-Object -First 5 | ForEach-Object { $sentryFrame.PreContext.Add($_) }
$lines | Select-Object -First $preContextCount | ForEach-Object { $sentryFrame.PreContext.Add($_) }
$sentryFrame.PostContext.Clear()
$lines | Select-Object -Last 5 | ForEach-Object { $sentryFrame.PostContext.Add($_) }
$lines | Select-Object -Last $postContextCount | ForEach-Object { $sentryFrame.PostContext.Add($_) }
}
catch
{
Expand Down
39 changes: 39 additions & 0 deletions tests/stacktrace.tests.ps1
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
BeforeAll {
. "$PSScriptRoot/utils.ps1"
. "$PSScriptRoot/throwing.ps1"
. "$PSScriptRoot/throwingshort.ps1"
$events = [System.Collections.Generic.List[Sentry.SentryEvent]]::new();
$transport = [RecordingTransport]::new()
StartSentryForEventTests ([ref] $events) ([ref] $transport)
Expand Down Expand Up @@ -77,6 +78,31 @@
# A module-based frame should be in-app=false
$frames | Where-Object -Property Module | Select-Object -First 1 -ExpandProperty 'InApp' | Should -Be $false
}

$checkShortErrorRecord = {

Check warning

Code scanning / PSScriptAnalyzer

The variable 'checkShortErrorRecord' is assigned but never used. Warning test

The variable 'checkShortErrorRecord' is assigned but never used.
$events.Count | Should -Be 1
[Sentry.SentryEvent]$event = $events.ToArray()[0]
$event.SentryExceptions.Count | Should -Be 2

$event.SentryExceptions[1].Type | Should -Be 'Short context test'
$event.SentryExceptions[1].Value | Should -Be 'Short context test'
$event.SentryExceptions[1].Module | Should -BeNullOrEmpty
[Sentry.SentryStackFrame[]] $frames = $event.SentryExceptions[1].Stacktrace.Frames
$frames.Count | Should -BeGreaterThan 1

$frame = GetListItem $frames -1

$frame.Function | Should -Be "funcC"
$frame.AbsolutePath | Should -Be (Join-Path $PSScriptRoot 'throwingshort.ps1')
$frame.LineNumber | Should -BeGreaterThan 0
$frame.InApp | Should -Be $true

$frame.PreContext | Should -Be @('function funcC {')
$frame.PreContext.Count | Should -Be 1
$frame.ContextLine | Should -Be ' throw "Short context test"'
$frame.PostContext | Should -Be @('}')
$frame.PostContext.Count | Should -Be 1
}
}

AfterAll {
Expand Down Expand Up @@ -119,6 +145,19 @@
@($null) | ForEach-Object $checkErrorRecord
}

It 'captures short context' {
try
{
funcC
}
catch
{
$_ | Out-Sentry
}

@($null) | ForEach-Object $checkShortErrorRecord
}

It 'captures exception' {
try
{
Expand Down
3 changes: 3 additions & 0 deletions tests/throwingshort.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function funcC {
throw "Short context test"
}
Loading