Bug 1334318 - remove last uses of PR_smprintf; r=froydnj

This removes the last uses of PR_smprintf from the tree (excluding the
security and nsprpub directories).  It also fixes a related latent bug
in nsAppRunner.cpp (which was incorrectly freeing the pointer passed to
PR_SetEnv).

MozReview-Commit-ID: GynP2PhuWWO

--HG--
extra : rebase_source : c3b83c7bd08b1c222e137a00323caf5481352845
This commit is contained in:
Tom Tromey 2017-04-28 10:13:26 -06:00
parent 74687b05c2
commit bd012d9546
4 changed files with 27 additions and 23 deletions

View File

@ -27,6 +27,7 @@
#include "prio.h"
#include "prprf.h"
#include "mozilla/Maybe.h"
#include "mozilla/Printf.h"
#include "mozilla/UniquePtr.h"
using namespace JS;
@ -64,7 +65,7 @@ TestStartupCache::TestStartupCache()
mSCFile->AppendNative(NS_LITERAL_CSTRING("test-startupcache.tmp"));
nsAutoCString path;
mSCFile->GetNativePath(path);
char* env = PR_smprintf("MOZ_STARTUP_CACHE=%s", path.get());
char* env = mozilla::Smprintf("MOZ_STARTUP_CACHE=%s", path.get()).release();
PR_SetEnv(env);
// We intentionally leak `env` here because it is required by PR_SetEnv
MOZ_LSAN_INTENTIONALLY_LEAK_OBJECT(env);

View File

@ -314,7 +314,7 @@ EnvHasValue(const char *name)
static void
SaveWordToEnv(const char *name, const nsACString & word)
{
char *expr = PR_smprintf("%s=%s", name, PromiseFlatCString(word).get());
char *expr = Smprintf("%s=%s", name, PromiseFlatCString(word).get()).release();
if (expr)
PR_SetEnv(expr);
// We intentionally leak |expr| here since it is required by PR_SetEnv.
@ -2810,7 +2810,7 @@ static void SaveStateForAppInitiatedRestart()
for (auto & savedVar : gSavedVars) {
const char *s = PR_GetEnv(savedVar.name);
if (s)
savedVar.value = PR_smprintf("%s=%s", savedVar.name, s);
savedVar.value = Smprintf("%s=%s", savedVar.name, s).release();
}
}
@ -4415,7 +4415,8 @@ XREMain::XRE_mainRun()
char appFile[MAX_PATH];
if (GetEnvironmentVariableA("XUL_APP_FILE", appFile, sizeof(appFile))) {
SmprintfPointer saved = mozilla::Smprintf("XUL_APP_FILE=%s", appFile);
PR_SetEnv(saved.get());
// We intentionally leak the string here since it is required by PR_SetEnv.
PR_SetEnv(saved.release());
}
#endif

View File

@ -24,6 +24,7 @@
#include "mozilla/Preferences.h"
#include "nsPrintfCString.h"
#include "mozilla/DebugOnly.h"
#include "mozilla/Printf.h"
#ifdef XP_MACOSX
#include "nsILocalFileMac.h"
@ -406,18 +407,15 @@ AppendToLibPath(const char *pathToAppend)
{
char *pathValue = getenv(LD_LIBRARY_PATH_ENVVAR_NAME);
if (nullptr == pathValue || '\0' == *pathValue) {
char *s = PR_smprintf("%s=%s", LD_LIBRARY_PATH_ENVVAR_NAME, pathToAppend);
// Leak the string because that is required by PR_SetEnv.
char *s = Smprintf("%s=%s", LD_LIBRARY_PATH_ENVVAR_NAME, pathToAppend).release();
PR_SetEnv(s);
} else if (!strstr(pathValue, pathToAppend)) {
char *s = PR_smprintf("%s=%s" PATH_SEPARATOR "%s",
LD_LIBRARY_PATH_ENVVAR_NAME, pathToAppend, pathValue);
// Leak the string because that is required by PR_SetEnv.
char *s = Smprintf("%s=%s" PATH_SEPARATOR "%s",
LD_LIBRARY_PATH_ENVVAR_NAME, pathToAppend, pathValue).release();
PR_SetEnv(s);
}
// The memory used by PR_SetEnv is not copied to the environment on all
// platform, it can be used by reference directly. So we purposely do not
// call PR_smprintf_free on s. Subsequent calls to PR_SetEnv will free
// the old memory first.
}
#endif

