Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change SString::GetUTF8NoConvert to GetUTF8 that converts the SString #71101

Merged
merged 8 commits into from
Jun 23, 2022
2 changes: 1 addition & 1 deletion src/coreclr/debug/daccess/daccess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2501,7 +2501,7 @@ namespace serialization { namespace bin {
return ErrOverflow;
}

memcpy_s(dest, destSize, s.GetUTF8NoConvert(), cnt);
memcpy_s(dest, destSize, s.GetUTF8(), cnt);

return cnt;
}
Expand Down
6 changes: 4 additions & 2 deletions src/coreclr/inc/sstring.h
Original file line number Diff line number Diff line change
Expand Up @@ -553,8 +553,9 @@ class EMPTY_BASES_DECL SString : private SBuffer
const UTF8 *GetUTF8(AbstractScratchBuffer &scratch, COUNT_T *pcbUtf8) const;
const ANSI *GetANSI(AbstractScratchBuffer &scratch) const;

// Used when the representation is known, throws if the representation doesn't match
const UTF8 *GetUTF8NoConvert() const;
// You can always get a UTF8 string. This will force a conversion
// if necessary.
const UTF8 *GetUTF8() const;

// Converts/copies into the given output string
void ConvertToUnicode(SString &dest) const;
Expand Down Expand Up @@ -779,6 +780,7 @@ class EMPTY_BASES_DECL SString : private SBuffer
void ConvertASCIIToUnicode(SString &dest) const;
void ConvertToUnicode() const;
void ConvertToUnicode(const CIterator &i) const;
void ConvertToUTF8() const;

const SString &GetCompatibleString(const SString &s, SString &scratch) const;
const SString &GetCompatibleString(const SString &s, SString &scratch, const CIterator &i) const;
Expand Down
19 changes: 19 additions & 0 deletions src/coreclr/inc/sstring.inl
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,25 @@ inline const WCHAR *SString::GetUnicode() const
SS_RETURN GetRawUnicode();
}

// Get a const pointer to the internal buffer as a UTF8 string.
inline const UTF8 *SString::GetUTF8() const
{
SS_CONTRACT(const UTF8 *)
{
GC_NOTRIGGER;
PRECONDITION(CheckPointer(this));
SS_POSTCONDITION(CheckPointer(RETVAL));
if (IsRepresentation(REPRESENTATION_UTF8)) NOTHROW; else THROWS;
GC_NOTRIGGER;
SUPPORTS_DAC;
}
SS_CONTRACT_END;

ConvertToUTF8();

SS_RETURN GetRawUTF8();
}

// Normalize the string to unicode. This will make many operations nonfailing.
inline void SString::Normalize() const
{
Expand Down
4 changes: 2 additions & 2 deletions src/coreclr/utilcode/clrconfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,10 @@ namespace
SString valueAsUTF8;
jkoritzinsky marked this conversation as resolved.
Show resolved Hide resolved
temp.ConvertToUTF8(valueAsUTF8);

CLRConfigNoCache nonCache = CLRConfigNoCache::Get(nameAsUTF8.GetUTF8NoConvert(), noPrefix);
CLRConfigNoCache nonCache = CLRConfigNoCache::Get(nameAsUTF8.GetUTF8(), noPrefix);
LPCSTR valueNoCache = nonCache.AsString();

_ASSERTE(SString::_stricmp(valueNoCache, valueAsUTF8.GetUTF8NoConvert()) == 0);
_ASSERTE(SString::_stricmp(valueNoCache, valueAsUTF8.GetUTF8()) == 0);
#endif // defined(DEBUG) && !defined(SELF_NO_HOST)
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/coreclr/utilcode/debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ bool _DbgBreakCheck(
" Image: %s\n\n",
GetCurrentProcessId(), GetCurrentProcessId(),
GetCurrentThreadId(), GetCurrentThreadId(),
szExpr, szFile, iLine, modulePath.GetUTF8NoConvert());
szExpr, szFile, iLine, modulePath.GetUTF8());

formattedMessages = TRUE;
}
Expand Down Expand Up @@ -680,11 +680,11 @@ void DECLSPEC_NORETURN __FreeBuildAssertFail(const char *szFile, int iLine, cons
" File: %s, Line: %d Image:\n%s\n",
GetCurrentProcessId(), GetCurrentProcessId(),
GetCurrentThreadId(), GetCurrentThreadId(),
szExpr, szFile, iLine, modulePath.GetUTF8NoConvert());
OutputDebugStringUtf8(buffer.GetUTF8NoConvert());
szExpr, szFile, iLine, modulePath.GetUTF8());
OutputDebugStringUtf8(buffer.GetUTF8());

