Skip to content

Commit

Permalink
Fix memory leaks in Assembler::CreateExportDirectory (dotnet#50431)
Browse files Browse the repository at this point in the history
Use smart pointers to avoid having to manually deallocate memory.

- szOutputFileName was never being deallocated

- `pAlias` and `exportDirData` were only being deallocated on the
  success path and not on any of the error paths.
  • Loading branch information
omajid authored Apr 16, 2021
1 parent 180e5fa commit ff17846
Showing 1 changed file with 4 additions and 7 deletions.
11 changes: 4 additions & 7 deletions src/coreclr/ilasm/writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -322,13 +322,12 @@ HRESULT Assembler::CreateExportDirectory()

IMAGE_EXPORT_DIRECTORY exportDirIDD;
DWORD exportDirDataSize;
BYTE *exportDirData;
EATEntry *pEATE;
unsigned i, L, ordBase = 0xFFFFFFFF, Ldllname;
// get the DLL name from output file name
char* pszDllName;
Ldllname = (unsigned)wcslen(m_wzOutputFileName)*3+3;
char* szOutputFileName = new char[Ldllname];
NewArrayHolder<char> szOutputFileName(new char[Ldllname]);
memset(szOutputFileName,0,wcslen(m_wzOutputFileName)*3+3);
WszWideCharToMultiByte(CP_ACP,0,m_wzOutputFileName,-1,szOutputFileName,Ldllname,NULL,NULL);
pszDllName = strrchr(szOutputFileName,DIRECTORY_SEPARATOR_CHAR_A);
Expand All @@ -341,11 +340,11 @@ HRESULT Assembler::CreateExportDirectory()
// Allocate buffer for tables
for(i = 0, L=0; i < Nentries; i++) L += 1+(unsigned)strlen(m_EATList.PEEK(i)->szAlias);
exportDirDataSize = Nentries*5*sizeof(WORD) + L + Ldllname;
exportDirData = new BYTE[exportDirDataSize];
NewArrayHolder<BYTE> exportDirData(new BYTE[exportDirDataSize]);
memset(exportDirData,0,exportDirDataSize);

// Export address table
DWORD* pEAT = (DWORD*)exportDirData;
DWORD* pEAT = (DWORD*)(BYTE*)exportDirData;
// Name pointer table
DWORD* pNPT = pEAT + Nentries;
// Ordinal table
Expand All @@ -356,7 +355,7 @@ HRESULT Assembler::CreateExportDirectory()
char* pDLLName = pENT + L;

// sort the names/ordinals
char** pAlias = new char*[Nentries];
NewArrayHolder<char*> pAlias(new char*[Nentries]);
for(i = 0; i < Nentries; i++)
{
pEATE = m_EATList.PEEK(i);
Expand Down Expand Up @@ -474,8 +473,6 @@ HRESULT Assembler::CreateExportDirectory()
// Copy the debug directory into the section.
memcpy(de, &exportDirIDD, sizeof(IMAGE_EXPORT_DIRECTORY));
memcpy(de + sizeof(IMAGE_EXPORT_DIRECTORY), exportDirData, exportDirDataSize);
delete [] pAlias;
delete [] exportDirData;
return S_OK;
}

Expand Down

0 comments on commit ff17846

Please sign in to comment.