View File

@ -14,6 +14,8 @@
#include "nsMemory.h"
#include "nsString.h"
#include "nsCRTGlue.h"
#include "mozilla/IntegerPrintfMacros.h"
#include "mozilla/Printf.h"
/***************************************************************************/
// Helpers for static convert functions...
@ -746,7 +748,7 @@ nsDiscriminatedUnion::ConvertToID(nsID* aResult) const
nsresult
nsDiscriminatedUnion::ToString(nsACString& aOutString) const
{
char* ptr;
mozilla::SmprintfPointer pptr;
switch (mType) {
// all the stuff we don't handle...
@ -778,16 +780,17 @@ nsDiscriminatedUnion::ToString(nsACString& aOutString) const
// nsID has its own text formatter.
case nsIDataType::VTYPE_ID:
ptr = u.mIDValue.ToString();
case nsIDataType::VTYPE_ID: {
char* ptr = u.mIDValue.ToString();
if (!ptr) {
return NS_ERROR_OUT_OF_MEMORY;
}
aOutString.Assign(ptr);
free(ptr);
return NS_OK;
}
// Can't use PR_smprintf for floats, since it's locale-dependent
// Can't use Smprintf for floats, since it's locale-dependent
#define CASE__APPENDFLOAT_NUMBER(type_, member_) \
case nsIDataType::type_ : \
{ \
@ -802,22 +805,24 @@ nsDiscriminatedUnion::ToString(nsACString& aOutString) const
#undef CASE__APPENDFLOAT_NUMBER
// the rest can be PR_smprintf'd and use common code.
// the rest can be Smprintf'd and use common code.
#define CASE__SMPRINTF_NUMBER(type_, format_, cast_, member_) \
case nsIDataType::type_: \
ptr = PR_smprintf( format_ , (cast_) u.member_); \
static_assert(sizeof(cast_) >= sizeof(u.member_), \
"size of type should be at least as big as member"); \
pptr = mozilla::Smprintf( format_ , (cast_) u.member_); \
break;
CASE__SMPRINTF_NUMBER(VTYPE_INT8, "%d", int, mInt8Value)
CASE__SMPRINTF_NUMBER(VTYPE_INT16, "%d", int, mInt16Value)
CASE__SMPRINTF_NUMBER(VTYPE_INT32, "%d", int, mInt32Value)
CASE__SMPRINTF_NUMBER(VTYPE_INT64, "%lld", int64_t, mInt64Value)
CASE__SMPRINTF_NUMBER(VTYPE_INT64, "%" PRId64, int64_t, mInt64Value)
CASE__SMPRINTF_NUMBER(VTYPE_UINT8, "%u", unsigned, mUint8Value)
CASE__SMPRINTF_NUMBER(VTYPE_UINT16, "%u", unsigned, mUint16Value)
CASE__SMPRINTF_NUMBER(VTYPE_UINT32, "%u", unsigned, mUint32Value)
CASE__SMPRINTF_NUMBER(VTYPE_UINT64, "%llu", int64_t, mUint64Value)
CASE__SMPRINTF_NUMBER(VTYPE_UINT64, "%" PRIu64, int64_t, mUint64Value)
// XXX Would we rather print "true" / "false" ?
CASE__SMPRINTF_NUMBER(VTYPE_BOOL, "%d", int, mBoolValue)
@ -827,11 +832,10 @@ nsDiscriminatedUnion::ToString(nsACString& aOutString) const
#undef CASE__SMPRINTF_NUMBER
}
if (!ptr) {
if (!pptr) {
return NS_ERROR_OUT_OF_MEMORY;
}
aOutString.Assign(ptr);
PR_smprintf_free(ptr);
aOutString.Assign(pptr.get());
return NS_OK;
}