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: Support uint64 in crash reports #2631

Merged
merged 3 commits into from
Jan 20, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
- AttachScreenshots is GA (#2623)
- Gather profiling timeseries metrics for CPU usage and memory footprint, and thermal and memory pressure events (#2493)

### Fixes

- Support uint64 in crash reports (#2631)

## 8.0.0

### Features
Expand Down
24 changes: 14 additions & 10 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 <limits.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
Expand Down Expand Up @@ -1190,17 +1191,17 @@ decodeElement(const char *const name, SentryCrashJSONDecodeContext *context)
case '8':
case '9': {
// Try integer conversion.
int64_t accum = 0;
uint64_t accum = 0;
bool isOverflow = false;
const char *const start = context->bufferPtr;

for (; context->bufferPtr < context->bufferEnd && isdigit(*context->bufferPtr);
context->bufferPtr++) {
accum = accum * 10 + (*context->bufferPtr - '0');
unlikely_if(accum < 0)
{
// Overflow
break;
}
unlikely_if((isOverflow = accum > (ULLONG_MAX / 10))) { break; }
accum *= 10;
int nextDigit = (*context->bufferPtr - '0');
unlikely_if((isOverflow = accum > (ULLONG_MAX - nextDigit))) { break; }
philipphofmann marked this conversation as resolved.
Show resolved Hide resolved
accum += nextDigit;
}

unlikely_if(context->bufferPtr >= context->bufferEnd)
Expand All @@ -1209,9 +1210,12 @@ decodeElement(const char *const name, SentryCrashJSONDecodeContext *context)
return SentryCrashJSON_ERROR_INCOMPLETE;
}

if (!isFPChar(*context->bufferPtr) && accum >= 0) {
accum *= sign;
return context->callbacks->onIntegerElement(name, accum, context->userData);
if (!isFPChar(*context->bufferPtr) && !isOverflow) {
if (sign > 0 || accum <= ((uint64_t)LLONG_MAX + 1)) {
int64_t signedAccum = accum;
philipphofmann marked this conversation as resolved.
Show resolved Hide resolved
signedAccum *= sign;
return context->callbacks->onIntegerElement(name, signedAccum, context->userData);
}
}

while (context->bufferPtr < context->bufferEnd && isFPChar(*context->bufferPtr)) {
Expand Down
93 changes: 68 additions & 25 deletions Tests/SentryTests/SentryCrash/SentryCrashJSONCodec_Tests.m
Original file line number Diff line number Diff line change
Expand Up @@ -126,55 +126,56 @@ - (void)testSerializeDeserializeArrayInteger
{
NSError *error = (NSError *)self;
NSString *expected = @"[1]";
id original = [NSArray arrayWithObjects:[NSNumber numberWithInt:1], nil];
int value = 1;
NSArray<NSNumber *> *original = [NSArray arrayWithObjects:[NSNumber numberWithInt:value], nil];
NSString *jsonString = toString([SentryCrashJSONCodec encode:original
options:SentryCrashJSONEncodeOptionSorted
error:&error]);
XCTAssertNotNil(jsonString, @"");
XCTAssertNil(error, @"");
XCTAssertEqualObjects(jsonString, expected, @"");
XCTAssertNotNil(jsonString);
XCTAssertNil(error);
XCTAssertEqualObjects(jsonString, expected);
id result = [SentryCrashJSONCodec decode:toData(jsonString) options:0 error:&error];
XCTAssertNotNil(result, @"");
XCTAssertNil(error, @"");
XCTAssertEqualObjects(result, original, @"");
XCTAssertNotNil(result);
XCTAssertNil(error);
XCTAssertEqualObjects(result, original);
}

- (void)testSerializeDeserializeArrayFloat
{
NSError *error = (NSError *)self;
philipphofmann marked this conversation as resolved.
Show resolved Hide resolved
float value = -0.2f;
NSString *expected = @"[-0.2]";
id original = [NSArray arrayWithObjects:[NSNumber numberWithFloat:-0.2f], nil];
NSArray<NSNumber *> *original =
[NSArray arrayWithObjects:[NSNumber numberWithFloat:value], nil];
NSString *jsonString = toString([SentryCrashJSONCodec encode:original
options:SentryCrashJSONEncodeOptionSorted
error:&error]);
XCTAssertNotNil(jsonString, @"");
XCTAssertNil(error, @"");
XCTAssertEqualObjects(jsonString, expected, @"");
XCTAssertNotNil(jsonString);
XCTAssertNil(error);
XCTAssertEqualObjects(jsonString, expected);
id result = [SentryCrashJSONCodec decode:toData(jsonString) options:0 error:&error];
XCTAssertNotNil(result, @"");
XCTAssertNil(error, @"");
XCTAssertEqual([[result objectAtIndex:0] floatValue], -0.2f, @"");
// This always fails on NSNumber filled with float.
// XCTAssertEqualObjects(result, original, @"");
XCTAssertNotNil(result);
XCTAssertNil(error);
XCTAssertEqual([[result objectAtIndex:0] floatValue], value);
}

- (void)testSerializeDeserializeArrayFloat2
{
NSError *error = (NSError *)self;
NSString *expected = @"[-2e-15]";
id original = [NSArray arrayWithObjects:[NSNumber numberWithFloat:-2e-15f], nil];
float value = -2e-15f;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a special value in any way? If we're trying to target the smallest representable magnitude, we could try using something like -2 * powf(10, FLT_MIN_10_EXP) or FLT16_MIN_10_EXP.

Copy link
Member Author

@philipphofmann philipphofmann Jan 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, but it was already like that. I don't want to change this in this PR, I only simplified the code a bit.

NSArray<NSNumber *> *original =
[NSArray arrayWithObjects:[NSNumber numberWithFloat:value], nil];
NSString *jsonString = toString([SentryCrashJSONCodec encode:original
options:SentryCrashJSONEncodeOptionSorted
error:&error]);
XCTAssertNotNil(jsonString, @"");
XCTAssertNil(error, @"");
XCTAssertEqualObjects(jsonString, expected, @"");
XCTAssertNotNil(jsonString);
XCTAssertNil(error);
XCTAssertEqualObjects(jsonString, expected);
id result = [SentryCrashJSONCodec decode:toData(jsonString) options:0 error:&error];
XCTAssertNotNil(result, @"");
XCTAssertNil(error, @"");
XCTAssertEqual([[result objectAtIndex:0] floatValue], -2e-15f, @"");
// This always fails on NSNumber filled with float.
// XCTAssertEqualObjects(result, original, @"");
XCTAssertNotNil(result);
XCTAssertNil(error);
XCTAssertEqual([[result objectAtIndex:0] floatValue], value);
}

- (void)testSerializeDeserializeArrayString
Expand Down Expand Up @@ -1275,6 +1276,48 @@ - (void)testDeserializeArrayNumberOverflow
XCTAssertNil(error, @"");
}

- (void)testDeserializeArrayInt64Min
{
NSError *error = (NSError *)self;
int64_t value = LLONG_MIN;
NSString *jsonString = [NSString stringWithFormat:@"[%lld]", value];
NSArray<NSNumber *> *result = [SentryCrashJSONCodec decode:toData(jsonString)
options:0
error:&error];
XCTAssertNotNil(result);
XCTAssertNil(error);

XCTAssertEqual([result[0] longLongValue], value);
}

- (void)testDeserializeArray64IntMax
{
NSError *error = (NSError *)self;
int64_t value = LLONG_MAX;
NSString *jsonString = [NSString stringWithFormat:@"[%lld]", value];
NSArray<NSNumber *> *result = [SentryCrashJSONCodec decode:toData(jsonString)
options:0
error:&error];
XCTAssertNotNil(result);
XCTAssertNil(error);

XCTAssertEqual([result[0] longLongValue], value);
}

- (void)testDeserializeArrayUIntMax
{
NSError *error = (NSError *)self;
uint64_t value = ULLONG_MAX;
NSString *jsonString = [NSString stringWithFormat:@"[%llu]", value];
NSArray<NSNumber *> *result = [SentryCrashJSONCodec decode:toData(jsonString)
options:0
error:&error];
XCTAssertNotNil(result);
XCTAssertNil(error);

XCTAssertEqual([result[0] unsignedLongLongValue], value);
}

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