diff --git a/reactos/dll/win32/kernel32/misc/ldr.c b/reactos/dll/win32/kernel32/misc/ldr.c index 985bc1c10b3..66476944c86 100644 --- a/reactos/dll/win32/kernel32/misc/ldr.c +++ b/reactos/dll/win32/kernel32/misc/ldr.c @@ -230,24 +230,31 @@ GetProcAddress( HMODULE hModule, LPCSTR lpProcName ) { ANSI_STRING ProcedureName; FARPROC fnExp = NULL; + NTSTATUS Status; if (HIWORD(lpProcName) != 0) { RtlInitAnsiString (&ProcedureName, (LPSTR)lpProcName); - LdrGetProcedureAddress ((PVOID)hModule, + Status = LdrGetProcedureAddress ((PVOID)hModule, &ProcedureName, 0, (PVOID*)&fnExp); } else { - LdrGetProcedureAddress ((PVOID)hModule, + Status = LdrGetProcedureAddress ((PVOID)hModule, NULL, (ULONG)lpProcName, (PVOID*)&fnExp); } + if (!NT_SUCCESS(Status)) + { + SetLastError( RtlNtStatusToDosError( Status ) ); + fnExp = NULL; + } + return fnExp; } @@ -313,27 +320,25 @@ GetModuleFileNameA ( Module = CONTAINING_RECORD(Entry, LDR_DATA_TABLE_ENTRY, InLoadOrderLinks); if (Module->DllBase == (PVOID)hModule) { - if (nSize * sizeof(WCHAR) < Module->FullDllName.Length) - { - SetLastErrorByStatus (STATUS_BUFFER_TOO_SMALL); - } - else - { - FileName.Length = 0; - FileName.MaximumLength = (USHORT)nSize * sizeof(WCHAR); - FileName.Buffer = lpFilename; + Length = min(nSize, Module->FullDllName.Length / sizeof(WCHAR)); + FileName.Length = 0; + FileName.MaximumLength = (USHORT)Length * sizeof(WCHAR); + FileName.Buffer = lpFilename; - /* convert unicode string to ansi (or oem) */ - if (bIsFileApiAnsi) - RtlUnicodeStringToAnsiString (&FileName, - &Module->FullDllName, - FALSE); - else - RtlUnicodeStringToOemString (&FileName, - &Module->FullDllName, - FALSE); - Length = Module->FullDllName.Length / sizeof(WCHAR); - } + /* convert unicode string to ansi (or oem) */ + if (bIsFileApiAnsi) + RtlUnicodeStringToAnsiString (&FileName, + &Module->FullDllName, + FALSE); + else + RtlUnicodeStringToOemString (&FileName, + &Module->FullDllName, + FALSE); + + if (nSize < Length) + SetLastErrorByStatus (STATUS_BUFFER_TOO_SMALL); + else + lpFilename[Length] = '\0'; RtlLeaveCriticalSection (Peb->LoaderLock); return Length; @@ -381,22 +386,20 @@ GetModuleFileNameW ( if (Module->DllBase == (PVOID)hModule) { - if (nSize * sizeof(WCHAR) < Module->FullDllName.Length) - { - SetLastErrorByStatus (STATUS_BUFFER_TOO_SMALL); - } - else - { - FileName.Length = 0; - FileName.MaximumLength =(USHORT)nSize * sizeof(WCHAR); - FileName.Buffer = lpFilename; + Length = min(nSize, Module->FullDllName.Length / sizeof(WCHAR)); + FileName.Length = 0; + FileName.MaximumLength = (USHORT) Length * sizeof(WCHAR); + FileName.Buffer = lpFilename; - RtlCopyUnicodeString (&FileName, - &Module->FullDllName); - Length = Module->FullDllName.Length / sizeof(WCHAR); - } + RtlCopyUnicodeString (&FileName, + &Module->FullDllName); + if (nSize < Length) + SetLastErrorByStatus (STATUS_BUFFER_TOO_SMALL); + else + lpFilename[Length] = L'\0'; RtlLeaveCriticalSection (Peb->LoaderLock); + return Length; }