Skip to content
This repository has been archived by the owner on Nov 7, 2018. It is now read-only.

Commit

Permalink
Bug 1486690 - Rename nsMemory::Clone() and remove unnecessary checks …
Browse files Browse the repository at this point in the history
…after it. r=glandium

The 'x' in the new name makes it clearer that it's infallible.
  • Loading branch information
nnethercote committed Aug 28, 2018
1 parent 0870829 commit eafd8e4
Show file tree
Hide file tree
Showing 15 changed files with 44 additions and 77 deletions.
6 changes: 3 additions & 3 deletions extensions/auth/nsAuthGSSAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ nsAuthGSSAPI::GetNextToken(const void *inToken,

*outTokenLen = output_token.length;
if (output_token.length != 0)
*outToken = nsMemory::Clone(output_token.value, output_token.length);
*outToken = moz_xmemdup(output_token.value, output_token.length);
else
*outToken = nullptr;

Expand Down Expand Up @@ -565,7 +565,7 @@ nsAuthGSSAPI::Unwrap(const void *inToken,
*outTokenLen = output_token.length;

if (output_token.length)
*outToken = nsMemory::Clone(output_token.value, output_token.length);
*outToken = moz_xmemdup(output_token.value, output_token.length);
else
*outToken = nullptr;

Expand Down Expand Up @@ -607,7 +607,7 @@ nsAuthGSSAPI::Wrap(const void *inToken,
*outTokenLen = output_token.length;

/* it is not possible for output_token.length to be zero */
*outToken = nsMemory::Clone(output_token.value, output_token.length);
*outToken = moz_xmemdup(output_token.value, output_token.length);
gss_release_buffer_ptr(&minor_status, &output_token);

return NS_OK;
Expand Down
4 changes: 1 addition & 3 deletions extensions/auth/nsAuthSSPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -542,10 +542,8 @@ nsAuthSSPI::Unwrap(const void *inToken,
*outToken = ib[1].pvBuffer;
}
else {
*outToken = nsMemory::Clone(ib[1].pvBuffer, ib[1].cbBuffer);
*outToken = moz_xmemdup(ib[1].pvBuffer, ib[1].cbBuffer);
free(ib[0].pvBuffer);
if (!*outToken)
return NS_ERROR_OUT_OF_MEMORY;
}
*outTokenLen = ib[1].cbBuffer;
}
Expand Down
9 changes: 2 additions & 7 deletions extensions/auth/nsAuthSambaNTLM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,9 +235,7 @@ nsAuthSambaNTLM::GetNextToken(const void *inToken,
{
if (!inToken) {
/* someone wants our initial message */
*outToken = nsMemory::Clone(mInitialMessage, mInitialMessageLen);
if (!*outToken)
return NS_ERROR_OUT_OF_MEMORY;
*outToken = moz_xmemdup(mInitialMessage, mInitialMessageLen);
*outTokenLen = mInitialMessageLen;
return NS_OK;
}
Expand Down Expand Up @@ -266,11 +264,8 @@ nsAuthSambaNTLM::GetNextToken(const void *inToken,
uint8_t* buf = ExtractMessage(line, outTokenLen);
if (!buf)
return NS_ERROR_FAILURE;
*outToken = nsMemory::Clone(buf, *outTokenLen);
*outToken = moz_xmemdup(buf, *outTokenLen);
PR_Free(buf);
if (!*outToken) {
return NS_ERROR_OUT_OF_MEMORY;
}

// We're done. Close our file descriptors now and reap the helper
// process.
Expand Down
4 changes: 2 additions & 2 deletions js/xpconnect/src/XPCJSID.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -306,8 +306,8 @@ NS_IMETHODIMP nsJSIID::GetNumber(char * *aNumber)
const nsIID* id;
mInfo->GetIIDShared(&id);
id->ToProvidedString(str);
*aNumber = (char*) nsMemory::Clone(str, NSID_LENGTH);
return *aNumber ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
*aNumber = (char*) moz_xmemdup(str, NSID_LENGTH);
return NS_OK;
}

NS_IMETHODIMP_(const nsID*) nsJSIID::GetID()
Expand Down
8 changes: 8 additions & 0 deletions memory/mozalloc/mozalloc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,14 @@ moz_xstrndup(const char* str, size_t strsize)
}
#endif // if defined(HAVE_STRNDUP)

void*
moz_xmemdup(const void* ptr, size_t size)
{
void* newPtr = moz_xmalloc(size);
memcpy(newPtr, ptr, size);
return newPtr;
}

#ifndef HAVE_MEMALIGN
// We always have a definition of memalign, but system headers don't
// necessarily come with a declaration.
Expand Down
19 changes: 11 additions & 8 deletions memory/mozalloc/mozalloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,17 @@ MFBT_API void* moz_xrealloc(void* ptr, size_t size)
MFBT_API char* moz_xstrdup(const char* str)
MOZ_ALLOCATOR;

#if defined(HAVE_STRNDUP)
MFBT_API char* moz_xstrndup(const char* str, size_t strsize)
MOZ_ALLOCATOR;
#endif /* if defined(HAVE_STRNDUP) */

MFBT_API void* moz_xmemdup(const void* ptr, size_t size)
MOZ_ALLOCATOR;

MFBT_API void* moz_xmemalign(size_t boundary, size_t size)
MOZ_ALLOCATOR;

MFBT_API size_t moz_malloc_usable_size(void *ptr);

MFBT_API size_t moz_malloc_size_of(const void *ptr);
Expand All @@ -82,14 +93,6 @@ MFBT_API size_t moz_malloc_size_of(const void *ptr);
*/
MFBT_API size_t moz_malloc_enclosing_size_of(const void *ptr);

#if defined(HAVE_STRNDUP)
MFBT_API char* moz_xstrndup(const char* str, size_t strsize)
MOZ_ALLOCATOR;
#endif /* if defined(HAVE_STRNDUP) */

MFBT_API void* moz_xmemalign(size_t boundary, size_t size)
MOZ_ALLOCATOR;

MOZ_END_EXTERN_C


Expand Down
10 changes: 2 additions & 8 deletions modules/libpref/Preferences.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2943,14 +2943,8 @@ nsPrefBranch::GetChildList(const char* aStartingAt,
// back to us because if they do we are going to add mPrefRoot again.
const nsCString& element = prefArray[dwIndex];
outArray[dwIndex] =
(char*)nsMemory::Clone(element.get() + mPrefRoot.Length(),
element.Length() - mPrefRoot.Length() + 1);

if (!outArray[dwIndex]) {
// We ran out of memory... this is annoying.
NS_FREE_XPCOM_ALLOCATED_POINTER_ARRAY(dwIndex, outArray);
return NS_ERROR_OUT_OF_MEMORY;
}
(char*) moz_xmemdup(element.get() + mPrefRoot.Length(),
element.Length() - mPrefRoot.Length() + 1);
}
*aChildArray = outArray;
}
Expand Down
21 changes: 7 additions & 14 deletions netwerk/mime/nsMIMEHeaderParamImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -407,8 +407,7 @@ nsMIMEHeaderParamImpl::DoParameterInternal(const char *aHeaderValue,
if (str == start)
return NS_ERROR_FIRST_HEADER_FIELD_COMPONENT_EMPTY;

*aResult = (char *) nsMemory::Clone(start, (str - start) + 1);
NS_ENSURE_TRUE(*aResult, NS_ERROR_OUT_OF_MEMORY);
*aResult = (char*) moz_xmemdup(start, (str - start) + 1);
(*aResult)[str - start] = '\0'; // null-terminate
return NS_OK;
}
Expand Down Expand Up @@ -630,10 +629,8 @@ nsMIMEHeaderParamImpl::DoParameterInternal(const char *aHeaderValue,
}

// allocate buffer for the raw value
char *tmpResult = (char *) nsMemory::Clone(rawValStart, rawValLength + 1);
if (!tmpResult) {
goto increment_str;
}
char* tmpResult =
(char*) moz_xmemdup(rawValStart, rawValLength + 1);
*(tmpResult + rawValLength) = 0;

nsUnescape(tmpResult);
Expand Down Expand Up @@ -724,17 +721,13 @@ nsMIMEHeaderParamImpl::DoParameterInternal(const char *aHeaderValue,
// then return charset and lang as well
if (aLang && !lang.IsEmpty()) {
uint32_t len = lang.Length();
*aLang = (char *) nsMemory::Clone(lang.BeginReading(), len + 1);
if (*aLang) {
*(*aLang + len) = 0;
}
*aLang = (char*) moz_xmemdup(lang.BeginReading(), len + 1);
*(*aLang + len) = 0;
}
if (aCharset && !charset.IsEmpty()) {
uint32_t len = charset.Length();
*aCharset = (char *) nsMemory::Clone(charset.BeginReading(), len + 1);
if (*aCharset) {
*(*aCharset + len) = 0;
}
*aCharset = (char*) moz_xmemdup(charset.BeginReading(), len + 1);
*(*aCharset + len) = 0;
}
}

Expand Down
3 changes: 1 addition & 2 deletions storage/Variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -303,8 +303,7 @@ struct variant_blob_traits<uint8_t[], false>
}

// Otherwise, we copy the array.
*_result = nsMemory::Clone(aData.Elements(), aData.Length() * sizeof(uint8_t));
NS_ENSURE_TRUE(*_result, NS_ERROR_OUT_OF_MEMORY);
*_result = moz_xmemdup(aData.Elements(), aData.Length() * sizeof(uint8_t));

// Set type and size
*_type = nsIDataType::VTYPE_UINT8;
Expand Down
4 changes: 1 addition & 3 deletions storage/mozStorageArgValueArray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,7 @@ ArgValueArray::GetBlob(uint32_t aIndex,
ENSURE_INDEX_VALUE(aIndex, mArgc);

int size = ::sqlite3_value_bytes(mArgv[aIndex]);
void *blob = nsMemory::Clone(::sqlite3_value_blob(mArgv[aIndex]), size);
NS_ENSURE_TRUE(blob, NS_ERROR_OUT_OF_MEMORY);

void* blob = moz_xmemdup(::sqlite3_value_blob(mArgv[aIndex]), size);
*_blob = static_cast<uint8_t *>(blob);
*_size = size;
return NS_OK;
Expand Down
3 changes: 1 addition & 2 deletions storage/mozStorageStatement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -783,8 +783,7 @@ Statement::GetBlob(uint32_t aIndex,
int size = ::sqlite3_column_bytes(mDBStatement, aIndex);
void *blob = nullptr;
if (size) {
blob = nsMemory::Clone(::sqlite3_column_blob(mDBStatement, aIndex), size);
NS_ENSURE_TRUE(blob, NS_ERROR_OUT_OF_MEMORY);
blob = moz_xmemdup(::sqlite3_column_blob(mDBStatement, aIndex), size);
}

*_blob = static_cast<uint8_t *>(blob);
Expand Down
10 changes: 2 additions & 8 deletions widget/GfxInfoBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1105,15 +1105,9 @@ NS_IMETHODIMP GfxInfoBase::GetFailures(uint32_t* failureCount,
LoggingRecord::const_iterator it;
uint32_t i=0;
for(it = loggedStrings.begin() ; it != loggedStrings.end(); ++it, i++) {
(*failures)[i] = (char*)nsMemory::Clone(Get<1>(*it).c_str(), Get<1>(*it).size() + 1);
(*failures)[i] =
(char*) moz_xmemdup(Get<1>(*it).c_str(), Get<1>(*it).size() + 1);
if (indices) (*indices)[i] = Get<0>(*it);

if (!(*failures)[i]) {
/* <sarcasm> I'm too afraid to use an inline function... </sarcasm> */
NS_FREE_XPCOM_ALLOCATED_POINTER_ARRAY(i, (*failures));
*failureCount = i;
return NS_ERROR_OUT_OF_MEMORY;
}
}
}

Expand Down
8 changes: 0 additions & 8 deletions xpcom/base/nsMemory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,6 @@ nsMemory::HeapMinimize(bool aImmediate)
return mem->HeapMinimize(aImmediate);
}

void*
nsMemory::Clone(const void* aPtr, size_t aSize)
{
void* newPtr = moz_xmalloc(aSize);
memcpy(newPtr, aPtr, aSize);
return newPtr;
}

nsIMemory*
nsMemory::GetGlobalMemoryService()
{
Expand Down
1 change: 0 additions & 1 deletion xpcom/base/nsMemory.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ class nsMemory
{
public:
static nsresult HeapMinimize(bool aImmediate);
static void* Clone(const void* aPtr, size_t aSize);
static nsIMemory* GetGlobalMemoryService(); // AddRefs
};

Expand Down
11 changes: 3 additions & 8 deletions xpcom/ds/nsVariant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1493,10 +1493,7 @@ nsDiscriminatedUnion::SetFromStringWithSize(uint32_t aSize,
if (!aValue) {
return NS_ERROR_NULL_POINTER;
}
if (!(u.str.mStringValue =
(char*)nsMemory::Clone(aValue, (aSize + 1) * sizeof(char)))) {
return NS_ERROR_OUT_OF_MEMORY;
}
u.str.mStringValue = (char*) moz_xmemdup(aValue, (aSize + 1) * sizeof(char));
u.str.mStringLength = aSize;
DATA_SETTER_EPILOGUE(VTYPE_STRING_SIZE_IS);
return NS_OK;
Expand All @@ -1509,10 +1506,8 @@ nsDiscriminatedUnion::SetFromWStringWithSize(uint32_t aSize,
if (!aValue) {
return NS_ERROR_NULL_POINTER;
}
if (!(u.wstr.mWStringValue =
(char16_t*)nsMemory::Clone(aValue, (aSize + 1) * sizeof(char16_t)))) {
return NS_ERROR_OUT_OF_MEMORY;
}
u.wstr.mWStringValue =
(char16_t*) moz_xmemdup(aValue, (aSize + 1) * sizeof(char16_t));
u.wstr.mWStringLength = aSize;
DATA_SETTER_EPILOGUE(VTYPE_WSTRING_SIZE_IS);
return NS_OK;
Expand Down

0 comments on commit eafd8e4

Please sign in to comment.