// Write out the error to the console
printf(buffer.GetUTF8NoConvert());
printf(buffer.GetUTF8());

// Log to the stress log. Note that we can't include the szExpr b/c that
// may not be a string literal (particularly for formatt-able asserts).
Expand Down
53 changes: 33 additions & 20 deletions src/coreclr/utilcode/sstring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,39 @@ void SString::ConvertToUnicode(const CIterator &i) const
RETURN;
}

//-----------------------------------------------------------------------------
// Convert the internal representation for this String to UTF8.
//-----------------------------------------------------------------------------
void SString::ConvertToUTF8() const
{
CONTRACT_VOID
{
POSTCONDITION(IsRepresentation(REPRESENTATION_UTF8));
if (IsRepresentation(REPRESENTATION_UTF8)) NOTHROW; else THROWS;
GC_NOTRIGGER;
SUPPORTS_DAC_HOST_ONLY;
}
CONTRACT_END;

if (!IsRepresentation(REPRESENTATION_UTF8))
{
if (IsRepresentation(REPRESENTATION_ASCII))
{
// ASCII is a subset of UTF8, so we can just set the representation.
(const_cast<SString*>(this))->SetRepresentation(REPRESENTATION_UTF8);
}
else
{
StackSString s;
ConvertToUTF8(s);
PREFIX_ASSUME(!s.IsImmutable());
(const_cast<SString*>(this))->Set(s);
}
}

RETURN;
}

//-----------------------------------------------------------------------------
// Set s to be a copy of this string's contents, but in the unicode format.
//-----------------------------------------------------------------------------
Expand Down Expand Up @@ -1827,26 +1860,6 @@ const UTF8 *SString::GetUTF8(AbstractScratchBuffer &scratch, COUNT_T *pcbUtf8) c
RETURN ((SString&)scratch).GetRawUTF8();
}

//-----------------------------------------------------------------------------
// Get a const pointer to the internal buffer which must already be a UTF8 string.
// This avoids the need to create a scratch buffer we know will never be used.
//-----------------------------------------------------------------------------
const UTF8 *SString::GetUTF8NoConvert() const
{
CONTRACT(const UTF8 *)
{
INSTANCE_CHECK_NULL;
THROWS;
GC_NOTRIGGER;
}
CONTRACT_END;

if (IsRepresentation(REPRESENTATION_UTF8))
RETURN GetRawUTF8();

ThrowHR(E_INVALIDARG);
}

//-----------------------------------------------------------------------------
// Safe version of sprintf.
// Prints formatted ansi text w/ var args to this buffer.
Expand Down
9 changes: 3 additions & 6 deletions src/coreclr/vm/array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -402,8 +402,7 @@ MethodTable* Module::CreateArrayMethodTable(TypeHandle elemTypeHnd, CorElementTy
StackSString ssElemName;
elemTypeHnd.GetName(ssElemName);

StackScratchBuffer scratch;
jkoritzinsky marked this conversation as resolved.
Show resolved Hide resolved
elemTypeHnd.GetAssembly()->ThrowTypeLoadException(ssElemName.GetUTF8(scratch), IDS_CLASSLOAD_VALUECLASSTOOLARGE);
elemTypeHnd.GetAssembly()->ThrowTypeLoadException(ssElemName.GetUTF8(), IDS_CLASSLOAD_VALUECLASSTOOLARGE);
}
}

