Mercurial > pub > dyncall > dyncall
changeset 398:a29af998e457
- dynload w/ PE files:
* some better header check
* check if export table exists (thanks Ashok!)
* make some dynsyms function behaviour consistent with ELF and mach-o (thanks Ashok!)
author | Tassilo Philipp |
---|---|
date | Tue, 06 Apr 2021 21:23:39 +0200 |
parents | 77ec5123e7ab |
children | 7eb98750bc47 |
files | dynload/dynload_syms_pe.c |
diffstat | 1 files changed, 15 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- a/dynload/dynload_syms_pe.c Sat Jan 23 14:30:24 2021 +0100 +++ b/dynload/dynload_syms_pe.c Tue Apr 06 21:23:39 2021 +0200 @@ -59,7 +59,19 @@ base = (const char*)pLib; pDOSHeader = (IMAGE_DOS_HEADER*)base; pNTHeader = (IMAGE_NT_HEADERS*)(base + pDOSHeader->e_lfanew); + + /* optional header present and big enough? this header should exist as it's only optional for object files */ + if(pNTHeader->FileHeader.SizeOfOptionalHeader < (&pNTHeader->OptionalHeader.DataDirectory - &pNTHeader->OptionalHeader)) + return NULL; + + /* export table available? */ + if(pNTHeader->OptionalHeader.NumberOfRvaAndSizes <= IMAGE_DIRECTORY_ENTRY_EXPORT) + return NULL; + pExportsDataDir = &pNTHeader->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT]; + if(!pExportsDataDir->VirtualAddress) + return NULL; + pExports = (IMAGE_EXPORT_DIRECTORY*)(base + pExportsDataDir->VirtualAddress); pSyms = (DLSyms*)dlAllocMem(sizeof(DLSyms)); @@ -85,12 +97,14 @@ int dlSymsCount(DLSyms* pSyms) { - return (int)pSyms->count; + return pSyms ? (int)pSyms->count : 0; } const char* dlSymsName(DLSyms* pSyms, int index) { + if(!pSyms || index < 0 || index >= pSyms->count) + return NULL; return pSyms->pBase + pSyms->pNames[index]; }