Skip to content

Commit

Permalink
Bug 1251364 - Use more snprintf_literal in the JS engine. r=terrence
Browse files Browse the repository at this point in the history
  • Loading branch information
evilpie committed Aug 3, 2016
1 parent 2fd985f commit 4b86468
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 147 deletions.
11 changes: 6 additions & 5 deletions js/src/builtin/TestingFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "mozilla/FloatingPoint.h"
#include "mozilla/Move.h"
#include "mozilla/Snprintf.h"
#include "mozilla/unused.h"

#include <cmath>
Expand Down Expand Up @@ -318,8 +319,8 @@ GC(JSContext* cx, unsigned argc, Value* vp)

char buf[256] = { '\0' };
#ifndef JS_MORE_DETERMINISTIC
snprintf(buf, sizeof(buf), "before %" PRIuSIZE ", after %" PRIuSIZE "\n",
preBytes, cx->runtime()->gc.usage.gcBytes());
snprintf_literal(buf, "before %" PRIuSIZE ", after %" PRIuSIZE "\n",
preBytes, cx->runtime()->gc.usage.gcBytes());
#endif
JSString* str = JS_NewStringCopyZ(cx, buf);
if (!str)
Expand Down Expand Up @@ -2401,7 +2402,7 @@ ObjectAddress(JSContext* cx, unsigned argc, Value* vp)
#else
void* ptr = js::UncheckedUnwrap(&args[0].toObject(), true);
char buffer[64];
snprintf(buffer, sizeof(buffer), "%p", ptr);
snprintf_literal(buffer, "%p", ptr);

JSString* str = JS_NewStringCopyZ(cx, buffer);
if (!str)
Expand Down Expand Up @@ -2442,8 +2443,8 @@ SharedAddress(JSContext* cx, unsigned argc, Value* vp)
}
char buffer[64];
uint32_t nchar =
snprintf(buffer, sizeof(buffer), "%p",
obj->as<SharedArrayBufferObject>().dataPointerShared().unwrap(/*safeish*/));
snprintf_literal(buffer, "%p",
obj->as<SharedArrayBufferObject>().dataPointerShared().unwrap(/*safeish*/));

JSString* str = JS_NewStringCopyN(cx, buffer, nchar);
if (!str)
Expand Down
41 changes: 21 additions & 20 deletions js/src/ctypes/CTypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "mozilla/FloatingPoint.h"
#include "mozilla/MemoryReporting.h"
#include "mozilla/SizePrintfMacros.h"
#include "mozilla/Snprintf.h"
#include "mozilla/Vector.h"

