From c15973ce179467f68d0c93912c6b05ab820b6e37 Mon Sep 17 00:00:00 2001 From: "Yi Zhang (CLR)" Date: Fri, 2 Jun 2017 09:10:34 -0700 Subject: [PATCH] Fix a bug in LoadNativeStringResource to honor the contract properly when the buffer is not big enough (dotnet/coreclr#12051) * Return last error properly * adjust pcwchUsed if ERROR_INSUFFICIENT_BUFFER * return hr * ERROR_INSUFFICIENT_BUFFER is a win32 error.... Commit migrated from https://github.com/dotnet/coreclr/commit/3a6895ebcc29e0ad84bef61a216433e99fda5f6c --- src/coreclr/src/nativeresources/resourcestring.cpp | 12 ++++++++++++ src/coreclr/src/utilcode/ccomprc.cpp | 3 +-- src/coreclr/src/utilcode/sstring_com.cpp | 2 +- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/coreclr/src/nativeresources/resourcestring.cpp b/src/coreclr/src/nativeresources/resourcestring.cpp index 236a2a3..1c3f50a 100644 --- a/src/coreclr/src/nativeresources/resourcestring.cpp +++ b/src/coreclr/src/nativeresources/resourcestring.cpp @@ -38,6 +38,16 @@ int LoadNativeStringResource(const NativeStringResourceTable &nativeStringResour if (resourceEntry != NULL) { len = PAL_GetResourceString(NULL, resourceEntry->resourceString, szBuffer, iMax); + if (len == 0) + { + int hr = HRESULT_FROM_GetLastError(); + + // Tell the caller if the buffer isn't big enough + if (hr == HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER) && pcwchUsed) + *pcwchUsed = iMax; + + return hr; + } } else { @@ -47,6 +57,8 @@ int LoadNativeStringResource(const NativeStringResourceTable &nativeStringResour { // The only possible failure is that that string didn't fit the buffer. So the buffer contains // partial string terminated by '\0' + // We could return ERROR_INSUFFICIENT_BUFFER, but we'll error on the side of caution here and + // actually show something (given that this is likely a scenario involving a bug/deployment issue) len = iMax - 1; } } diff --git a/src/coreclr/src/utilcode/ccomprc.cpp b/src/coreclr/src/utilcode/ccomprc.cpp index ed902dc..bc649e7 100644 --- a/src/coreclr/src/utilcode/ccomprc.cpp +++ b/src/coreclr/src/utilcode/ccomprc.cpp @@ -852,9 +852,8 @@ HRESULT CCompRC::LoadString(ResourceCategory eCategory, LocaleID langId, UINT iR return hr; #else // !FEATURE_PAL - LoadNativeStringResource(NATIVE_STRING_RESOURCE_TABLE(NATIVE_STRING_RESOURCE_NAME), iResourceID, + return LoadNativeStringResource(NATIVE_STRING_RESOURCE_TABLE(NATIVE_STRING_RESOURCE_NAME), iResourceID, szBuffer, iMax, pcwchUsed); - return S_OK; #endif // !FEATURE_PAL } diff --git a/src/coreclr/src/utilcode/sstring_com.cpp b/src/coreclr/src/utilcode/sstring_com.cpp index 61cf4dd..ada4c9c 100644 --- a/src/coreclr/src/utilcode/sstring_com.cpp +++ b/src/coreclr/src/utilcode/sstring_com.cpp @@ -60,7 +60,7 @@ HRESULT SString::LoadResourceAndReturnHR(CCompRC* pResourceDLL, CCompRC::Resourc // In fatal error reporting scenarios, we may not have enough memory to // allocate a larger buffer. - hr = pResourceDLL->LoadString(eCategory, resourceID, GetRawUnicode(), GetRawCount()+1,&size); + hr = pResourceDLL->LoadString(eCategory, resourceID, GetRawUnicode(), GetRawCount()+1, &size); if (hr != HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER)) { if (FAILED(hr)) -- 2.7.4