Bug 1334278 - change JS_smprintf to return UniqueChars; r=froydnj

This changes JS_smprintf to return UniqueChars, rather than relying on
manual memory management.

MozReview-Commit-ID: ENjQJODYdD1

--HG--
extra : rebase_source : 4c8ad4719dce205a7ef25e41eca25c5af793bb47
This commit is contained in:
Tom Tromey 2017-03-03 15:10:11 -07:00
parent 99f4608655
commit bcbdcb14bb
24 changed files with 120 additions and 133 deletions

View File

@ -257,7 +257,7 @@ ToStringHelper(const char* aSeverity, const nsString& aMessage,
static const char format2[] =
"[%s: \"%s\"]";
char* temp;
UniqueChars temp;
char* tempMessage = nullptr;
char* tempSourceName = nullptr;
char* tempSourceLine = nullptr;
@ -301,8 +301,7 @@ ToStringHelper(const char* aSeverity, const nsString& aMessage,
if (!temp)
return NS_ERROR_OUT_OF_MEMORY;
aResult.Assign(temp);
JS_smprintf_free(temp);
aResult.Assign(temp.get());
return NS_OK;
}

View File

@ -1308,7 +1308,7 @@ NewUCollator(JSContext* cx, Handle<CollatorObject*> collator)
memcpy(newLocale + index, insert, insertLen);
memcpy(newLocale + index + insertLen, oldLocale + index, localeLen - index + 1); // '\0'
locale.clear();
locale.initBytes(newLocale);
locale.initBytes(JS::UniqueChars(newLocale));
} else {
MOZ_ASSERT(StringEqualsAscii(usage, "sort"));
}

View File

@ -2313,21 +2313,21 @@ LiveRange::toString() const
{
AutoEnterOOMUnsafeRegion oomUnsafe;
char* buf = JS_smprintf("v%u [%u,%u)", hasVreg() ? vreg() : 0, from().bits(), to().bits());
UniqueChars buf = JS_smprintf("v%u [%u,%u)", hasVreg() ? vreg() : 0, from().bits(), to().bits());
if (buf && bundle() && !bundle()->allocation().isBogus())
buf = JS_sprintf_append(buf, " %s", bundle()->allocation().toString().get());
buf = JS_sprintf_append(Move(buf), " %s", bundle()->allocation().toString().get());
if (buf && hasDefinition())
buf = JS_sprintf_append(buf, " (def)");
buf = JS_sprintf_append(Move(buf), " (def)");
for (UsePositionIterator iter = usesBegin(); buf && iter; iter++)
buf = JS_sprintf_append(buf, " %s@%u", iter->use()->toString().get(), iter->pos.bits());
buf = JS_sprintf_append(Move(buf), " %s@%u", iter->use()->toString().get(), iter->pos.bits());
if (!buf)
oomUnsafe.crash("LiveRange::toString()");
return UniqueChars(buf);
return buf;
}
UniqueChars
@ -2336,10 +2336,10 @@ LiveBundle::toString() const
AutoEnterOOMUnsafeRegion oomUnsafe;
// Suppress -Wformat warning.
char *buf = JS_smprintf("%s", "");
UniqueChars buf = JS_smprintf("%s", "");
for (LiveRange::BundleLinkIterator iter = rangesBegin(); buf && iter; iter++) {
buf = JS_sprintf_append(buf, "%s %s",
buf = JS_sprintf_append(Move(buf), "%s %s",
(iter == rangesBegin()) ? "" : " ##",
LiveRange::get(*iter)->toString().get());
}
@ -2347,7 +2347,7 @@ LiveBundle::toString() const
if (!buf)
oomUnsafe.crash("LiveBundle::toString()");
return UniqueChars(buf);
return buf;
}
#endif // JS_JITSPEW

View File

@ -3325,12 +3325,11 @@ jit::Invalidate(JSContext* cx, JSScript* script, bool resetUses, bool cancelOffT
filename = "<unknown>";
// Construct the descriptive string.
char* buf = JS_smprintf("Invalidate %s:%" PRIuSIZE, filename, script->lineno());
UniqueChars buf = JS_smprintf("Invalidate %s:%" PRIuSIZE, filename, script->lineno());
// Ignore the event on allocation failure.
if (buf) {
cx->runtime()->geckoProfiler().markEvent(buf);
JS_smprintf_free(buf);
cx->runtime()->geckoProfiler().markEvent(buf.get());
}
}

