From 4b86468edfeaadb72852f28b5d1c80a2ea64497d Mon Sep 17 00:00:00 2001 From: Tom Schuster Date: Wed, 3 Aug 2016 14:37:31 +0200 Subject: [PATCH] Bug 1251364 - Use more snprintf_literal in the JS engine. r=terrence --- js/src/builtin/TestingFunctions.cpp | 11 +- js/src/ctypes/CTypes.cpp | 41 +++---- js/src/gc/Statistics.cpp | 171 ++++++++++++++-------------- js/src/jsdate.cpp | 56 +++++---- js/src/jsopcode.cpp | 7 +- js/src/shell/js.cpp | 5 +- 6 files changed, 144 insertions(+), 147 deletions(-) diff --git a/js/src/builtin/TestingFunctions.cpp b/js/src/builtin/TestingFunctions.cpp index c3b68f77c5672..e902a29bbb78e 100644 --- a/js/src/builtin/TestingFunctions.cpp +++ b/js/src/builtin/TestingFunctions.cpp @@ -8,6 +8,7 @@ #include "mozilla/FloatingPoint.h" #include "mozilla/Move.h" +#include "mozilla/Snprintf.h" #include "mozilla/unused.h" #include @@ -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) @@ -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) @@ -2442,8 +2443,8 @@ SharedAddress(JSContext* cx, unsigned argc, Value* vp) } char buffer[64]; uint32_t nchar = - snprintf(buffer, sizeof(buffer), "%p", - obj->as().dataPointerShared().unwrap(/*safeish*/)); + snprintf_literal(buffer, "%p", + obj->as().dataPointerShared().unwrap(/*safeish*/)); JSString* str = JS_NewStringCopyN(cx, buffer, nchar); if (!str) diff --git a/js/src/ctypes/CTypes.cpp b/js/src/ctypes/CTypes.cpp index 0921430b6a5a2..d3ce8c64aa24f 100644 --- a/js/src/ctypes/CTypes.cpp +++ b/js/src/ctypes/CTypes.cpp @@ -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 @@ -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); } @@ -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; @@ -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; @@ -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); @@ -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; @@ -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; @@ -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; @@ -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); @@ -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": ""; @@ -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); @@ -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); @@ -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, @@ -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); @@ -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"); } diff --git a/js/src/gc/Statistics.cpp b/js/src/gc/Statistics.cpp index 49f3741faf68d..eec024d45e421 100644 --- a/js/src/gc/Statistics.cpp +++ b/js/src/gc/Statistics.cpp @@ -10,6 +10,7 @@ #include "mozilla/DebugOnly.h" #include "mozilla/IntegerRange.h" #include "mozilla/PodOperations.h" +#include "mozilla/Snprintf.h" #include #include @@ -340,12 +341,11 @@ Statistics::formatCompactSliceMessage() const const char* format = "GC Slice %u - Pause: %.3fms of %s budget (@ %.3fms); Reason: %s; Reset: %s%s; Times: "; char buffer[1024]; - memset(buffer, 0, sizeof(buffer)); - snprintf(buffer, sizeof(buffer), format, index, - t(slice.duration()), budgetDescription, t(slice.start - slices[0].start), - ExplainReason(slice.reason), - slice.resetReason ? "yes - " : "no", - slice.resetReason ? slice.resetReason : ""); + snprintf_literal(buffer, format, index, + t(slice.duration()), budgetDescription, t(slice.start - slices[0].start), + ExplainReason(slice.reason), + slice.resetReason ? "yes - " : "no", + slice.resetReason ? slice.resetReason : ""); FragmentVector fragments; if (!fragments.append(DuplicateString(buffer)) || @@ -373,34 +373,34 @@ Statistics::formatCompactSummaryMessage() const char buffer[1024]; if (!nonincrementalReason_) { - snprintf(buffer, sizeof(buffer), - "Max Pause: %.3fms; MMU 20ms: %.1f%%; MMU 50ms: %.1f%%; Total: %.3fms; ", - t(longest), mmu20 * 100., mmu50 * 100., t(total)); + snprintf_literal(buffer, + "Max Pause: %.3fms; MMU 20ms: %.1f%%; MMU 50ms: %.1f%%; Total: %.3fms; ", + t(longest), mmu20 * 100., mmu50 * 100., t(total)); } else { - snprintf(buffer, sizeof(buffer), "Non-Incremental: %.3fms (%s); ", - t(total), nonincrementalReason_); + snprintf_literal(buffer, "Non-Incremental: %.3fms (%s); ", + t(total), nonincrementalReason_); } if (!fragments.append(DuplicateString(buffer))) return UniqueChars(nullptr); - snprintf(buffer, sizeof(buffer), - "Zones: %d of %d (-%d); Compartments: %d of %d (-%d); HeapSize: %.3f MiB; "\ - "HeapChange (abs): %+d (%d); ", - zoneStats.collectedZoneCount, zoneStats.zoneCount, zoneStats.sweptZoneCount, - zoneStats.collectedCompartmentCount, zoneStats.compartmentCount, - zoneStats.sweptCompartmentCount, - double(preBytes) / bytesPerMiB, - counts[STAT_NEW_CHUNK] - counts[STAT_DESTROY_CHUNK], - counts[STAT_NEW_CHUNK] + counts[STAT_DESTROY_CHUNK]); + snprintf_literal(buffer, + "Zones: %d of %d (-%d); Compartments: %d of %d (-%d); HeapSize: %.3f MiB; "\ + "HeapChange (abs): %+d (%d); ", + zoneStats.collectedZoneCount, zoneStats.zoneCount, zoneStats.sweptZoneCount, + zoneStats.collectedCompartmentCount, zoneStats.compartmentCount, + zoneStats.sweptCompartmentCount, + double(preBytes) / bytesPerMiB, + counts[STAT_NEW_CHUNK] - counts[STAT_DESTROY_CHUNK], + counts[STAT_NEW_CHUNK] + counts[STAT_DESTROY_CHUNK]); if (!fragments.append(DuplicateString(buffer))) return UniqueChars(nullptr); MOZ_ASSERT_IF(counts[STAT_ARENA_RELOCATED], gckind == GC_SHRINK); if (gckind == GC_SHRINK) { - snprintf(buffer, sizeof(buffer), - "Kind: %s; Relocated: %.3f MiB; ", - ExplainInvocationKind(gckind), - double(ArenaSize * counts[STAT_ARENA_RELOCATED]) / bytesPerMiB); + snprintf_literal(buffer, + "Kind: %s; Relocated: %.3f MiB; ", + ExplainInvocationKind(gckind), + double(ArenaSize * counts[STAT_ARENA_RELOCATED]) / bytesPerMiB); if (!fragments.append(DuplicateString(buffer))) return UniqueChars(nullptr); } @@ -425,13 +425,13 @@ Statistics::formatCompactSlicePhaseTimes(const PhaseTimeTable phaseTimes) const int64_t ownTime = phaseTimes[dagSlot][phase]; int64_t childTime = SumChildTimes(dagSlot, phase, phaseTimes); if (ownTime > MaxUnaccountedTimeUS) { - snprintf(buffer, sizeof(buffer), "%s: %.3fms", phases[phase].name, t(ownTime)); + snprintf_literal(buffer, "%s: %.3fms", phases[phase].name, t(ownTime)); if (!fragments.append(DuplicateString(buffer))) return UniqueChars(nullptr); if (childTime && (ownTime - childTime) > MaxUnaccountedTimeUS) { MOZ_ASSERT(level < 3); - snprintf(buffer, sizeof(buffer), "%s: %.3fms", "Other", t(ownTime - childTime)); + snprintf_literal(buffer, "%s: %.3fms", "Other", t(ownTime - childTime)); if (!fragments.append(DuplicateString(buffer))) return UniqueChars(nullptr); } @@ -491,23 +491,22 @@ Statistics::formatDetailedDescription() Arenas Relocated: %.3f MiB\n\ "; char buffer[1024]; - memset(buffer, 0, sizeof(buffer)); - snprintf(buffer, sizeof(buffer), format, - ExplainInvocationKind(gckind), - ExplainReason(slices[0].reason), - nonincrementalReason_ ? "no - " : "yes", - nonincrementalReason_ ? nonincrementalReason_ : "", - zoneStats.collectedZoneCount, zoneStats.zoneCount, zoneStats.sweptZoneCount, - zoneStats.collectedCompartmentCount, zoneStats.compartmentCount, - zoneStats.sweptCompartmentCount, - counts[STAT_MINOR_GC], - counts[STAT_STOREBUFFER_OVERFLOW], - mmu20 * 100., mmu50 * 100., - t(sccTotal), t(sccLongest), - double(preBytes) / bytesPerMiB, - counts[STAT_NEW_CHUNK] - counts[STAT_DESTROY_CHUNK], counts[STAT_NEW_CHUNK] + - counts[STAT_DESTROY_CHUNK], - double(ArenaSize * counts[STAT_ARENA_RELOCATED]) / bytesPerMiB); + snprintf_literal(buffer, format, + ExplainInvocationKind(gckind), + ExplainReason(slices[0].reason), + nonincrementalReason_ ? "no - " : "yes", + nonincrementalReason_ ? nonincrementalReason_ : "", + zoneStats.collectedZoneCount, zoneStats.zoneCount, zoneStats.sweptZoneCount, + zoneStats.collectedCompartmentCount, zoneStats.compartmentCount, + zoneStats.sweptCompartmentCount, + counts[STAT_MINOR_GC], + counts[STAT_STOREBUFFER_OVERFLOW], + mmu20 * 100., mmu50 * 100., + t(sccTotal), t(sccLongest), + double(preBytes) / bytesPerMiB, + counts[STAT_NEW_CHUNK] - counts[STAT_DESTROY_CHUNK], counts[STAT_NEW_CHUNK] + + counts[STAT_DESTROY_CHUNK], + double(ArenaSize * counts[STAT_ARENA_RELOCATED]) / bytesPerMiB); return DuplicateString(buffer); } @@ -527,13 +526,12 @@ Statistics::formatDetailedSliceDescription(unsigned i, const SliceData& slice) Pause: %.3fms of %s budget (@ %.3fms)\n\ "; char buffer[1024]; - memset(buffer, 0, sizeof(buffer)); - snprintf(buffer, sizeof(buffer), format, i, - ExplainReason(slice.reason), - slice.resetReason ? "yes - " : "no", slice.resetReason ? slice.resetReason : "", - gc::StateName(slice.initialState), gc::StateName(slice.finalState), - uint64_t(slice.endFaults - slice.startFaults), - t(slice.duration()), budgetDescription, t(slice.start - slices[0].start)); + snprintf_literal(buffer, format, i, ExplainReason(slice.reason), + slice.resetReason ? "yes - " : "no", + slice.resetReason ? slice.resetReason : "", + gc::StateName(slice.initialState), gc::StateName(slice.finalState), + uint64_t(slice.endFaults - slice.startFaults), + t(slice.duration()), budgetDescription, t(slice.start - slices[0].start)); return DuplicateString(buffer); } @@ -555,15 +553,15 @@ Statistics::formatDetailedPhaseTimes(const PhaseTimeTable phaseTimes) int64_t ownTime = phaseTimes[dagSlot][phase]; int64_t childTime = SumChildTimes(dagSlot, phase, phaseTimes); if (ownTime > 0) { - snprintf(buffer, sizeof(buffer), " %s%s: %.3fms\n", - LevelToIndent[level], phases[phase].name, t(ownTime)); + snprintf_literal(buffer, " %s%s: %.3fms\n", + LevelToIndent[level], phases[phase].name, t(ownTime)); if (!fragments.append(DuplicateString(buffer))) return UniqueChars(nullptr); if (childTime && (ownTime - childTime) > MaxUnaccountedChildTimeUS) { MOZ_ASSERT(level < 3); - snprintf(buffer, sizeof(buffer), " %s%s: %.3fms\n", - LevelToIndent[level + 1], "Other", t(ownTime - childTime)); + snprintf_literal(buffer, " %s%s: %.3fms\n", + LevelToIndent[level + 1], "Other", t(ownTime - childTime)); if (!fragments.append(DuplicateString(buffer))) return UniqueChars(nullptr); } @@ -585,8 +583,7 @@ Statistics::formatDetailedTotals() Max Pause: %.3fms\n\ "; char buffer[1024]; - memset(buffer, 0, sizeof(buffer)); - snprintf(buffer, sizeof(buffer), format, t(total), t(longest)); + snprintf_literal(buffer, format, t(total), t(longest)); return DuplicateString(buffer); } @@ -656,24 +653,23 @@ Statistics::formatJsonDescription(uint64_t timestamp) "\"added_chunks\":%d," "\"removed_chunks\":%d,"; char buffer[1024]; - memset(buffer, 0, sizeof(buffer)); - snprintf(buffer, sizeof(buffer), format, - (unsigned long long)timestamp, - longest / 1000, longest % 1000, - total / 1000, total % 1000, - zoneStats.collectedZoneCount, - zoneStats.zoneCount, - zoneStats.compartmentCount, - counts[STAT_MINOR_GC], - counts[STAT_STOREBUFFER_OVERFLOW], - int(mmu20 * 100), - int(mmu50 * 100), - sccTotal / 1000, sccTotal % 1000, - sccLongest / 1000, sccLongest % 1000, - nonincrementalReason_ ? nonincrementalReason_ : "none", - unsigned(preBytes / 1024 / 1024), - counts[STAT_NEW_CHUNK], - counts[STAT_DESTROY_CHUNK]); + snprintf_literal(buffer, format, + (unsigned long long)timestamp, + longest / 1000, longest % 1000, + total / 1000, total % 1000, + zoneStats.collectedZoneCount, + zoneStats.zoneCount, + zoneStats.compartmentCount, + counts[STAT_MINOR_GC], + counts[STAT_STOREBUFFER_OVERFLOW], + int(mmu20 * 100), + int(mmu50 * 100), + sccTotal / 1000, sccTotal % 1000, + sccLongest / 1000, sccLongest % 1000, + nonincrementalReason_ ? nonincrementalReason_ : "none", + unsigned(preBytes / 1024 / 1024), + counts[STAT_NEW_CHUNK], + counts[STAT_DESTROY_CHUNK]); return DuplicateString(buffer); } @@ -698,18 +694,17 @@ Statistics::formatJsonSliceDescription(unsigned i, const SliceData& slice) "\"start_timestamp\":%llu," "\"end_timestamp\":%llu,"; char buffer[1024]; - memset(buffer, 0, sizeof(buffer)); - snprintf(buffer, sizeof(buffer), format, - i, - duration / 1000, duration % 1000, - when / 1000, when % 1000, - ExplainReason(slice.reason), - gc::StateName(slice.initialState), - gc::StateName(slice.finalState), - budgetDescription, - pageFaults, - slice.start, - slice.end); + snprintf_literal(buffer, format, + i, + duration / 1000, duration % 1000, + when / 1000, when % 1000, + ExplainReason(slice.reason), + gc::StateName(slice.initialState), + gc::StateName(slice.finalState), + budgetDescription, + pageFaults, + slice.start, + slice.end); return DuplicateString(buffer); } @@ -740,8 +735,8 @@ Statistics::formatJsonPhaseTimes(const PhaseTimeTable phaseTimes) UniqueChars name = FilterJsonKey(phases[phase].name); int64_t ownTime = phaseTimes[dagSlot][phase]; - snprintf(buffer, sizeof(buffer), "\"%s\":%" PRId64 ".%03" PRId64, - name.get(), ownTime / 1000, ownTime % 1000); + snprintf_literal(buffer, "\"%s\":%" PRId64 ".%03" PRId64, + name.get(), ownTime / 1000, ownTime % 1000); if (!fragments.append(DuplicateString(buffer))) return UniqueChars(nullptr); diff --git a/js/src/jsdate.cpp b/js/src/jsdate.cpp index 338f2b323505b..99088efdb922f 100644 --- a/js/src/jsdate.cpp +++ b/js/src/jsdate.cpp @@ -19,6 +19,7 @@ #include "mozilla/ArrayUtils.h" #include "mozilla/FloatingPoint.h" +#include "mozilla/Snprintf.h" #include #include @@ -2467,7 +2468,7 @@ date_toGMTString_impl(JSContext* cx, const CallArgs& args) char buf[100]; if (!IsFinite(utctime)) - snprintf(buf, sizeof buf, js_NaN_date_str); + snprintf_literal(buf, js_NaN_date_str); else print_gmt_string(buf, sizeof buf, utctime); @@ -2592,7 +2593,7 @@ date_format(JSContext* cx, double date, formatspec format, MutableHandleValue rv PRMJTime split; if (!IsFinite(date)) { - snprintf(buf, sizeof buf, js_NaN_date_str); + snprintf_literal(buf, js_NaN_date_str); } else { MOZ_ASSERT(NumbersAreIdentical(TimeClip(date).toDouble(), date)); @@ -2653,38 +2654,35 @@ date_format(JSContext* cx, double date, formatspec format, MutableHandleValue rv * requires a PRMJTime... which only has 16-bit years. Sub-ECMA. */ /* Tue Oct 31 2000 09:41:40 GMT-0800 (PST) */ - snprintf(buf, sizeof buf, - "%s %s %.2d %.4d %.2d:%.2d:%.2d GMT%+.4d%s%s", - days[int(WeekDay(local))], - months[int(MonthFromTime(local))], - int(DateFromTime(local)), - int(YearFromTime(local)), - int(HourFromTime(local)), - int(MinFromTime(local)), - int(SecFromTime(local)), - offset, - usetz ? " " : "", - usetz ? tzbuf : ""); + snprintf_literal(buf, "%s %s %.2d %.4d %.2d:%.2d:%.2d GMT%+.4d%s%s", + days[int(WeekDay(local))], + months[int(MonthFromTime(local))], + int(DateFromTime(local)), + int(YearFromTime(local)), + int(HourFromTime(local)), + int(MinFromTime(local)), + int(SecFromTime(local)), + offset, + usetz ? " " : "", + usetz ? tzbuf : ""); break; case FORMATSPEC_DATE: /* Tue Oct 31 2000 */ - snprintf(buf, sizeof buf, - "%s %s %.2d %.4d", - days[int(WeekDay(local))], - months[int(MonthFromTime(local))], - int(DateFromTime(local)), - int(YearFromTime(local))); + snprintf_literal(buf, "%s %s %.2d %.4d", + days[int(WeekDay(local))], + months[int(MonthFromTime(local))], + int(DateFromTime(local)), + int(YearFromTime(local))); break; case FORMATSPEC_TIME: /* 09:41:40 GMT-0800 (PST) */ - snprintf(buf, sizeof buf, - "%.2d:%.2d:%.2d GMT%+.4d%s%s", - int(HourFromTime(local)), - int(MinFromTime(local)), - int(SecFromTime(local)), - offset, - usetz ? " " : "", - usetz ? tzbuf : ""); + snprintf_literal(buf, "%.2d:%.2d:%.2d GMT%+.4d%s%s", + int(HourFromTime(local)), + int(MinFromTime(local)), + int(SecFromTime(local)), + offset, + usetz ? " " : "", + usetz ? tzbuf : ""); break; } } @@ -2703,7 +2701,7 @@ ToLocaleFormatHelper(JSContext* cx, HandleObject obj, const char* format, Mutabl char buf[100]; if (!IsFinite(utctime)) { - snprintf(buf, sizeof buf, js_NaN_date_str); + snprintf_literal(buf, js_NaN_date_str); } else { int result_len; double local = LocalTime(utctime); diff --git a/js/src/jsopcode.cpp b/js/src/jsopcode.cpp index bcb1798a8ba42..88c8e8837922a 100644 --- a/js/src/jsopcode.cpp +++ b/js/src/jsopcode.cpp @@ -13,6 +13,7 @@ #define __STDC_FORMAT_MACROS #include "mozilla/SizePrintfMacros.h" +#include "mozilla/Snprintf.h" #include #include @@ -842,8 +843,8 @@ js::Disassemble1(JSContext* cx, HandleScript script, jsbytecode* pc, JSOp op = (JSOp)*pc; if (op >= JSOP_LIMIT) { char numBuf1[12], numBuf2[12]; - snprintf(numBuf1, sizeof numBuf1, "%d", op); - snprintf(numBuf2, sizeof numBuf2, "%d", JSOP_LIMIT); + snprintf_literal(numBuf1, "%d", op); + snprintf_literal(numBuf2, "%d", JSOP_LIMIT); JS_ReportErrorNumber(cx, GetErrorMessage, nullptr, JSMSG_BYTECODE_TOO_BIG, numBuf1, numBuf2); return 0; @@ -1016,7 +1017,7 @@ js::Disassemble1(JSContext* cx, HandleScript script, jsbytecode* pc, default: { char numBuf[12]; - snprintf(numBuf, sizeof numBuf, "%x", cs->format); + snprintf_literal(numBuf, "%x", cs->format); JS_ReportErrorNumber(cx, GetErrorMessage, nullptr, JSMSG_UNKNOWN_FORMAT, numBuf); return 0; diff --git a/js/src/shell/js.cpp b/js/src/shell/js.cpp index d8f33d99f95ae..4ebc8ac3ba15c 100644 --- a/js/src/shell/js.cpp +++ b/js/src/shell/js.cpp @@ -14,6 +14,7 @@ #include "mozilla/mozalloc.h" #include "mozilla/PodOperations.h" #include "mozilla/SizePrintfMacros.h" +#include "mozilla/Snprintf.h" #include "mozilla/TimeStamp.h" #ifdef XP_WIN @@ -1508,9 +1509,9 @@ Evaluate(JSContext* cx, unsigned argc, Value* vp) if (loadBytecode && assertEqBytecode) { if (saveLength != loadLength) { char loadLengthStr[16]; - snprintf(loadLengthStr, sizeof(loadLengthStr), "%" PRIu32, loadLength); + snprintf_literal(loadLengthStr, "%" PRIu32, loadLength); char saveLengthStr[16]; - snprintf(saveLengthStr, sizeof(saveLengthStr), "%" PRIu32, saveLength); + snprintf_literal(saveLengthStr,"%" PRIu32, saveLength); JS_ReportErrorNumber(cx, my_GetErrorMessage, nullptr, JSSMSG_CACHE_EQ_SIZE_FAILED, loadLengthStr, saveLengthStr);