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

Fix memory leaks in Assembler::CreateExportDirectory #50431

Merged
1 commit merged into from
Apr 16, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't a change in this PR, but a DWORD (unsigned int on my machine) might have a different alignment than a BYTE (char on my machine). Could this fail?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

malloc on Linux returns 8 byte aligned memory for 32 bit systems and 16 byte aligned ones on 64 bit systems. See https://www.gnu.org/software/libc/manual/html_node/Aligned-Memory-Blocks.html. I assume new would behave the same way.

// 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