diff --git a/CHANGELOG.md b/CHANGELOG.md index e20f4c7be27..565ad06aa9e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## Unreleased + +### Fixes + +- Convert one of the two remaining usages of `sprintf` to `snprintf` (#2866) + ## 8.7.2 ### Fixed diff --git a/Sources/SentryCrash/Recording/Tools/SentryCrashJSONCodec.c b/Sources/SentryCrash/Recording/Tools/SentryCrashJSONCodec.c index 53d4c38be5e..8f9f8197b7d 100644 --- a/Sources/SentryCrash/Recording/Tools/SentryCrashJSONCodec.c +++ b/Sources/SentryCrash/Recording/Tools/SentryCrashJSONCodec.c @@ -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)); } diff --git a/Tests/SentryTests/SentryCrash/SentryCrashJSONCodec_Tests.m b/Tests/SentryTests/SentryCrash/SentryCrashJSONCodec_Tests.m index 0bc90ad5b1b..f89453e836c 100644 --- a/Tests/SentryTests/SentryCrash/SentryCrashJSONCodec_Tests.m +++ b/Tests/SentryTests/SentryCrash/SentryCrashJSONCodec_Tests.m @@ -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