Expand Down Expand Up @@ -510,8 +509,7 @@ MethodTable* Module::CreateArrayMethodTable(TypeHandle elemTypeHnd, CorElementTy
#ifdef _DEBUG
StackSString debugName;
TypeString::AppendType(debugName, TypeHandle(pMT));
StackScratchBuffer buff;
const char* pDebugNameUTF8 = debugName.GetUTF8(buff);
const char* pDebugNameUTF8 = debugName.GetUTF8();
S_SIZE_T safeLen = S_SIZE_T(strlen(pDebugNameUTF8))+S_SIZE_T(1);
if(safeLen.IsOverflow()) COMPlusThrowHR(COR_E_OVERFLOW);
size_t len = safeLen.Value();
Expand Down Expand Up @@ -657,8 +655,7 @@ MethodTable* Module::CreateArrayMethodTable(TypeHandle elemTypeHnd, CorElementTy
StackSString ssElemName;
elemTypeHnd.GetName(ssElemName);

StackScratchBuffer scratch;
elemTypeHnd.GetAssembly()->ThrowTypeLoadException(ssElemName.GetUTF8(scratch),
elemTypeHnd.GetAssembly()->ThrowTypeLoadException(ssElemName.GetUTF8(),
IDS_CLASSLOAD_VALUECLASSTOOLARGE);
}

Expand Down
3 changes: 1 addition & 2 deletions src/coreclr/vm/assembly.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1004,8 +1004,7 @@ Module *Assembly::FindModuleByName(LPCSTR pszModuleName)
SString moduleName(SString::Utf8, pszModuleName);
moduleName.LowerCase();

StackScratchBuffer buffer;
pszModuleName = moduleName.GetUTF8(buffer);
pszModuleName = moduleName.GetUTF8();

mdFile kFile = GetManifestFileToken(pszModuleName);
if (kFile == mdTokenNil)
Expand Down
9 changes: 4 additions & 5 deletions src/coreclr/vm/assemblynative.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ extern "C" void QCALLTYPE AssemblyNative_InternalLoad(NativeAssemblyNameParts* p
if (pAssemblyNameParts->_pName == NULL)
COMPlusThrow(kArgumentException, W("Format_StringZeroLength"));

StackSString ssName;
SString(SString::Literal, pAssemblyNameParts->_pName).ConvertToUTF8(ssName);
StackSString ssName(pAssemblyNameParts->_pName);
jkoritzinsky marked this conversation as resolved.
Show resolved Hide resolved

AssemblyMetaDataInternal asmInfo;

Expand All @@ -87,11 +86,11 @@ extern "C" void QCALLTYPE AssemblyNative_InternalLoad(NativeAssemblyNameParts* p

SmallStackSString ssLocale;
if (pAssemblyNameParts->_pCultureName != NULL)
SString(SString::Literal, pAssemblyNameParts->_pCultureName).ConvertToUTF8(ssLocale);
asmInfo.szLocale = (pAssemblyNameParts->_pCultureName != NULL) ? ssLocale.GetUTF8NoConvert() : NULL;
ssLocale.Set(pAssemblyNameParts->_pCultureName);
jkoritzinsky marked this conversation as resolved.
Show resolved Hide resolved
asmInfo.szLocale = (pAssemblyNameParts->_pCultureName != NULL) ? ssLocale.GetUTF8() : NULL;

// Initialize spec
spec.Init(ssName.GetUTF8NoConvert(), &asmInfo,
spec.Init(ssName.GetUTF8(), &asmInfo,
pAssemblyNameParts->_pPublicKeyOrToken, pAssemblyNameParts->_cbPublicKeyOrToken, pAssemblyNameParts->_flags);

if (pParentAssembly != NULL)
Expand Down
12 changes: 6 additions & 6 deletions src/coreclr/vm/class.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2236,7 +2236,7 @@ void MethodTable::DebugRecursivelyDumpInstanceFields(LPCUTF8 pszClassName, BOOL
// Display them
if(debug) {
ssBuff.Printf("%s:\n", pszClassName);
OutputDebugStringUtf8(ssBuff.GetUTF8NoConvert());
OutputDebugStringUtf8(ssBuff.GetUTF8());
}
else {
LOG((LF_CLASSLOADER, LL_ALWAYS, "%s:\n", pszClassName));
Expand All @@ -2250,7 +2250,7 @@ void MethodTable::DebugRecursivelyDumpInstanceFields(LPCUTF8 pszClassName, BOOL
#endif
if(debug) {
ssBuff.Printf("offset %3d %s\n", pFD->GetOffset_NoLogging(), pFD->GetName());
OutputDebugStringUtf8(ssBuff.GetUTF8NoConvert());
OutputDebugStringUtf8(ssBuff.GetUTF8());
}
else {
LOG((LF_CLASSLOADER, LL_ALWAYS, "offset %3d %s\n", pFD->GetOffset_NoLogging(), pFD->GetName()));
Expand Down Expand Up @@ -2299,7 +2299,7 @@ void MethodTable::DebugDumpFieldLayout(LPCUTF8 pszClassName, BOOL debug)
if (debug)
{
ssBuff.Printf("Field layout for '%s':\n\n", pszClassName);
OutputDebugStringUtf8(ssBuff.GetUTF8NoConvert());
OutputDebugStringUtf8(ssBuff.GetUTF8());
}
else
{
Expand All @@ -2326,7 +2326,7 @@ void MethodTable::DebugDumpFieldLayout(LPCUTF8 pszClassName, BOOL debug)
FieldDesc *pFD = GetClass()->GetFieldDescList() + ((GetNumInstanceFields()-cParentInstanceFields) + i);
if(debug) {
ssBuff.Printf("offset %3d %s\n", pFD->GetOffset_NoLogging(), pFD->GetName());
OutputDebugStringUtf8(ssBuff.GetUTF8NoConvert());
OutputDebugStringUtf8(ssBuff.GetUTF8());
}
else
{
Expand Down Expand Up @@ -2404,7 +2404,7 @@ MethodTable::DebugDumpGCDesc(
if (fDebug)
{
ssBuff.Printf("GC description for '%s':\n\n", pszClassName);
OutputDebugStringUtf8(ssBuff.GetUTF8NoConvert());
OutputDebugStringUtf8(ssBuff.GetUTF8());
}
else
{
Expand Down Expand Up @@ -2438,7 +2438,7 @@ MethodTable::DebugDumpGCDesc(
pSeries->GetSeriesOffset() - OBJECT_SIZE,
pSeries->GetSeriesSize(),
pSeries->GetSeriesSize() + GetBaseSize() );
OutputDebugStringUtf8(ssBuff.GetUTF8NoConvert());
OutputDebugStringUtf8(ssBuff.GetUTF8());
}
else
{
Expand Down
6 changes: 2 additions & 4 deletions src/coreclr/vm/classhash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,14 +205,12 @@ static void ConstructKeyFromDataCaseInsensitive(EEClassHashTable::ConstructKeyCa
StackSString nameSpace(SString::Utf8, pszNameSpace);
nameSpace.LowerCase();

StackScratchBuffer nameSpaceBuffer;
Key[0] = (LPUTF8)nameSpace.GetUTF8(nameSpaceBuffer);
Key[0] = (LPUTF8)nameSpace.GetUTF8();

StackSString name(SString::Utf8, pszName);
name.LowerCase();

StackScratchBuffer nameBuffer;
Key[1] = (LPUTF8)name.GetUTF8(nameBuffer);
Key[1] = (LPUTF8)name.GetUTF8();

pCallback->UseKeys(Key);
}
Expand Down
6 changes: 2 additions & 4 deletions src/coreclr/vm/clsload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1796,15 +1796,13 @@ VOID ClassLoader::CreateCanonicallyCasedKey(LPCUTF8 pszNameSpace, LPCUTF8 pszNam
StackSString nameSpace(SString::Utf8, pszNameSpace);
nameSpace.LowerCase();

StackScratchBuffer nameSpaceBuffer;
pszNameSpace = nameSpace.GetUTF8(nameSpaceBuffer);
pszNameSpace = nameSpace.GetUTF8();


StackSString name(SString::Utf8, pszName);
name.LowerCase();

StackScratchBuffer nameBuffer;
pszName = name.GetUTF8(nameBuffer);
pszName = name.GetUTF8();


size_t iNSLength = strlen(pszNameSpace);
Expand Down
5 changes: 2 additions & 3 deletions src/coreclr/vm/codepitchingmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,8 @@ static void LookupOrCreateInPitchingCandidate(MethodDesc* pMD, ULONG sizeOfCode)
SString className, methodName, methodSig;
pMD->GetMethodInfo(className, methodName, methodSig);

StackScratchBuffer scratch;
const char* szClassName = className.GetUTF8(scratch);
const char* szMethodSig = methodSig.GetUTF8(scratch);
const char* szClassName = className.GetUTF8();
const char* szMethodSig = methodSig.GetUTF8();

printf("Candidate %lu %s :: %s %s\n",
sizeOfCode, szClassName, pMD->GetName(), szMethodSig);
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/vm/comcallablewrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -734,7 +734,7 @@ void SimpleComCallWrapper::LogRefCount(ComCallWrapper *pWrap, StackSString &ssMe
EX_TRY
{
ssMessage.AppendPrintf(", RefCount=%u\n", dwRefCountToLog);
OutputDebugStringUtf8(ssMessage.GetUTF8NoConvert());
OutputDebugStringUtf8(ssMessage.GetUTF8());
}
EX_CATCH
{ }
Expand Down
3 changes: 1 addition & 2 deletions src/coreclr/vm/comdelegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -925,8 +925,7 @@ FCIMPL5(FC_BOOL_RET, COMDelegate::BindToMethodName,

// get the name in UTF8 format
SString wszName(SString::Literal, gc.methodName->GetBuffer());
StackScratchBuffer utf8Name;
LPCUTF8 szNameStr = wszName.GetUTF8(utf8Name);
LPCUTF8 szNameStr = wszName.GetUTF8();
jkoritzinsky marked this conversation as resolved.
Show resolved Hide resolved

// pick a proper compare function
typedef int (__cdecl *UTF8StringCompareFuncPtr)(const char *, const char *);
Expand Down
9 changes: 4 additions & 5 deletions src/coreclr/vm/coreassemblyspec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,7 @@ extern "C" void QCALLTYPE AssemblyName_InitializeAssemblySpec(NativeAssemblyName

BEGIN_QCALL;

StackSString ssName;
SString(SString::Literal, pAssemblyNameParts->_pName).ConvertToUTF8(ssName);
StackSString ssName(pAssemblyNameParts->_pName);
jkoritzinsky marked this conversation as resolved.
Show resolved Hide resolved

AssemblyMetaDataInternal asmInfo;

Expand All @@ -197,11 +196,11 @@ extern "C" void QCALLTYPE AssemblyName_InitializeAssemblySpec(NativeAssemblyName

SmallStackSString ssLocale;
if (pAssemblyNameParts->_pCultureName != NULL)
SString(SString::Literal, pAssemblyNameParts->_pCultureName).ConvertToUTF8(ssLocale);
asmInfo.szLocale = (pAssemblyNameParts->_pCultureName != NULL) ? ssLocale.GetUTF8NoConvert() : NULL;
ssLocale.Set(pAssemblyNameParts->_pCultureName);
jkoritzinsky marked this conversation as resolved.
Show resolved Hide resolved
asmInfo.szLocale = (pAssemblyNameParts->_pCultureName != NULL) ? ssLocale.GetUTF8() : NULL;

// Initialize spec
pAssemblySpec->Init(ssName.GetUTF8NoConvert(), &asmInfo,
pAssemblySpec->Init(ssName.GetUTF8(), &asmInfo,
pAssemblyNameParts->_pPublicKeyOrToken, pAssemblyNameParts->_cbPublicKeyOrToken, pAssemblyNameParts->_flags);

// Copy and own any fields we do not own
Expand Down
6 changes: 2 additions & 4 deletions src/coreclr/vm/corhost.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -426,15 +426,13 @@ HRESULT CorHost2::ExecuteInDefaultAppDomain(LPCWSTR pwzAssemblyPath,
Assembly *pAssembly = AssemblySpec::LoadAssembly(pwzAssemblyPath);

SString szTypeName(pwzTypeName);
StackScratchBuffer buff1;
const char* szTypeNameUTF8 = szTypeName.GetUTF8(buff1);
const char* szTypeNameUTF8 = szTypeName.GetUTF8();
MethodTable *pMT = ClassLoader::LoadTypeByNameThrowing(pAssembly,
NULL,
szTypeNameUTF8).AsMethodTable();

SString szMethodName(pwzMethodName);
StackScratchBuffer buff;
const char* szMethodNameUTF8 = szMethodName.GetUTF8(buff);
const char* szMethodNameUTF8 = szMethodName.GetUTF8();
MethodDesc *pMethodMD = MemberLoader::FindMethod(pMT, szMethodNameUTF8, &gsig_SM_Str_RetInt);

if (!pMethodMD)
Expand Down
3 changes: 1 addition & 2 deletions src/coreclr/vm/customattribute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,8 @@ TypeHandle Attribute::GetTypeForEnum(LPCUTF8 szEnumName, COUNT_T cbEnumName, Dom
}
CONTRACTL_END;

StackScratchBuffer buff;
StackSString sszEnumName(SString::Utf8, szEnumName, cbEnumName);
return TypeName::GetTypeUsingCASearchRules(sszEnumName.GetUTF8(buff), pDomainAssembly->GetAssembly());
return TypeName::GetTypeUsingCASearchRules(sszEnumName.GetUTF8(), pDomainAssembly->GetAssembly());
}

/*static*/
Expand Down
Loading