Skip to content

Commit

Permalink
Consolidate the GUID to string logic (#77394)
Browse files Browse the repository at this point in the history
* Consolidate the GUID to string logic

Replace obvious uses of GUID to string
logic with a new constant for buffer length.
  • Loading branch information
AaronRobinsonMSFT authored Oct 25, 2022
1 parent 9290f85 commit a394aea
Show file tree
Hide file tree
Showing 12 changed files with 29 additions and 54 deletions.
2 changes: 1 addition & 1 deletion src/coreclr/ildasm/dasm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5154,7 +5154,7 @@ void DumpCodeManager(IMAGE_COR20_HEADER *CORHeader, void* GUICookie)
ULONG iCount = VAL32(CORHeader->CodeManagerTable.Size) / sizeof(GUID);
for (ULONG i=0; i<iCount; i++)
{
CHAR rcguid[128];
CHAR rcguid[GUID_STR_BUFFER_LEN];
GUID Guid = *pcm;
SwapGuid(&Guid);
GuidToLPSTR(Guid, rcguid);
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/ildasm/dis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -821,7 +821,7 @@ BOOL SourceLinesHelper(void *GUICookie, LineCodeDescr* pLCD, _Out_writes_(nSize)

PAL_TRY(Param *, pParam, &param) {
GUID guidLang={0},guidLangVendor={0},guidDoc={0};
CHAR zLang[64],zVendor[64],zDoc[64];
CHAR zLang[GUID_STR_BUFFER_LEN],zVendor[GUID_STR_BUFFER_LEN],zDoc[GUID_STR_BUFFER_LEN];
ULONG32 k;
if(pParam->pLCD->FileToken != ulWasFileToken)
{
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/ildasm/dman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ void DumpScope(void* GUICookie)
mdModule mdm;
GUID mvid;
WCHAR scopeName[1024];
CHAR guidString[1024];
CHAR guidString[GUID_STR_BUFFER_LEN];
memset(scopeName,0,1024*sizeof(WCHAR));
if(SUCCEEDED(g_pPubImport->GetScopeProps( scopeName, 1024, NULL, &mvid))&& scopeName[0])
{
Expand Down
30 changes: 3 additions & 27 deletions src/coreclr/inc/utilcode.h
Original file line number Diff line number Diff line change
Expand Up @@ -411,33 +411,6 @@ inline WCHAR* FormatInteger(WCHAR* str, size_t strCount, const char* fmt, I v)
return str;
}

class GuidString final
{
char _buffer[ARRAY_SIZE("{12345678-1234-1234-1234-123456789abc}")];
public:
static void Create(const GUID& g, GuidString& ret)
{
// Ensure we always have a null
ret._buffer[ARRAY_SIZE(ret._buffer) - 1] = '\0';
sprintf_s(ret._buffer, ARRAY_SIZE(ret._buffer), "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
g.Data1, g.Data2, g.Data3,
g.Data4[0], g.Data4[1],
g.Data4[2], g.Data4[3],
g.Data4[4], g.Data4[5],
g.Data4[6], g.Data4[7]);
}

const char* AsString() const
{
return _buffer;
}

operator const char*() const
{
return _buffer;
}
};

inline
LPWSTR DuplicateString(
LPCWSTR wszString,
Expand Down Expand Up @@ -3447,6 +3420,9 @@ class ConfigMethodSet
BYTE m_inited;
};

// 38 characters + 1 null terminating.
#define GUID_STR_BUFFER_LEN (ARRAY_SIZE("{12345678-1234-1234-1234-123456789abc}"))

//*****************************************************************************
// Convert a GUID into a pointer to a string
//*****************************************************************************
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/utilcode/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2895,7 +2895,7 @@ namespace Com
{
STANDARD_VM_CONTRACT;

WCHAR wszClsid[39];
WCHAR wszClsid[GUID_STR_BUFFER_LEN];
if (GuidToLPWSTR(rclsid, wszClsid) == 0)
return E_UNEXPECTED;

Expand Down
4 changes: 2 additions & 2 deletions src/coreclr/utilcode/util_nodependencies.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ GuidToLPSTR(
_Out_writes_(cchGuid) LPSTR szGuid, // String into which the GUID is stored
DWORD cchGuid) // Count in chars
{
if (cchGuid < 39)
if (cchGuid < GUID_STR_BUFFER_LEN)
return 0;

return sprintf_s(szGuid, cchGuid, "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
Expand Down Expand Up @@ -508,7 +508,7 @@ GuidToLPWSTR(
// successive fields break the GUID into the form DWORD-WORD-WORD-WORD-WORD.DWORD
// covering the 128-bit GUID. The string includes enclosing braces, which are an OLE convention.

if (cchGuid < 39) // 38 chars + 1 null terminating.
if (cchGuid < GUID_STR_BUFFER_LEN)
return 0;

// {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}
Expand Down
5 changes: 2 additions & 3 deletions src/coreclr/vm/assemblybinder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,10 @@ NativeImage* AssemblyBinder::LoadNativeImage(Module* componentModule, LPCUTF8 na
#ifdef FEATURE_READYTORUN
static void MvidMismatchFatalError(GUID mvidActual, GUID mvidExpected, LPCUTF8 simpleName, bool compositeComponent, LPCUTF8 assemblyRequirementName)
{
static const size_t MVID_TEXT_LENGTH = 39;
CHAR assemblyMvidText[MVID_TEXT_LENGTH];
CHAR assemblyMvidText[GUID_STR_BUFFER_LEN];
GuidToLPSTR(mvidActual, assemblyMvidText);

CHAR componentMvidText[MVID_TEXT_LENGTH];
CHAR componentMvidText[GUID_STR_BUFFER_LEN];
GuidToLPSTR(mvidExpected, componentMvidText);

SString message;
Expand Down
4 changes: 2 additions & 2 deletions src/coreclr/vm/comcallablewrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3652,7 +3652,7 @@ BOOL ComMethodTable::LayOutInterfaceMethodTable(MethodTable* pClsMT)
{
BEGIN_PROFILER_CALLBACK(CORProfilerTrackCCW());
#if defined(_DEBUG)
CHAR rIID[40]; // {00000000-0000-0000-0000-000000000000}
CHAR rIID[GUID_STR_BUFFER_LEN];
GuidToLPSTR(m_IID, rIID);
LOG((LF_CORPROF, LL_INFO100, "COMClassicVTableCreated Class:%hs, IID:%s, vTbl:%#08x\n",
pItfClass->GetDebugClassName(), rIID, pUnkVtable));
Expand Down Expand Up @@ -4703,7 +4703,7 @@ ComCallWrapperTemplate* ComCallWrapperTemplate::CreateTemplate(TypeHandle thClas
GenerateClassItfGuid(thClass, &IClassXIID);

#if defined(_DEBUG)
CHAR rIID[40]; // {00000000-0000-0000-0000-000000000000}
CHAR rIID[GUID_STR_BUFFER_LEN];
GuidToLPSTR(IClassXIID, rIID);
SString ssName;
thClass.GetName(ssName);
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/vm/interoputil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3967,7 +3967,7 @@ VOID LogInteropQI(IUnknown* pItf, REFIID iid, HRESULT hrArg, _In_z_ LPCSTR szMsg
HRESULT hr = S_OK;
SafeComHolder<IUnknown> pUnk = NULL;
int cch = 0;
CHAR szIID[64];
CHAR szIID[GUID_STR_BUFFER_LEN];

hr = SafeQueryInterface(pItf, IID_IUnknown, &pUnk);

Expand Down
12 changes: 6 additions & 6 deletions src/coreclr/vm/profilinghelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -702,8 +702,8 @@ HRESULT ProfilingAPIUtility::AttemptLoadProfilerForStartup()
return hr;
}

GuidString clsidUtf8;
GuidString::Create(clsid, clsidUtf8);
char clsidUtf8[GUID_STR_BUFFER_LEN];
GuidToLPSTR(clsid, clsidUtf8);
hr = LoadProfiler(
kStartupLoad,
&clsid,
Expand Down Expand Up @@ -736,8 +736,8 @@ HRESULT ProfilingAPIUtility::AttemptLoadDelayedStartupProfilers()
LOG((LF_CORPROF, LL_INFO10, "**PROF: Profiler loading from GUID/Path stored from the IPC channel."));
CLSID *pClsid = &(item->guid);

GuidString clsidUtf8;
GuidString::Create(*pClsid, clsidUtf8);
char clsidUtf8[GUID_STR_BUFFER_LEN];
GuidToLPSTR(*pClsid, clsidUtf8);
HRESULT hr = LoadProfiler(
kStartupLoad,
pClsid,
Expand Down Expand Up @@ -816,8 +816,8 @@ HRESULT ProfilingAPIUtility::AttemptLoadProfilerList()
continue;
}

GuidString clsidUtf8;
GuidString::Create(clsid, clsidUtf8);
char clsidUtf8[GUID_STR_BUFFER_LEN];
GuidToLPSTR(clsid, clsidUtf8);
hr = LoadProfiler(
kStartupLoad,
&clsid,
Expand Down
8 changes: 4 additions & 4 deletions src/coreclr/vm/profilinghelper.inl
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ inline void ProfilingAPIUtility::LogNoInterfaceError(REFIID iidRequested, LPCSTR
}
CONTRACTL_END;

GuidString iidUtf8;
GuidString::Create(iidRequested, iidUtf8);
char iidUtf8[GUID_STR_BUFFER_LEN];
GuidToLPSTR(iidRequested, iidUtf8);
ProfilingAPIUtility::LogProfError(IDS_E_PROF_NO_CALLBACK_IFACE, szCLSID, (LPCSTR)iidUtf8);
}

Expand Down Expand Up @@ -176,8 +176,8 @@ inline HRESULT ProfilingAPIUtility::LoadProfilerForAttach(
CONTRACTL_END;

// Inform user we're about to try attaching the profiler
GuidString clsidUtf8;
GuidString::Create(*pClsid, clsidUtf8);
char clsidUtf8[GUID_STR_BUFFER_LEN];
GuidToLPSTR(*pClsid, clsidUtf8);
ProfilingAPIUtility::LogProfInfo(IDS_PROF_ATTACH_REQUEST_RECEIVED, (LPCSTR)clsidUtf8);

return LoadProfiler(
Expand Down
10 changes: 5 additions & 5 deletions src/coreclr/vm/runtimecallablewrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ void ComClassFactory::ThrowHRMsg(HRESULT hr, DWORD dwMsgResID)

SString strMessage;
SString strResource;
WCHAR strClsid[39];
WCHAR strClsid[GUID_STR_BUFFER_LEN];
SString strHRDescription;

// Obtain the textual representation of the HRESULT.
Expand Down Expand Up @@ -504,7 +504,7 @@ IClassFactory *ComClassFactory::GetIClassFactory()
{
SString strMessage;
SString strResource;
WCHAR strClsid[39];
WCHAR strClsid[GUID_STR_BUFFER_LEN];
SString strHRDescription;

// Obtain the textual representation of the HRESULT.
Expand Down Expand Up @@ -2635,7 +2635,7 @@ void ComObject::ThrowInvalidCastException(OBJECTREF *pObj, MethodTable *pCastToM
}

// Convert the IID to a string.
WCHAR strIID[39];
WCHAR strIID[GUID_STR_BUFFER_LEN];
GuidToLPWSTR(iid, strIID);

// Obtain the textual description of the HRESULT.
Expand All @@ -2653,7 +2653,7 @@ void ComObject::ThrowInvalidCastException(OBJECTREF *pObj, MethodTable *pCastToM
pSrcItfClass->GetGuid(&SrcItfIID, TRUE);

// Convert the source interface IID to a string.
WCHAR strSrcItfIID[39];
WCHAR strSrcItfIID[GUID_STR_BUFFER_LEN];
GuidToLPWSTR(SrcItfIID, strSrcItfIID);

COMPlusThrow(kInvalidCastException, IDS_EE_RCW_INVALIDCAST_EVENTITF, strHRDescription.GetUnicode(), strComObjClassName.GetUnicode(),
Expand All @@ -2667,7 +2667,7 @@ void ComObject::ThrowInvalidCastException(OBJECTREF *pObj, MethodTable *pCastToM
else if ((pNativeIID = MngStdInterfaceMap::GetNativeIIDForType(thCastTo)) != NULL)
{
// Convert the source interface IID to a string.
WCHAR strNativeItfIID[39];
WCHAR strNativeItfIID[GUID_STR_BUFFER_LEN];
GuidToLPWSTR(*pNativeIID, strNativeItfIID);

// Query for the interface to determine the failure HRESULT.
Expand Down

0 comments on commit a394aea

Please sign in to comment.