#include <limits>
Expand Down Expand Up @@ -95,7 +96,7 @@ GetDeflatedUTF8StringLength(JSContext* maybecx, const CharT* chars,
if (maybecx) {
js::gc::AutoSuppressGC suppress(maybecx);
char buffer[10];
snprintf(buffer, sizeof(buffer), "0x%x", c);
snprintf_literal(buffer, "0x%x", c);
JS_ReportErrorFlagsAndNumber(maybecx, JSREPORT_ERROR, GetErrorMessage,
nullptr, JSMSG_BAD_SURROGATE_CHAR, buffer);
}
Expand Down Expand Up @@ -1121,7 +1122,7 @@ ConvError(JSContext* cx, const char* expectedStr, HandleValue actual,
MOZ_ASSERT(!funObj);

char indexStr[16];
snprintf(indexStr, sizeof(indexStr), "%u", arrIndex);
snprintf_literal(indexStr, "%u", arrIndex);

AutoString arrSource;
JSAutoByteString arrBytes;
Expand Down Expand Up @@ -1178,7 +1179,7 @@ ConvError(JSContext* cx, const char* expectedStr, HandleValue actual,
MOZ_ASSERT(funObj);

char indexStr[16];
snprintf(indexStr, sizeof(indexStr), "%u", argIndex + 1);
snprintf_literal(indexStr, "%u", argIndex + 1);

AutoString funSource;
JSAutoByteString funBytes;
Expand Down Expand Up @@ -1261,7 +1262,7 @@ ArgumentConvError(JSContext* cx, HandleValue actual, const char* funStr,
return false;

char indexStr[16];
snprintf(indexStr, sizeof(indexStr), "%u", argIndex + 1);
snprintf_literal(indexStr, "%u", argIndex + 1);

JS_ReportErrorNumber(cx, GetErrorMessage, nullptr,
CTYPESMSG_CONV_ERROR_ARG, valStr, indexStr, funStr);
Expand Down Expand Up @@ -1290,9 +1291,9 @@ ArrayLengthMismatch(JSContext* cx, unsigned expectedLength, HandleObject arrObj,
return false;

char expectedLengthStr[16];
snprintf(expectedLengthStr, sizeof(expectedLengthStr), "%u", expectedLength);
snprintf_literal(expectedLengthStr, "%u", expectedLength);
char actualLengthStr[16];
snprintf(actualLengthStr, sizeof(actualLengthStr), "%u", actualLength);
snprintf_literal(actualLengthStr, "%u", actualLength);

AutoString arrSource;
JSAutoByteString arrBytes;
Expand Down Expand Up @@ -1320,9 +1321,9 @@ ArrayLengthOverflow(JSContext* cx, unsigned expectedLength, HandleObject arrObj,
return false;

char expectedLengthStr[16];
snprintf(expectedLengthStr, sizeof(expectedLengthStr), "%u", expectedLength);
snprintf_literal(expectedLengthStr, "%u", expectedLength);
char actualLengthStr[16];
snprintf(actualLengthStr, sizeof(actualLengthStr), "%u", actualLength);
snprintf_literal(actualLengthStr, "%u", actualLength);

AutoString arrSource;
JSAutoByteString arrBytes;
Expand Down Expand Up @@ -1427,9 +1428,9 @@ FieldCountMismatch(JSContext* cx,
return false;

char expectedCountStr[16];
snprintf(expectedCountStr, sizeof(expectedCountStr), "%u", expectedCount);
snprintf_literal(expectedCountStr, "%u", expectedCount);
char actualCountStr[16];
snprintf(actualCountStr, sizeof(actualCountStr), "%u", actualCount);
snprintf_literal(actualCountStr, "%u", actualCount);

JSAutoByteString posBytes;
const char* posStr;
Expand Down Expand Up @@ -1460,7 +1461,7 @@ FieldDescriptorCountError(JSContext* cx, Value val, size_t length)
return false;

char lengthStr[16];
snprintf(lengthStr, sizeof(lengthStr), "%" PRIuSIZE, length);
snprintf_literal(lengthStr, "%" PRIuSIZE, length);

JS_ReportErrorNumber(cx, GetErrorMessage, nullptr,
CTYPESMSG_FIELD_DESC_COUNT, valStr, lengthStr);
Expand Down Expand Up @@ -1595,9 +1596,9 @@ FunctionArgumentLengthMismatch(JSContext* cx,
return false;

char expectedCountStr[16];
snprintf(expectedCountStr, sizeof(expectedCountStr), "%u", expectedCount);
snprintf_literal(expectedCountStr, "%u", expectedCount);
char actualCountStr[16];
snprintf(actualCountStr, sizeof(actualCountStr), "%u", actualCount);
snprintf_literal(actualCountStr, "%u", actualCount);

const char* variadicStr = isVariadic ? " or more": "";

Expand All @@ -1618,7 +1619,7 @@ FunctionArgumentTypeError(JSContext* cx,
return false;

char indexStr[16];
snprintf(indexStr, sizeof(indexStr), "%u", index + 1);
snprintf_literal(indexStr, "%u", index + 1);

JS_ReportErrorNumber(cx, GetErrorMessage, nullptr,
CTYPESMSG_ARG_TYPE_ERROR, indexStr, reason, valStr);
Expand Down Expand Up @@ -1724,10 +1725,10 @@ static bool
InvalidIndexRangeError(JSContext* cx, size_t index, size_t length)
{
char indexStr[16];
snprintf(indexStr, sizeof(indexStr), "%" PRIuSIZE, index);
snprintf_literal(indexStr, "%" PRIuSIZE, index);

char lengthStr[16];
snprintf(lengthStr, sizeof(lengthStr), "%" PRIuSIZE, length);
snprintf_literal(lengthStr,"%" PRIuSIZE, length);

JS_ReportErrorNumber(cx, GetErrorMessage, nullptr,
CTYPESMSG_INVALID_RANGE, indexStr, lengthStr);
Expand Down Expand Up @@ -1884,8 +1885,8 @@ SizeMismatchCastError(JSContext* cx,

char sourceSizeStr[16];
char targetSizeStr[16];
snprintf(sourceSizeStr, sizeof(sourceSizeStr), "%" PRIuSIZE, sourceSize);
snprintf(targetSizeStr, sizeof(targetSizeStr), "%" PRIuSIZE, targetSize);
snprintf_literal(sourceSizeStr, "%" PRIuSIZE, sourceSize);
snprintf_literal(targetSizeStr, "%" PRIuSIZE, targetSize);

JS_ReportErrorNumber(cx, GetErrorMessage, nullptr,
CTYPESMSG_SIZE_MISMATCH_CAST,
Expand Down Expand Up @@ -1917,7 +1918,7 @@ VariadicArgumentTypeError(JSContext* cx, uint32_t index, HandleValue actual)
return false;

char indexStr[16];
snprintf(indexStr, sizeof(indexStr), "%u", index + 1);
snprintf_literal(indexStr, "%u", index + 1);

JS_ReportErrorNumber(cx, GetErrorMessage, nullptr,
CTYPESMSG_VARG_TYPE_ERROR, indexStr, valStr);
Expand Down Expand Up @@ -6306,7 +6307,7 @@ StructType::ConstructData(JSContext* cx,
size_t count = fields->count();
if (count >= 2) {
char fieldLengthStr[32];
snprintf(fieldLengthStr, sizeof(fieldLengthStr), "0, 1, or %" PRIuSIZE, count);
snprintf_literal(fieldLengthStr, "0, 1, or %" PRIuSIZE, count);
return ArgumentLengthError(cx, "StructType constructor", fieldLengthStr,
"s");
}
Expand Down
Loading

0 comments on commit 4b86468

Please sign in to comment.