diff --git a/CHANGELOG.md b/CHANGELOG.md index 7d004a5c3a5..3647ca238d3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/Sources/SentryCrash/Recording/Tools/SentryCrashJSONCodec.c b/Sources/SentryCrash/Recording/Tools/SentryCrashJSONCodec.c index ab5e488cc78..d2f231cefd5 100644 --- a/Sources/SentryCrash/Recording/Tools/SentryCrashJSONCodec.c +++ b/Sources/SentryCrash/Recording/Tools/SentryCrashJSONCodec.c @@ -30,6 +30,7 @@ #include #include #include +#include #include #include #include @@ -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]; diff --git a/Tests/SentryTests/SentryCrash/SentryCrashJSONCodec_Tests.m b/Tests/SentryTests/SentryCrash/SentryCrashJSONCodec_Tests.m index aebf3439eae..838f4001d9e 100644 --- a/Tests/SentryTests/SentryCrash/SentryCrashJSONCodec_Tests.m +++ b/Tests/SentryTests/SentryCrash/SentryCrashJSONCodec_Tests.m @@ -25,6 +25,7 @@ // #import +#import #import "FileBasedTestCase.h" #import "SentryCrashJSONCodec.h" @@ -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; @@ -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;