diff --git a/intl/strres/src/nsStringBundle.cpp b/intl/strres/src/nsStringBundle.cpp index 5a7a50f7be7..53cb270fff1 100644 --- a/intl/strres/src/nsStringBundle.cpp +++ b/intl/strres/src/nsStringBundle.cpp @@ -24,6 +24,8 @@ #include "nsIComponentManager.h" #include "nsIMemory.h" #include "nsIObserverService.h" +#include "pratom.h" +#include "prmem.h" #include "nsCOMArray.h" #include "nsTextFormatter.h" #include "nsIErrorService.h" diff --git a/intl/uconv/src/nsConverterInputStream.cpp b/intl/uconv/src/nsConverterInputStream.cpp index 578d8c167b3..3c0e3cc0054 100644 --- a/intl/uconv/src/nsConverterInputStream.cpp +++ b/intl/uconv/src/nsConverterInputStream.cpp @@ -7,7 +7,6 @@ #include "nsIInputStream.h" #include "nsICharsetConverterManager.h" #include "nsIServiceManager.h" -#include "nsReadLine.h" #define CONVERTER_BUFFER_SIZE 8192 @@ -54,7 +53,7 @@ NS_IMETHODIMP nsConverterInputStream::Close() { nsresult rv = mInput ? mInput->Close() : NS_OK; - mLineBuffer = nullptr; + PR_FREEIF(mLineBuffer); mInput = nullptr; mConverter = nullptr; mByteData = nullptr; @@ -235,7 +234,8 @@ NS_IMETHODIMP nsConverterInputStream::ReadLine(nsAString& aLine, bool* aResult) { if (!mLineBuffer) { - mLineBuffer = new nsLineBuffer; + nsresult rv = NS_InitLineBuffer(&mLineBuffer); + if (NS_FAILED(rv)) return rv; } - return NS_ReadLine(this, mLineBuffer.get(), aLine, aResult); + return NS_ReadLine(this, mLineBuffer, aLine, aResult); } diff --git a/intl/uconv/src/nsConverterInputStream.h b/intl/uconv/src/nsConverterInputStream.h index 4e89f8d593a..f299ae2bfd3 100644 --- a/intl/uconv/src/nsConverterInputStream.h +++ b/intl/uconv/src/nsConverterInputStream.h @@ -7,15 +7,13 @@ #include "nsIConverterInputStream.h" #include "nsIUnicharLineInputStream.h" #include "nsString.h" +#include "nsReadLine.h" -#include "nsAutoPtr.h" #include "nsCOMPtr.h" #include "nsIUnicodeDecoder.h" #include "nsIByteBuffer.h" #include "nsIUnicharBuffer.h" -template class nsLineBuffer; - #define NS_CONVERTERINPUTSTREAM_CONTRACTID "@mozilla.org/intl/converter-input-stream;1" // {2BC2AD62-AD5D-4b7b-A9DB-F74AE203C527} @@ -60,5 +58,5 @@ class nsConverterInputStream : public nsIConverterInputStream, uint32_t mUnicharDataLength; PRUnichar mReplacementChar; - nsAutoPtr > mLineBuffer; + nsLineBuffer* mLineBuffer; }; diff --git a/netwerk/base/public/nsReadLine.h b/netwerk/base/public/nsReadLine.h index 1c42cc72a17..561017500b6 100644 --- a/netwerk/base/public/nsReadLine.h +++ b/netwerk/base/public/nsReadLine.h @@ -7,6 +7,7 @@ #ifndef nsReadLine_h__ #define nsReadLine_h__ +#include "prmem.h" #include "nsIInputStream.h" #include "mozilla/Likely.h" @@ -41,13 +42,43 @@ template class nsLineBuffer { public: - nsLineBuffer() : start(buf), end(buf) { } - CharT buf[kLineBufferSize+1]; CharT* start; CharT* end; }; +/** + * Initialize a line buffer for use with NS_ReadLine. + * + * @param aBufferPtr + * Pointer to pointer to a line buffer. Upon successful return, + * *aBufferPtr will contain a valid pointer to a line buffer, for use + * with NS_ReadLine. Use PR_Free when the buffer is no longer needed. + * + * @retval NS_OK Success. + * @retval NS_ERROR_OUT_OF_MEMORY Not enough memory to allocate the line buffer. + * + * @par Example: + * @code + * nsLineBuffer* lb; + * rv = NS_InitLineBuffer(&lb); + * if (NS_SUCCEEDED(rv)) { + * // do stuff... + * PR_Free(lb); + * } + * @endcode + */ +template +nsresult +NS_InitLineBuffer (nsLineBuffer ** aBufferPtr) { + *aBufferPtr = PR_NEW(nsLineBuffer); + if (!(*aBufferPtr)) + return NS_ERROR_OUT_OF_MEMORY; + + (*aBufferPtr)->start = (*aBufferPtr)->end = (*aBufferPtr)->buf; + return NS_OK; +} + /** * Read a line from an input stream. Lines are separated by '\r' (0x0D) or '\n' * (0x0A), or "\r\n" or "\n\r". @@ -55,7 +86,8 @@ class nsLineBuffer { * @param aStream * The stream to read from * @param aBuffer - * The line buffer to use. A single line buffer must not be used with + * The line buffer to use. Must have been inited with + * NS_InitLineBuffer before. A single line buffer must not be used with * different input streams. * @param aLine [out] * The string where the line will be stored. diff --git a/netwerk/base/src/nsFileStreams.cpp b/netwerk/base/src/nsFileStreams.cpp index 2420ffca400..cc7f7b1c5aa 100644 --- a/netwerk/base/src/nsFileStreams.cpp +++ b/netwerk/base/src/nsFileStreams.cpp @@ -418,7 +418,7 @@ nsFileInputStream::Close() } // null out mLineBuffer in case Close() is called again after failing - mLineBuffer = nullptr; + PR_FREEIF(mLineBuffer); nsresult rv = nsFileStreamBase::Close(); if (NS_FAILED(rv)) return rv; if (mFile && (mBehaviorFlags & DELETE_ON_CLOSE)) { @@ -453,9 +453,10 @@ nsFileInputStream::ReadLine(nsACString& aLine, bool* aResult) NS_ENSURE_SUCCESS(rv, rv); if (!mLineBuffer) { - mLineBuffer = new nsLineBuffer; + nsresult rv = NS_InitLineBuffer(&mLineBuffer); + if (NS_FAILED(rv)) return rv; } - return NS_ReadLine(this, mLineBuffer.get(), aLine, aResult); + return NS_ReadLine(this, mLineBuffer, aLine, aResult); } NS_IMETHODIMP @@ -464,7 +465,7 @@ nsFileInputStream::Seek(int32_t aWhence, int64_t aOffset) nsresult rv = DoPendingOpen(); NS_ENSURE_SUCCESS(rv, rv); - mLineBuffer = nullptr; + PR_FREEIF(mLineBuffer); // this invalidates the line buffer if (!mFD) { if (mBehaviorFlags & REOPEN_ON_REWIND) { rv = Open(mFile, mIOFlags, mPerm); diff --git a/netwerk/base/src/nsFileStreams.h b/netwerk/base/src/nsFileStreams.h index 95d35fb1aef..112882dba5d 100644 --- a/netwerk/base/src/nsFileStreams.h +++ b/netwerk/base/src/nsFileStreams.h @@ -7,7 +7,6 @@ #define nsFileStreams_h__ #include "nsAlgorithm.h" -#include "nsAutoPtr.h" #include "nsIFileStreams.h" #include "nsIFile.h" #include "nsIInputStream.h" @@ -142,7 +141,7 @@ public: Create(nsISupports *aOuter, REFNSIID aIID, void **aResult); protected: - nsAutoPtr > mLineBuffer; + nsLineBuffer *mLineBuffer; /** * The file being opened. diff --git a/netwerk/base/src/nsSerializationHelper.cpp b/netwerk/base/src/nsSerializationHelper.cpp index 146ee99188b..82ff90bf420 100644 --- a/netwerk/base/src/nsSerializationHelper.cpp +++ b/netwerk/base/src/nsSerializationHelper.cpp @@ -4,8 +4,9 @@ #include "nsSerializationHelper.h" +#include "plbase64.h" +#include "prmem.h" -#include "mozilla/Base64.h" #include "nsISerializable.h" #include "nsIObjectOutputStream.h" #include "nsIObjectInputStream.h" @@ -15,8 +16,6 @@ #include "nsComponentManagerUtils.h" #include "nsStringStream.h" -using namespace mozilla; - nsresult NS_SerializeToString(nsISerializable* obj, nsCSubstring& str) { @@ -39,12 +38,30 @@ NS_SerializeToString(nsISerializable* obj, nsCSubstring& str) nsresult NS_DeserializeObject(const nsCSubstring& str, nsISupports** obj) { - nsCString decodedData; - nsresult rv = Base64Decode(str, decodedData); - NS_ENSURE_SUCCESS(rv, rv); + // Base64 maps 3 binary bytes -> 4 ASCII bytes. If the original byte array + // does not have length 0 mod 3, the input is padded with zeros and the + // output is padded with a corresponding number of trailing '=' (which are + // then sometimes dropped). To compute the correct length of the original + // byte array, we have to subtract the number of trailing '=' and then + // multiply by 3 and then divide by 4 (making sure this is an integer + // division). + uint32_t size = str.Length(); + if (size > 0 && str[size-1] == '=') { + if (size > 1 && str[size-2] == '=') { + size -= 2; + } else { + size -= 1; + } + } + size = (size * 3) / 4; + char* buf = PL_Base64Decode(str.BeginReading(), str.Length(), nullptr); + if (!buf) + return NS_ERROR_OUT_OF_MEMORY; nsCOMPtr stream; - rv = NS_NewCStringInputStream(getter_AddRefs(stream), decodedData); + nsresult rv = NS_NewCStringInputStream(getter_AddRefs(stream), + Substring(buf, size)); + PR_Free(buf); NS_ENSURE_SUCCESS(rv, rv); nsCOMPtr objstream = diff --git a/netwerk/base/src/nsSocketTransport2.cpp b/netwerk/base/src/nsSocketTransport2.cpp index a348eb59b11..8e0b45efa7b 100644 --- a/netwerk/base/src/nsSocketTransport2.cpp +++ b/netwerk/base/src/nsSocketTransport2.cpp @@ -19,6 +19,7 @@ #include "nsAutoPtr.h" #include "nsCOMPtr.h" #include "netCore.h" +#include "prmem.h" #include "plstr.h" #include "prnetdb.h" #include "prerror.h" diff --git a/netwerk/cache/nsCacheMetaData.cpp b/netwerk/cache/nsCacheMetaData.cpp index d7dc37a32e7..929117e8473 100644 --- a/netwerk/cache/nsCacheMetaData.cpp +++ b/netwerk/cache/nsCacheMetaData.cpp @@ -6,6 +6,7 @@ #include "nsCacheMetaData.h" #include "nsICacheEntryDescriptor.h" +#include "prmem.h" const char * nsCacheMetaData::GetElement(const char * key) @@ -151,7 +152,7 @@ nsresult nsCacheMetaData::EnsureBuffer(uint32_t bufSize) { if (mBufferSize < bufSize) { - char * buf = (char *)moz_realloc(mBuffer, bufSize); + char * buf = (char *)PR_REALLOC(mBuffer, bufSize); if (!buf) { return NS_ERROR_OUT_OF_MEMORY; } diff --git a/netwerk/cache/nsCacheMetaData.h b/netwerk/cache/nsCacheMetaData.h index e0879c1473f..6bd7b995d2e 100644 --- a/netwerk/cache/nsCacheMetaData.h +++ b/netwerk/cache/nsCacheMetaData.h @@ -18,8 +18,7 @@ public: ~nsCacheMetaData() { mBufferSize = mMetaSize = 0; - moz_free(mBuffer); - mBuffer = nullptr; + PR_FREEIF(mBuffer); } const char * GetElement(const char * key); diff --git a/netwerk/protocol/data/nsDataChannel.cpp b/netwerk/protocol/data/nsDataChannel.cpp index 9a68c7f27af..2699b787fe4 100644 --- a/netwerk/protocol/data/nsDataChannel.cpp +++ b/netwerk/protocol/data/nsDataChannel.cpp @@ -5,10 +5,8 @@ // data implementation -#include "nsDataChannel.h" - -#include "mozilla/Base64.h" #include "nsIOService.h" +#include "nsDataChannel.h" #include "nsDataHandler.h" #include "nsNetUtil.h" #include "nsIPipe.h" @@ -16,8 +14,9 @@ #include "nsIOutputStream.h" #include "nsReadableUtils.h" #include "nsEscape.h" - -using namespace mozilla; +#include "plbase64.h" +#include "plstr.h" +#include "prmem.h" nsresult nsDataChannel::OpenContentStream(bool async, nsIInputStream **result, @@ -71,10 +70,17 @@ nsDataChannel::OpenContentStream(bool async, nsIInputStream **result, } resultLen = ((resultLen * 3) / 4); - nsAutoCString decodedData; - rv = Base64Decode(dataBuffer, decodedData); - NS_ENSURE_SUCCESS(rv, rv); - rv = bufOutStream->Write(decodedData.get(), resultLen, &contentLen); + // XXX PL_Base64Decode will return a null pointer for decoding + // errors. Since those are more likely than out-of-memory, + // should we return NS_ERROR_MALFORMED_URI instead? + char * decodedData = PL_Base64Decode(dataBuffer.get(), dataLen, nullptr); + if (!decodedData) { + return NS_ERROR_OUT_OF_MEMORY; + } + + rv = bufOutStream->Write(decodedData, resultLen, &contentLen); + + PR_Free(decodedData); } else { rv = bufOutStream->Write(dataBuffer.get(), dataBuffer.Length(), &contentLen); }