View File

@ -376,26 +376,26 @@ LDefinition::toString() const
{
AutoEnterOOMUnsafeRegion oomUnsafe;
char* buf;
UniqueChars buf;
if (isBogusTemp()) {
buf = JS_smprintf("bogus");
} else {
buf = JS_smprintf("v%u<%s>", virtualRegister(), typeName(type()));
if (buf) {
if (policy() == LDefinition::FIXED)
buf = JS_sprintf_append(buf, ":%s", output()->toString().get());
buf = JS_sprintf_append(Move(buf), ":%s", output()->toString().get());
else if (policy() == LDefinition::MUST_REUSE_INPUT)
buf = JS_sprintf_append(buf, ":tied(%u)", getReusedInput());
buf = JS_sprintf_append(Move(buf), ":tied(%u)", getReusedInput());
}
}
if (!buf)
oomUnsafe.crash("LDefinition::toString()");
return UniqueChars(buf);
return buf;
}
static char*
static UniqueChars
PrintUse(const LUse* use)
{
switch (use->policy()) {
@ -420,7 +420,7 @@ LAllocation::toString() const
{
AutoEnterOOMUnsafeRegion oomUnsafe;
char* buf;
UniqueChars buf;
if (isBogus()) {
buf = JS_smprintf("bogus");
} else {
@ -452,7 +452,7 @@ LAllocation::toString() const
if (!buf)
oomUnsafe.crash("LAllocation::toString()");
return UniqueChars(buf);
return buf;
}
void

View File

@ -1733,11 +1733,10 @@ MacroAssembler::printf(const char* output)
static void
Printf1_(const char* output, uintptr_t value) {
AutoEnterOOMUnsafeRegion oomUnsafe;
char* line = JS_sprintf_append(nullptr, output, value);
js::UniqueChars line = JS_sprintf_append(nullptr, output, value);
if (!line)
oomUnsafe.crash("OOM at masm.printf");
fprintf(stderr, "%s", line);
js_free(line);
fprintf(stderr, "%s", line.get());
}
void

View File

@ -306,9 +306,8 @@ Error(JSContext* cx, const char (&input)[N], uint32_t expectedLine,
CHECK(report.init(cx, exn, js::ErrorReport::WithSideEffects));
CHECK(report.report()->errorNumber == JSMSG_JSON_BAD_PARSE);
const char* lineAndColumnASCII = JS_smprintf("line %d column %d", expectedLine, expectedColumn);
CHECK(strstr(report.toStringResult().c_str(), lineAndColumnASCII) != nullptr);
js_free((void*)lineAndColumnASCII);
UniqueChars lineAndColumnASCII = JS_smprintf("line %d column %d", expectedLine, expectedColumn);
CHECK(strstr(report.toStringResult().c_str(), lineAndColumnASCII.get()) != nullptr);
/* We do not execute JS, so there should be no exception thrown. */
CHECK(!JS_IsExceptionPending(cx));

View File

@ -22,13 +22,10 @@ print_one (const char *expect, const char *fmt, ...)
va_list ap;
va_start(ap, fmt);
char *output = JS_vsmprintf (fmt, ap);
JS::UniqueChars output = JS_vsmprintf (fmt, ap);
va_end(ap);
bool result = output && !strcmp(output, expect);
JS_smprintf_free(output);
return result;
return output && !strcmp(output.get(), expect);
}
static const char *

View File

@ -5073,9 +5073,9 @@ class MOZ_RAII JSAutoByteString
}
/* Take ownership of the given byte array. */
void initBytes(char* bytes) {
void initBytes(JS::UniqueChars&& bytes) {
MOZ_ASSERT(!mBytes);
mBytes = bytes;
mBytes = bytes.release();
}
char* encodeLatin1(JSContext* cx, JSString* str) {

View File

@ -541,14 +541,13 @@ static bool
PrintSingleError(JSContext* cx, FILE* file, JS::ConstUTF8CharsZ toStringResult,
T* report, PrintErrorKind kind)
{
UniquePtr<char> prefix;
UniqueChars prefix;
if (report->filename)
prefix.reset(JS_smprintf("%s:", report->filename));
prefix = JS_smprintf("%s:", report->filename);
if (report->lineno) {
UniquePtr<char> tmp(JS_smprintf("%s%u:%u ", prefix ? prefix.get() : "", report->lineno,
report->column));
prefix = Move(tmp);
prefix = JS_smprintf("%s%u:%u ", prefix ? prefix.get() : "", report->lineno,
report->column);
}
if (kind != PrintErrorKind::Error) {
@ -567,8 +566,7 @@ PrintSingleError(JSContext* cx, FILE* file, JS::ConstUTF8CharsZ toStringResult,
break;
}
UniquePtr<char> tmp(JS_smprintf("%s%s: ", prefix ? prefix.get() : "", kindPrefix));
prefix = Move(tmp);
prefix = JS_smprintf("%s%s: ", prefix ? prefix.get() : "", kindPrefix);
}
const char* message = toStringResult ? toStringResult.c_str() : report->message().c_str();

View File

@ -791,7 +791,7 @@ sprintf_append(JSContext* cx, char* buf, const char* fmt, ...)
va_list ap;
va_start(ap, fmt);
char* result = JS_vsprintf_append(buf, fmt, ap);
char* result = JS_vsprintf_append(UniqueChars(buf), fmt, ap).release();
va_end(ap);
if (!result) {
@ -1046,7 +1046,7 @@ JS::FormatStackDump(JSContext* cx, char* buf, bool showArgs, bool showLocals, bo
}
if (!num)
buf = JS_sprintf_append(buf, "JavaScript stack is empty\n");
buf = JS_sprintf_append(UniqueChars(buf), "JavaScript stack is empty\n").release();
return buf;
}

View File

@ -1184,22 +1184,22 @@ ToDisassemblySource(JSContext* cx, HandleValue v, JSAutoByteString* bytes)
char* nbytes = QuoteString(&sprinter, v.toString(), '"');
if (!nbytes)
return false;
nbytes = JS_sprintf_append(nullptr, "%s", nbytes);
UniqueChars copy = JS_smprintf("%s", nbytes);
if (!nbytes) {
ReportOutOfMemory(cx);
return false;
}
bytes->initBytes(nbytes);
bytes->initBytes(Move(copy));
return true;
}
if (JS::CurrentThreadIsHeapBusy() || !cx->isAllocAllowed()) {
char* source = JS_sprintf_append(nullptr, "<value>");
UniqueChars source = JS_smprintf("<value>");
if (!source) {
ReportOutOfMemory(cx);
return false;
}
bytes->initBytes(source);
bytes->initBytes(Move(source));
return true;
}
@ -1228,7 +1228,7 @@ ToDisassemblySource(JSContext* cx, HandleValue v, JSAutoByteString* bytes)
static bool
ToDisassemblySource(JSContext* cx, HandleScope scope, JSAutoByteString* bytes)
{
char* source = JS_sprintf_append(nullptr, "%s {", ScopeKindString(scope->kind()));
UniqueChars source = JS_smprintf("%s {", ScopeKindString(scope->kind()));
if (!source) {
ReportOutOfMemory(cx);
return false;
@ -1239,7 +1239,7 @@ ToDisassemblySource(JSContext* cx, HandleScope scope, JSAutoByteString* bytes)
if (!AtomToPrintableString(cx, bi.name(), &nameBytes))
return false;
source = JS_sprintf_append(source, "%s: ", nameBytes.ptr());
source = JS_sprintf_append(Move(source), "%s: ", nameBytes.ptr());
if (!source) {
ReportOutOfMemory(cx);
return false;
@ -1248,27 +1248,27 @@ ToDisassemblySource(JSContext* cx, HandleScope scope, JSAutoByteString* bytes)
BindingLocation loc = bi.location();
switch (loc.kind()) {
case BindingLocation::Kind::Global:
source = JS_sprintf_append(source, "global");
source = JS_sprintf_append(Move(source), "global");
break;
case BindingLocation::Kind::Frame:
source = JS_sprintf_append(source, "frame slot %u", loc.slot());
source = JS_sprintf_append(Move(source), "frame slot %u", loc.slot());
break;
case BindingLocation::Kind::Environment:
source = JS_sprintf_append(source, "env slot %u", loc.slot());
source = JS_sprintf_append(Move(source), "env slot %u", loc.slot());
break;
case BindingLocation::Kind::Argument:
source = JS_sprintf_append(source, "arg slot %u", loc.slot());
source = JS_sprintf_append(Move(source), "arg slot %u", loc.slot());
break;
case BindingLocation::Kind::NamedLambdaCallee:
source = JS_sprintf_append(source, "named lambda callee");
source = JS_sprintf_append(Move(source), "named lambda callee");
break;
case BindingLocation::Kind::Import:
source = JS_sprintf_append(source, "import");
source = JS_sprintf_append(Move(source), "import");
break;
}
@ -1278,7 +1278,7 @@ ToDisassemblySource(JSContext* cx, HandleScope scope, JSAutoByteString* bytes)
}
if (!bi.isLast()) {
source = JS_sprintf_append(source, ", ");
source = JS_sprintf_append(Move(source), ", ");
if (!source) {
ReportOutOfMemory(cx);
return false;
@ -1286,13 +1286,13 @@ ToDisassemblySource(JSContext* cx, HandleScope scope, JSAutoByteString* bytes)
}
}
source = JS_sprintf_append(source, "}");
source = JS_sprintf_append(Move(source), "}");
if (!source) {
ReportOutOfMemory(cx);
return false;
}
bytes->initBytes(source);
bytes->initBytes(Move(source));
return true;
}

View File

@ -20,13 +20,13 @@ using namespace js;
typedef mozilla::SmprintfPolicyPointer<js::SystemAllocPolicy> JSSmprintfPointer;
JS_PUBLIC_API(char*) JS_smprintf(const char* fmt, ...)
JS_PUBLIC_API(JS::UniqueChars) JS_smprintf(const char* fmt, ...)
{
va_list ap;
va_start(ap, fmt);
JSSmprintfPointer result = mozilla::Vsmprintf<js::SystemAllocPolicy>(fmt, ap);
va_end(ap);
return result.release();
return JS::UniqueChars(result.release());
}
JS_PUBLIC_API(void) JS_smprintf_free(char* mem)
@ -34,23 +34,26 @@ JS_PUBLIC_API(void) JS_smprintf_free(char* mem)
mozilla::SmprintfFree<js::SystemAllocPolicy>(mem);
}
JS_PUBLIC_API(char*) JS_sprintf_append(char* last, const char* fmt, ...)
JS_PUBLIC_API(JS::UniqueChars) JS_sprintf_append(JS::UniqueChars&& last, const char* fmt, ...)
{
va_list ap;
va_start(ap, fmt);
JSSmprintfPointer lastPtr(last.release());
JSSmprintfPointer result =
mozilla::VsmprintfAppend<js::SystemAllocPolicy>(JSSmprintfPointer(last), fmt, ap);
mozilla::VsmprintfAppend<js::SystemAllocPolicy>(Move(lastPtr), fmt, ap);
va_end(ap);
return result.release();
return JS::UniqueChars(result.release());
}
JS_PUBLIC_API(char*) JS_vsmprintf(const char* fmt, va_list ap)
JS_PUBLIC_API(JS::UniqueChars) JS_vsmprintf(const char* fmt, va_list ap)
{
return mozilla::Vsmprintf<js::SystemAllocPolicy>(fmt, ap).release();
return JS::UniqueChars(mozilla::Vsmprintf<js::SystemAllocPolicy>(fmt, ap).release());
}
JS_PUBLIC_API(char*) JS_vsprintf_append(char* last, const char* fmt, va_list ap)
JS_PUBLIC_API(JS::UniqueChars) JS_vsprintf_append(JS::UniqueChars&& last,
const char* fmt, va_list ap)
{
return mozilla::VsmprintfAppend<js::SystemAllocPolicy>(JSSmprintfPointer(last),
fmt, ap).release();
JSSmprintfPointer lastPtr(last.release());
return JS::UniqueChars(mozilla::VsmprintfAppend<js::SystemAllocPolicy>(Move(lastPtr),
fmt, ap).release());
}

View File

@ -12,19 +12,22 @@
#include <stdarg.h>
#include "jstypes.h"
#include "js/Utility.h"
/* Wrappers for mozilla::Smprintf and friends that are used throughout
JS. */
extern JS_PUBLIC_API(char*) JS_smprintf(const char* fmt, ...)
extern JS_PUBLIC_API(JS::UniqueChars) JS_smprintf(const char* fmt, ...)
MOZ_FORMAT_PRINTF(1, 2);
extern JS_PUBLIC_API(void) JS_smprintf_free(char* mem);
extern JS_PUBLIC_API(char*) JS_sprintf_append(char* last, const char* fmt, ...)
extern JS_PUBLIC_API(JS::UniqueChars) JS_sprintf_append(JS::UniqueChars&& last,
const char* fmt, ...)
MOZ_FORMAT_PRINTF(2, 3);
extern JS_PUBLIC_API(char*) JS_vsmprintf(const char* fmt, va_list ap);
extern JS_PUBLIC_API(char*) JS_vsprintf_append(char* last, const char* fmt, va_list ap);
extern JS_PUBLIC_API(JS::UniqueChars) JS_vsmprintf(const char* fmt, va_list ap);
extern JS_PUBLIC_API(JS::UniqueChars) JS_vsprintf_append(JS::UniqueChars&& last,
const char* fmt, va_list ap);
#endif /* jsprf_h */

View File

@ -1363,22 +1363,22 @@ Options(JSContext* cx, unsigned argc, Value* vp)
}
}
char* names = strdup("");
UniqueChars names = DuplicateString("");
bool found = false;
if (names && oldContextOptions.extraWarnings()) {
names = JS_sprintf_append(names, "%s%s", found ? "," : "", "strict");
names = JS_sprintf_append(Move(names), "%s%s", found ? "," : "", "strict");
found = true;
}
if (names && oldContextOptions.werror()) {
names = JS_sprintf_append(names, "%s%s", found ? "," : "", "werror");
names = JS_sprintf_append(Move(names), "%s%s", found ? "," : "", "werror");
found = true;
}
if (names && oldContextOptions.throwOnAsmJSValidationFailure()) {
names = JS_sprintf_append(names, "%s%s", found ? "," : "", "throw_on_asmjs_validation_failure");
names = JS_sprintf_append(Move(names), "%s%s", found ? "," : "", "throw_on_asmjs_validation_failure");
found = true;
}
if (names && oldContextOptions.strictMode()) {
names = JS_sprintf_append(names, "%s%s", found ? "," : "", "strict_mode");
names = JS_sprintf_append(Move(names), "%s%s", found ? "," : "", "strict_mode");
found = true;
}
if (!names) {
@ -1386,8 +1386,7 @@ Options(JSContext* cx, unsigned argc, Value* vp)
return false;
}
JSString* str = JS_NewStringCopyZ(cx, names);
free(names);
JSString* str = JS_NewStringCopyZ(cx, names.get());
if (!str)
return false;
args.rval().setString(str);
@ -4944,10 +4943,10 @@ NestedShell(JSContext* cx, unsigned argc, Value* vp)
// As a special case, if the caller passes "--js-cache", replace that
// with "--js-cache=$(jsCacheDir)"
if (!strcmp(argv.back(), "--js-cache") && jsCacheDir) {
char* newArg = JS_smprintf("--js-cache=%s", jsCacheDir);
UniqueChars newArg = JS_smprintf("--js-cache=%s", jsCacheDir);
if (!newArg)
return false;
argv.replaceBack(newArg);
argv.replaceBack(newArg.release());
}
}
@ -8198,12 +8197,12 @@ SetContextOptions(JSContext* cx, const OptionParser& op)
jsCacheDir = op.getStringOption("js-cache");
if (jsCacheDir) {
if (!op.getBoolOption("no-js-cache-per-process"))
jsCacheDir = JS_smprintf("%s/%u", jsCacheDir, (unsigned)getpid());
jsCacheDir = JS_smprintf("%s/%u", jsCacheDir, (unsigned)getpid()).release();
else
jsCacheDir = JS_strdup(cx, jsCacheDir);
if (!jsCacheDir)
return false;
jsCacheAsmJSPath = JS_smprintf("%s/asmjs.cache", jsCacheDir);
jsCacheAsmJSPath = JS_smprintf("%s/asmjs.cache", jsCacheDir).release();
}
#ifdef DEBUG

View File

@ -291,7 +291,7 @@ ThrowErrorWithType(JSContext* cx, JSExnType type, const CallArgs& args)
UniqueChars bytes = DecompileValueGenerator(cx, JSDVG_SEARCH_STACK, val, nullptr);
if (!bytes)
return;
errorArgs[i - 1].initBytes(bytes.release());
errorArgs[i - 1].initBytes(Move(bytes));
}
if (!errorArgs[i - 1])
return;

