Skip to content

Commit

Permalink
fix: SentryCrash writing nan for invalid number (#2348)
Browse files Browse the repository at this point in the history
Instead of writing nan (which is invalid for json format) in the crash report for invalid numbers, start writing null.
  • Loading branch information
brustolin authored Nov 3, 2022
1 parent 59afa00 commit 7ade7ad
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
- Stop profiler when app moves to background (#2331)
- Clean up old envelopes (#2322)
- Crash when starting a profile from a non-main thread (#2345)
- SentryCrash writing nan for invalid number (#2348)

## 7.29.0

Expand Down
5 changes: 5 additions & 0 deletions Sources/SentryCrash/Recording/Tools/SentryCrashJSONCodec.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
Expand Down Expand Up @@ -311,6 +312,10 @@ int
sentrycrashjson_addFloatingPointElement(
SentryCrashJSONEncodeContext *const context, const char *const name, double value)
{
if (isnan(value)) {
return sentrycrashjson_addNullElement(context, name);
}

int result = sentrycrashjson_beginElement(context, name);
unlikely_if(result != SentryCrashJSON_OK) { return result; }
char buff[50];
Expand Down
38 changes: 38 additions & 0 deletions Tests/SentryTests/SentryCrash/SentryCrashJSONCodec_Tests.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
//

#import <XCTest/XCTest.h>
#import <math.h>

#import "FileBasedTestCase.h"
#import "SentryCrashJSONCodec.h"
Expand Down Expand Up @@ -798,6 +799,24 @@ - (void)testSerializeDeserializeFloat
[[result objectAtIndex:0] floatValue] == [[original objectAtIndex:0] floatValue], @"");
}

- (void)testSerializeDeserializeNanFloat
{
NSError *error = (NSError *)self;
NSString *expected = @"[null]";
float nanValue = nanf("");
id original = [NSArray arrayWithObjects:[NSNumber numberWithFloat:nanValue], nil];
NSString *jsonString = toString([SentryCrashJSONCodec encode:original
options:SentryCrashJSONEncodeOptionSorted
error:&error]);
XCTAssertNotNil(jsonString, @"");
XCTAssertNil(error, @"");
XCTAssertEqualObjects(jsonString, expected, @"");
id result = [SentryCrashJSONCodec decode:toData(jsonString) options:0 error:&error];
XCTAssertNotNil(result, @"");
XCTAssertNil(error, @"");
XCTAssertTrue([[result objectAtIndex:0] isKindOfClass:[NSNull class]]);
}

- (void)testSerializeDeserializeDouble
{
NSError *error = (NSError *)self;
Expand All @@ -816,6 +835,25 @@ - (void)testSerializeDeserializeDouble
[[result objectAtIndex:0] floatValue] == [[original objectAtIndex:0] floatValue], @"");
}

- (void)testSerializeDeserializeNANDouble
{
NSError *error = (NSError *)self;
NSString *expected = @"[null]";
double nanValue = nan("");
id original = [NSArray arrayWithObjects:[NSNumber numberWithDouble:nanValue], nil];

NSString *jsonString = toString([SentryCrashJSONCodec encode:original
options:SentryCrashJSONEncodeOptionSorted
error:&error]);
XCTAssertNotNil(jsonString, @"");
XCTAssertNil(error, @"");
XCTAssertEqualObjects(jsonString, expected, @"");
id result = [SentryCrashJSONCodec decode:toData(jsonString) options:0 error:&error];
XCTAssertNotNil(result, @"");
XCTAssertNil(error, @"");
XCTAssertTrue([[result objectAtIndex:0] isKindOfClass:[NSNull class]]);
}

- (void)testSerializeDeserializeChar
{
NSError *error = (NSError *)self;
Expand Down

0 comments on commit 7ade7ad

Please sign in to comment.