Skip to content

Commit

Permalink
fix: Crash deserializing empty envelope length>0 (#4281)
Browse files Browse the repository at this point in the history
* fixes crash deserializing empty envelope length>0

* changelog
  • Loading branch information
kahest authored Aug 14, 2024
1 parent 02671d8 commit 464117d
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
### Fixes

- Session replay not redacting buttons and other non UILabel texts (#4277)
- Crash deserializing empty envelope length>0 (#4281]


## 8.33.0

Expand Down
6 changes: 3 additions & 3 deletions Sources/Sentry/SentrySerialization.m
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,6 @@ + (SentryEnvelope *_Nullable)envelopeWithData:(NSData *)data
NSUInteger endOfEnvelope = data.length - 1;
for (NSInteger i = itemHeaderStart; i <= endOfEnvelope; ++i) {
if (bytes[i] == '\n' || i == endOfEnvelope) {
if (endOfEnvelope == i) {
i++; // 0 byte attachment
}

NSData *itemHeaderData =
[data subdataWithRange:NSMakeRange(itemHeaderStart, i - itemHeaderStart)];
Expand Down Expand Up @@ -222,6 +219,9 @@ + (SentryEnvelope *_Nullable)envelopeWithData:(NSData *)data
itemHeader = [[SentryEnvelopeItemHeader alloc] initWithType:type length:bodyLength];
}

if (endOfEnvelope == i) {
i++; // 0 byte attachment
}
NSData *itemBody = [data subdataWithRange:NSMakeRange(i + 1, bodyLength)];
#ifdef DEBUG
if ([SentryEnvelopeItemTypeEvent isEqual:type] ||
Expand Down
5 changes: 5 additions & 0 deletions Tests/SentryTests/PrivateSentrySDKOnlyTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ class PrivateSentrySDKOnlyTests: XCTestCase {
let itemData = "{}\n{\"length\":0,\"type\":\"attachment\"}\n".data(using: .utf8)!
XCTAssertNotNil(PrivateSentrySDKOnly.envelope(with: itemData))
}

func testEnvelopeWithDataLengthGtZero() throws {
let itemData = "{}\n{\"length\":1,\"type\":\"attachment\"}\n".data(using: .utf8)!
XCTAssertNil(PrivateSentrySDKOnly.envelope(with: itemData))
}

func testGetDebugImages() {
let images = PrivateSentrySDKOnly.getDebugImages()
Expand Down

0 comments on commit 464117d

Please sign in to comment.