Skip to content
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

fix: SentryCrash writing nan for invalid number #2348

Merged
merged 4 commits into from
Nov 3, 2022
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 @@ -12,6 +12,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