Skip to content

Commit

Permalink
Bug 1799630 - replace sprintf by snprintf or SprintfLiteral r=emilio
Browse files Browse the repository at this point in the history
  • Loading branch information
longsonr committed Nov 9, 2022
1 parent 4d82d9b commit 143ac3f
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 19 deletions.
2 changes: 1 addition & 1 deletion dom/gamepad/cocoa/CocoaGamepad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ void DarwinGamepadService::DeviceAdded(IOHIDDeviceRef device) {
CFStringGetCString(productRef, product_name, sizeof(product_name),
kCFStringEncodingASCII);
char buffer[256];
sprintf(buffer, "%x-%x-%s", vendorId, productId, product_name);
SprintfLiteral(buffer, "%x-%x-%s", vendorId, productId, product_name);

bool defaultRemapper = false;
RefPtr<GamepadRemapper> remapper =
Expand Down
5 changes: 3 additions & 2 deletions dom/security/test/gtest/TestSmartCrashTrimmer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "nsContentSecurityUtils.h"
#include "nsTString.h"
#include "nsStringFwd.h"
#include "mozilla/Sprintf.h"

#define ASSERT_STRCMP(first, second) ASSERT_TRUE(strcmp(first, second) == 0);

Expand All @@ -36,8 +37,8 @@ TEST(SmartCrashTrimmer, Test)
std::string(1025, '.').c_str(), std::string(1025, 'A').c_str(),
"Hello %s world %s!");
char expected[1025];
sprintf(expected, "Hello %s world AAAAAAAAAAAAAAAAAAAAAAAAA!",
std::string(984, '.').c_str());
SprintfLiteral(expected, "Hello %s world AAAAAAAAAAAAAAAAAAAAAAAAA!",
std::string(984, '.').c_str());
ASSERT_STRCMP(ret.get(), expected);
}
}
5 changes: 3 additions & 2 deletions memory/replace/dmd/DMD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,9 @@ void DMDFuncs::StatusMsg(const char* aFmt, va_list aAp) {
__android_log_vprint(ANDROID_LOG_INFO, "DMD", aFmt, aAp);
#else
// The +64 is easily enough for the "DMD[<pid>] " prefix and the NUL.
char* fmt = (char*)InfallibleAllocPolicy::malloc_(strlen(aFmt) + 64);
sprintf(fmt, "DMD[%d] %s", getpid(), aFmt);
size_t size = strlen(aFmt) + 64;
char* fmt = (char*)InfallibleAllocPolicy::malloc_(size);
snprintf(fmt, size, "DMD[%d] %s", getpid(), aFmt);
vfprintf(stderr, fmt, aAp);
InfallibleAllocPolicy::free_(fmt);
#endif
Expand Down
15 changes: 8 additions & 7 deletions memory/replace/dmd/test/SmokeDMD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class FpWriteFunc final : public mozilla::JSONWriteFunc {
// This stops otherwise-unused variables from being optimized away.
static void UseItOrLoseIt(void* aPtr, int aSeven) {
char buf[64];
int n = sprintf(buf, "%p\n", aPtr);
int n = SprintfLiteral(buf, "%p\n", aPtr);
if (n == 20 + aSeven) {
fprintf(stderr, "well, that is surprising");
}
Expand Down Expand Up @@ -95,11 +95,11 @@ void Foo(int aSeven) {

void TestEmpty(const char* aTestName, const char* aMode) {
char filename[128];
sprintf(filename, "complete-%s-%s.json", aTestName, aMode);
SprintfLiteral(filename, "complete-%s-%s.json", aTestName, aMode);
auto f = MakeUnique<FpWriteFunc>(filename);

char options[128];
sprintf(options, "--mode=%s --stacks=full", aMode);
SprintfLiteral(options, "--mode=%s --stacks=full", aMode);
ResetEverything(options);

// Zero for everything.
Expand All @@ -108,13 +108,14 @@ void TestEmpty(const char* aTestName, const char* aMode) {

void TestFull(const char* aTestName, int aNum, const char* aMode, int aSeven) {
char filename[128];
sprintf(filename, "complete-%s%d-%s.json", aTestName, aNum, aMode);
SprintfLiteral(filename, "complete-%s%d-%s.json", aTestName, aNum, aMode);
auto f = MakeUnique<FpWriteFunc>(filename);

// The --show-dump-stats=yes is there just to give that option some basic
// testing, e.g. ensure it doesn't crash. It's hard to test much beyond that.
char options[128];
sprintf(options, "--mode=%s --stacks=full --show-dump-stats=yes", aMode);
SprintfLiteral(options, "--mode=%s --stacks=full --show-dump-stats=yes",
aMode);
ResetEverything(options);

// Analyze 1: 1 freed, 9 out of 10 unreported.
Expand Down Expand Up @@ -277,11 +278,11 @@ void TestFull(const char* aTestName, int aNum, const char* aMode, int aSeven) {

void TestPartial(const char* aTestName, const char* aMode, int aSeven) {
char filename[128];
sprintf(filename, "complete-%s-%s.json", aTestName, aMode);
SprintfLiteral(filename, "complete-%s-%s.json", aTestName, aMode);
auto f = MakeUnique<FpWriteFunc>(filename);

char options[128];
sprintf(options, "--mode=%s", aMode);
SprintfLiteral(options, "--mode=%s", aMode);
ResetEverything(options);

int kTenThousand = aSeven + 9993;
Expand Down
6 changes: 3 additions & 3 deletions mozglue/tests/glibc_printf_tests/tfformat.c
Original file line number Diff line number Diff line change
Expand Up @@ -4113,7 +4113,7 @@ int main(int argc, char *argv[])
sprint_double_type *dptr;
for (dptr = sprint_doubles; dptr->line; dptr++)
{
sprintf (buffer, dptr->format_string, dptr->value);
snprintf (buffer, sizeof(buffer), dptr->format_string, dptr->value);
if (!matches(buffer, dptr->result))
{
errcount++;
Expand All @@ -4122,7 +4122,7 @@ int main(int argc, char *argv[])
dptr->line, dptr->format_string, buffer, dptr->result);
}

sprintf (buffer, "%.999g", dptr->value);
snprintf (buffer, sizeof(buffer), "%.999g", dptr->value);
sscanf (buffer, "%lg", &d);
if (dptr->value != d && !isnan(d))
{
Expand All @@ -4140,7 +4140,7 @@ int main(int argc, char *argv[])
d = 1.0;
for (i = 1; i < 50; ++i)
d /= 2;
sprintf (buffer, "%.100g", d);
snprintf (buffer, sizeof(buffer), "%.100g", d);
if (!matches (buffer, ref))
{
++errcount;
Expand Down
2 changes: 1 addition & 1 deletion mozglue/tests/glibc_printf_tests/tiformat.c
Original file line number Diff line number Diff line change
Expand Up @@ -5048,7 +5048,7 @@ main(int argc, char *argv[])
char buffer[BSIZE];
sprint_int_type *iptr;
for (iptr = sprint_ints; iptr->line; iptr++) {
sprintf(buffer, iptr->format_string, iptr->value);
snprintf(buffer, sizeof(buffer), iptr->format_string, iptr->value);
if (strcmp(buffer, iptr->result) != 0) {
errcount++;
printf(
Expand Down
2 changes: 1 addition & 1 deletion mozglue/tests/glibc_printf_tests/tllformat.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ main (void)
sprint_int_type *iptr;
for (iptr = sprint_ints; iptr->line; iptr++)
{
sprintf (buffer, iptr->format_string, iptr->value);
snprintf (buffer, sizeof(buffer), iptr->format_string, iptr->value);
if (strcmp (buffer, iptr->result) != 0)
{
++errcount;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#include "sslimpl.h"
#include "TLSServer.h"

#include "mozilla/Sprintf.h"

using namespace mozilla;
using namespace mozilla::test;

Expand Down Expand Up @@ -96,7 +98,7 @@ int DoCallback(const char* path) {
}

char request[512];
sprintf(request, "GET %s HTTP/1.0\r\n\r\n", path);
SprintfLiteral(request, "GET %s HTTP/1.0\r\n\r\n", path);
SendAll(socket.get(), request, strlen(request));
char buf[4096];
memset(buf, 0, sizeof(buf));
Expand Down Expand Up @@ -139,7 +141,7 @@ void SecretCallbackFailZeroRtt(PRFileDesc* fd, PRUint16 epoch,
}

char path[256];
sprintf(path, "/callback/%d", epoch);
SprintfLiteral(path, "/callback/%d", epoch);
DoCallback(path);

fprintf(stderr, "0RTT handler, configuring alert\n");
Expand Down

0 comments on commit 143ac3f

Please sign in to comment.