Skip to content

Commit

Permalink
ref: convert an sprintf call to snprintf (#2866)
Browse files Browse the repository at this point in the history
  • Loading branch information
armcknight authored May 18, 2023
1 parent fcde045 commit 736495a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

### Fixes

- Convert one of the two remaining usages of `sprintf` to `snprintf` (#2866)

## 8.7.2

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion Sources/SentryCrash/Recording/Tools/SentryCrashJSONCodec.c
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ sentrycrashjson_addUIntegerElement(
int result = sentrycrashjson_beginElement(context, name);
unlikely_if(result != SentryCrashJSON_OK) { return result; }
char buff[30];
sprintf(buff, "%" PRIu64, value);
snprintf(buff, sizeof(buff), "%" PRIu64, value);
return addJSONData(context, buff, (int)strlen(buff));
}

Expand Down
16 changes: 16 additions & 0 deletions Tests/SentryTests/SentryCrash/SentryCrashJSONCodec_Tests.m
Original file line number Diff line number Diff line change
Expand Up @@ -1672,4 +1672,20 @@ - (void)testDontCloseLastContainer
return result;
}

- (void)testFastUIntEncode
{
char *expectedJson = "{\"uint\":1234567890}";

NSMutableData *encodedData = [NSMutableData data];
SentryCrashJSONEncodeContext context = { 0 };
sentrycrashjson_beginEncode(&context, false, addJSONData, (__bridge void *)(encodedData));
sentrycrashjson_beginObject(&context, NULL);
sentrycrashjson_addUIntegerElement(&context, "uint", 1234567890);
sentrycrashjson_endContainer(&context);
sentrycrashjson_endEncode(&context);
[encodedData appendBytes:"\0" length:1];

[self expectEquivalentJSON:encodedData.bytes toJSON:expectedJson];
}

@end

0 comments on commit 736495a

Please sign in to comment.