View File

@ -2273,7 +2273,7 @@ class MOZ_STACK_CLASS ModuleValidator
MOZ_ASSERT(errorOffset_ == UINT32_MAX);
MOZ_ASSERT(fmt);
errorOffset_ = offset;
errorString_.reset(JS_vsmprintf(fmt, ap));
errorString_ = JS_vsmprintf(fmt, ap);
return false;
}
@ -8588,7 +8588,7 @@ LookupAsmJSModuleInCache(JSContext* cx, AsmJSParser& parser, bool* loadedFromCac
int64_t after = PRMJ_Now();
int ms = (after - before) / PRMJ_USEC_PER_MSEC;
*compilationTimeReport = UniqueChars(JS_smprintf("loaded from cache in %dms", ms));
*compilationTimeReport = JS_smprintf("loaded from cache in %dms", ms);
if (!*compilationTimeReport)
return false;
@ -8689,7 +8689,7 @@ BuildConsoleMessage(JSContext* cx, unsigned time, JS::AsmJSCacheResult cacheResu
break;
}
return UniqueChars(JS_smprintf("total compilation time %dms; %s", time, cacheString));
return JS_smprintf("total compilation time %dms; %s", time, cacheString);
#else
return DuplicateString("");
#endif

View File

@ -556,11 +556,11 @@ class WasmTokenStream
{}
void generateError(WasmToken token, UniqueChars* error) {
unsigned column = token.begin() - lineStart_ + 1;
error->reset(JS_smprintf("parsing wasm text at %u:%u", line_, column));
*error = JS_smprintf("parsing wasm text at %u:%u", line_, column);
}
void generateError(WasmToken token, const char* msg, UniqueChars* error) {
unsigned column = token.begin() - lineStart_ + 1;
error->reset(JS_smprintf("parsing wasm text at %u:%u: %s", line_, column, msg));
*error = JS_smprintf("parsing wasm text at %u:%u: %s", line_, column, msg);
}
WasmToken peek() {
if (!lookaheadDepth_) {
@ -3410,7 +3410,7 @@ class Resolver
bool failResolveLabel(const char* kind, AstName name) {
TwoByteChars chars(name.begin(), name.length());
UniqueChars utf8Chars(CharsToNewUTF8CharsZ(nullptr, chars).c_str());
error_->reset(JS_smprintf("%s label '%s' not found", kind, utf8Chars.get()));
*error_ = JS_smprintf("%s label '%s' not found", kind, utf8Chars.get());
return false;
}
@ -3494,7 +3494,7 @@ class Resolver
}
bool fail(const char* message) {
error_->reset(JS_smprintf("%s", message));
*error_ = JS_smprintf("%s", message);
return false;
}
};

View File

@ -139,11 +139,11 @@ public:
explicit JSCLContextHelper(JSContext* aCx);
~JSCLContextHelper();
void reportErrorAfterPop(char* buf);
void reportErrorAfterPop(UniqueChars&& buf);
private:
JSContext* mContext;
char* mBuf;
UniqueChars mBuf;
// prevent copying and assignment
JSCLContextHelper(const JSCLContextHelper&) = delete;
@ -161,14 +161,13 @@ ReportOnCallerUTF8(JSContext* callerContext,
va_list ap;
va_start(ap, format);
char* buf = JS_vsmprintf(format, ap);
UniqueChars buf = JS_vsmprintf(format, ap);
if (!buf) {
va_end(ap);
return NS_ERROR_OUT_OF_MEMORY;
}
JS_ReportErrorUTF8(callerContext, "%s", buf);
JS_smprintf_free(buf);
JS_ReportErrorUTF8(callerContext, "%s", buf.get());
va_end(ap);
return NS_OK;
@ -182,13 +181,13 @@ ReportOnCallerUTF8(JSCLContextHelper& helper,
va_list ap;
va_start(ap, format);
char* buf = JS_vsmprintf(format, ap);
UniqueChars buf = JS_vsmprintf(format, ap);
if (!buf) {
va_end(ap);
return NS_ERROR_OUT_OF_MEMORY;
}
helper.reportErrorAfterPop(buf);
helper.reportErrorAfterPop(Move(buf));
va_end(ap);
return NS_OK;
}
@ -1351,14 +1350,13 @@ JSCLContextHelper::JSCLContextHelper(JSContext* aCx)
JSCLContextHelper::~JSCLContextHelper()
{
if (mBuf) {
JS_ReportErrorUTF8(mContext, "%s", mBuf);
JS_smprintf_free(mBuf);
JS_ReportErrorUTF8(mContext, "%s", mBuf.get());
}
}
void
JSCLContextHelper::reportErrorAfterPop(char* buf)
JSCLContextHelper::reportErrorAfterPop(UniqueChars&& buf)
{
MOZ_ASSERT(!mBuf, "Already called reportErrorAfterPop");
mBuf = buf;
mBuf = Move(buf);
}

View File

@ -520,31 +520,30 @@ Options(JSContext* cx, unsigned argc, Value* vp)
}
}
char* names = nullptr;
UniqueChars names;
if (oldContextOptions.extraWarnings()) {
names = JS_sprintf_append(names, "%s", "strict");
names = JS_sprintf_append(Move(names), "%s", "strict");
if (!names) {
JS_ReportOutOfMemory(cx);
return false;
}
}
if (oldContextOptions.werror()) {
names = JS_sprintf_append(names, "%s%s", names ? "," : "", "werror");
names = JS_sprintf_append(Move(names), "%s%s", names ? "," : "", "werror");
if (!names) {
JS_ReportOutOfMemory(cx);
return false;
}
}
if (names && oldContextOptions.strictMode()) {
names = JS_sprintf_append(names, "%s%s", names ? "," : "", "strict_mode");
names = JS_sprintf_append(Move(names), "%s%s", names ? "," : "", "strict_mode");
if (!names) {
JS_ReportOutOfMemory(cx);
return false;
}
}
str = JS_NewStringCopyZ(cx, names);
free(names);
str = JS_NewStringCopyZ(cx, names.get());
if (!str)
return false;
@ -717,7 +716,7 @@ env_setProperty(JSContext* cx, HandleObject obj, HandleId id, MutableHandleValue
return false;
#if defined XP_WIN || defined HPUX || defined OSF1 || defined SCO
{
char* waste = JS_smprintf("%s=%s", name.ptr(), value.ptr());
char* waste = JS_smprintf("%s=%s", name.ptr(), value.ptr()).release();
if (!waste) {
JS_ReportOutOfMemory(cx);
return false;

View File

@ -116,9 +116,9 @@ XPCThrower::ThrowBadResult(nsresult rv, nsresult result, XPCCallContext& ccx)
format = "";
if (nsXPCException::NameAndFormatForNSResult(result, &name, nullptr) && name)
sz = JS_smprintf("%s 0x%x (%s)", format, (unsigned) result, name);
sz = JS_smprintf("%s 0x%x (%s)", format, (unsigned) result, name).release();
else
sz = JS_smprintf("%s 0x%x", format, (unsigned) result);
sz = JS_smprintf("%s 0x%x", format, (unsigned) result).release();
NS_ENSURE_TRUE_VOID(sz);
if (sz && sVerbose)
@ -140,7 +140,7 @@ XPCThrower::ThrowBadParam(nsresult rv, unsigned paramNum, XPCCallContext& ccx)
if (!nsXPCException::NameAndFormatForNSResult(rv, nullptr, &format))
format = "";
sz = JS_smprintf("%s arg %d", format, paramNum);
sz = JS_smprintf("%s arg %d", format, paramNum).release();
NS_ENSURE_TRUE_VOID(sz);
if (sz && sVerbose)
@ -168,7 +168,7 @@ XPCThrower::Verbosify(XPCCallContext& ccx,
if (!name) {
name = "";
}
sz = JS_smprintf("%s [%s.%s]", *psz, iface->GetNameString(), name);
sz = JS_smprintf("%s [%s.%s]", *psz, iface->GetNameString(), name).release();
}
if (sz) {

View File

@ -1220,16 +1220,14 @@ pre_call_clean_up:
NS_ERROR_XPC_JSOBJECT_HAS_NO_FUNCTION_NAMED;
static const char format[] = "%s \"%s\"";
const char * msg;
char* sz = nullptr;
UniqueChars sz;
if (nsXPCException::NameAndFormatForNSResult(code, nullptr, &msg) && msg)
sz = JS_smprintf(format, msg, name);
XPCConvert::ConstructException(code, sz, GetInterfaceName(), name,
XPCConvert::ConstructException(code, sz.get(), GetInterfaceName(), name,
nullptr, getter_AddRefs(syntheticException),
nullptr, nullptr);
if (sz)
JS_smprintf_free(sz);
success = false;
}
}

View File

@ -2141,15 +2141,15 @@ XPCWrappedNative::ToString(XPCWrappedNativeTearOff* to /* = nullptr */ ) const
# define PARAM_ADDR(w)
#endif
char* sz = nullptr;
char* name = nullptr;
UniqueChars sz;
UniqueChars name;
nsCOMPtr<nsIXPCScriptable> scr = GetScriptable();
if (scr)
name = JS_smprintf("%s", scr->GetJSClass()->name);
if (to) {
const char* fmt = name ? " (%s)" : "%s";
name = JS_sprintf_append(name, fmt,
name = JS_sprintf_append(Move(name), fmt,
to->GetInterface()->GetNameString());
} else if (!name) {
XPCNativeSet* set = GetSet();
@ -2158,15 +2158,15 @@ XPCWrappedNative::ToString(XPCWrappedNativeTearOff* to /* = nullptr */ ) const
uint16_t count = set->GetInterfaceCount();
if (count == 1)
name = JS_sprintf_append(name, "%s", array[0]->GetNameString());
name = JS_sprintf_append(Move(name), "%s", array[0]->GetNameString());
else if (count == 2 && array[0] == isupp) {
name = JS_sprintf_append(name, "%s", array[1]->GetNameString());
name = JS_sprintf_append(Move(name), "%s", array[1]->GetNameString());
} else {
for (uint16_t i = 0; i < count; i++) {
const char* fmt = (i == 0) ?
"(%s" : (i == count-1) ?
", %s)" : ", %s";
name = JS_sprintf_append(name, fmt,
name = JS_sprintf_append(Move(name), fmt,
array[i]->GetNameString());
}
}
@ -2180,12 +2180,9 @@ XPCWrappedNative::ToString(XPCWrappedNativeTearOff* to /* = nullptr */ ) const
if (scr) {
fmt = "[object %s" FMT_ADDR FMT_STR(" (native") FMT_ADDR FMT_STR(")") "]";
}
sz = JS_smprintf(fmt, name PARAM_ADDR(this) PARAM_ADDR(mIdentity.get()));
sz = JS_smprintf(fmt, name.get() PARAM_ADDR(this) PARAM_ADDR(mIdentity.get()));
JS_smprintf_free(name);
return sz;
return sz.release();
#undef FMT_ADDR
#undef PARAM_ADDR

View File

@ -44,11 +44,11 @@ static bool Throw(nsresult errNum, JSContext* cx)
static bool
ToStringGuts(XPCCallContext& ccx)
{
char* sz;
UniqueChars sz;
XPCWrappedNative* wrapper = ccx.GetWrapper();
if (wrapper)
sz = wrapper->ToString(ccx.GetTearOff());
sz.reset(wrapper->ToString(ccx.GetTearOff()));
else
sz = JS_smprintf("[xpconnect wrapped native prototype]");
@ -57,8 +57,7 @@ ToStringGuts(XPCCallContext& ccx)
return false;
}
JSString* str = JS_NewStringCopyZ(ccx, sz);
JS_smprintf_free(sz);
JSString* str = JS_NewStringCopyZ(ccx, sz.get());
if (!str)
return false;