diff --git a/lib/Common/Codex/Utf8Helper.h b/lib/Common/Codex/Utf8Helper.h index 26bcbb0b525..4468bc85bc9 100644 --- a/lib/Common/Codex/Utf8Helper.h +++ b/lib/Common/Codex/Utf8Helper.h @@ -87,7 +87,7 @@ namespace utf8 } inline HRESULT NarrowStringToWideNoAlloc(_In_ LPCSTR sourceString, size_t sourceCount, - __out_ecount(destBufferCount) LPWSTR destString, size_t destBufferCount, _Out_ size_t* destCount) + __out_ecount(destBufferCount) LPWSTR destString, size_t destBufferCount, _Out_ charcount_t* destCount) { size_t sourceStart = 0; size_t cbSourceString = sourceCount; @@ -123,7 +123,7 @@ namespace utf8 if (sourceStart == sourceCount) { - *destCount = sourceCount; + *destCount = static_cast(sourceCount); destString[sourceCount] = WCHAR(0); } else @@ -160,7 +160,7 @@ namespace utf8 /// template HRESULT NarrowStringToWide(_In_ AllocatorFunction allocator,_In_ LPCSTR sourceString, - size_t sourceCount, _Out_ LPWSTR* destStringPtr, _Out_ size_t* destCount, size_t* allocateCount = nullptr) + size_t sourceCount, _Out_ LPWSTR* destStringPtr, _Out_ charcount_t* destCount, size_t* allocateCount = nullptr) { size_t cbDestString = (sourceCount + 1) * sizeof(WCHAR); if (cbDestString < sourceCount) // overflow ? @@ -184,7 +184,7 @@ namespace utf8 } template - HRESULT NarrowStringToWide(_In_ LPCSTR sourceString, size_t sourceCount, _Out_ LPWSTR* destStringPtr, _Out_ size_t* destCount, size_t* allocateCount = nullptr) + HRESULT NarrowStringToWide(_In_ LPCSTR sourceString, size_t sourceCount, _Out_ LPWSTR* destStringPtr, _Out_ charcount_t* destCount, size_t* allocateCount = nullptr) { return NarrowStringToWide(Allocator::allocate, sourceString, sourceCount, destStringPtr, destCount, allocateCount); } @@ -205,30 +205,30 @@ namespace utf8 inline HRESULT NarrowStringToWideDynamic(_In_ LPCSTR sourceString, _Out_ LPWSTR* destStringPtr) { - size_t unused; + charcount_t unused; return NarrowStringToWide( sourceString, strlen(sourceString), destStringPtr, &unused); } - inline HRESULT NarrowStringToWideDynamicGetLength(_In_ LPCSTR sourceString, _Out_ LPWSTR* destStringPtr, _Out_ size_t* destLength) + inline HRESULT NarrowStringToWideDynamicGetLength(_In_ LPCSTR sourceString, _Out_ LPWSTR* destStringPtr, _Out_ charcount_t* destLength) { return NarrowStringToWide( sourceString, strlen(sourceString), destStringPtr, destLength); } - template + template class NarrowWideStringConverter { public: static size_t Length(const SrcType& src); static HRESULT Convert( - SrcType src, size_t srcCount, DstType* dst, size_t* dstCount, size_t* allocateCount = nullptr); + SrcType src, size_t srcCount, DstType* dst, CountType* dstCount, size_t* allocateCount = nullptr); static HRESULT ConvertNoAlloc( - SrcType src, size_t srcCount, DstType dst, size_t dstCount, size_t* written); + SrcType src, size_t srcCount, DstType dst, CountType dstCount, CountType* written); }; template - class NarrowWideStringConverter + class NarrowWideStringConverter { public: // Note: Typically caller should pass in Utf8 string length. Following @@ -240,7 +240,7 @@ namespace utf8 static HRESULT Convert( LPCSTR sourceString, size_t sourceCount, - LPWSTR* destStringPtr, size_t* destCount, size_t* allocateCount = nullptr) + LPWSTR* destStringPtr, charcount_t * destCount, size_t* allocateCount = nullptr) { return NarrowStringToWide( sourceString, sourceCount, destStringPtr, destCount, allocateCount); @@ -248,7 +248,7 @@ namespace utf8 static HRESULT ConvertNoAlloc( LPCSTR sourceString, size_t sourceCount, - LPWSTR destStringPtr, size_t destCount, size_t* written) + LPWSTR destStringPtr, charcount_t destCount, charcount_t* written) { return NarrowStringToWideNoAlloc( sourceString, sourceCount, destStringPtr, destCount, written); @@ -256,7 +256,7 @@ namespace utf8 }; template - class NarrowWideStringConverter + class NarrowWideStringConverter { public: // Note: Typically caller should pass in WCHAR string length. Following @@ -283,14 +283,14 @@ namespace utf8 } }; - template + template class NarrowWideConverter { - typedef NarrowWideStringConverter + typedef NarrowWideStringConverter StringConverter; private: DstType dst; - size_t dstCount; + CountType dstCount; size_t allocateCount; bool freeDst; @@ -347,6 +347,6 @@ namespace utf8 } }; - typedef NarrowWideConverter NarrowToWide; - typedef NarrowWideConverter WideToNarrow; + typedef NarrowWideConverter NarrowToWide; + typedef NarrowWideConverter WideToNarrow; } diff --git a/lib/Jsrt/Jsrt.cpp b/lib/Jsrt/Jsrt.cpp index a92d733a709..feb6fa9e4fe 100644 --- a/lib/Jsrt/Jsrt.cpp +++ b/lib/Jsrt/Jsrt.cpp @@ -4662,7 +4662,7 @@ CHAKRA_API JsCreateString( length = strlen(content); } - if (length > static_cast(-1)) + if (length > MaxCharCount) { return JsErrorOutOfMemory; } diff --git a/lib/Runtime/Library/ConcatString.cpp b/lib/Runtime/Library/ConcatString.cpp index d666ee0e3fc..c4aee1683df 100644 --- a/lib/Runtime/Library/ConcatString.cpp +++ b/lib/Runtime/Library/ConcatString.cpp @@ -99,24 +99,24 @@ namespace Js } ScriptContext * scriptContext = library->GetScriptContext(); - size_t cbDestString = (charCount + 1) * sizeof(WCHAR); - if ((CharCount)cbDestString < charCount) // overflow + if (charCount > MaxCharCount) { Js::JavascriptError::ThrowOutOfMemoryError(scriptContext); } Recycler * recycler = library->GetRecycler(); - char16* destString = RecyclerNewArrayLeaf(recycler, WCHAR, cbDestString); + char16* destString = RecyclerNewArrayLeaf(recycler, WCHAR, charCount + 1); if (destString == nullptr) { Js::JavascriptError::ThrowOutOfMemoryError(scriptContext); } - HRESULT result = utf8::NarrowStringToWideNoAlloc(cString, charCount, destString, charCount + 1, &cbDestString); + charcount_t cchDestString = 0; + HRESULT result = utf8::NarrowStringToWideNoAlloc(cString, charCount, destString, charCount + 1, &cchDestString); if (result == S_OK) { - return (JavascriptString*) RecyclerNew(library->GetRecycler(), LiteralStringWithPropertyStringPtr, destString, (CharCount)cbDestString, library); + return (JavascriptString*) RecyclerNew(library->GetRecycler(), LiteralStringWithPropertyStringPtr, destString, cchDestString, library); } Js::JavascriptError::ThrowOutOfMemoryError(scriptContext); diff --git a/lib/Runtime/Library/WabtInterface.cpp b/lib/Runtime/Library/WabtInterface.cpp index c11514deeaf..438383def0a 100644 --- a/lib/Runtime/Library/WabtInterface.cpp +++ b/lib/Runtime/Library/WabtInterface.cpp @@ -18,11 +18,11 @@ struct Context ScriptContext* scriptContext; }; -char16* NarrowStringToWide(Context* ctx, const char* src, const size_t* srcSize = nullptr, size_t* dstSize = nullptr) +char16* NarrowStringToWide(Context* ctx, const char* src, const size_t* srcSize = nullptr, charcount_t* dstSize = nullptr) { auto allocator = [&ctx](size_t size) {return (char16*)AnewArray(ctx->allocator, char16, size); }; char16* dst = nullptr; - size_t size; + charcount_t size; HRESULT hr = utf8::NarrowStringToWide(allocator, src, srcSize ? *srcSize : strlen(src), &dst, &size); if (hr != S_OK) { @@ -86,11 +86,11 @@ Js::Var Int64ToVar(int64 value, void* user_data) Js::Var StringToVar(const char* src, uint length, void* user_data) { Context* ctx = (Context*)user_data; - size_t bufSize = 0; + charcount_t bufSize = 0; size_t slength = (size_t)length; char16* buf = NarrowStringToWide(ctx, src, &slength, &bufSize); Assert(bufSize < UINT32_MAX); - return JavascriptString::NewCopyBuffer(buf, (charcount_t)bufSize, ctx->scriptContext); + return JavascriptString::NewCopyBuffer(buf, bufSize, ctx->scriptContext); } Js::Var CreateBuffer(const uint8* buf, uint size, void* user_data)