Changeset 2341


Ignore:
Timestamp:
11/4/2011 11:08:06 PM (3 years ago)
Author:
lowjoel
Message:

Fixed an obscure crash when running the unlocker code under Vista SP2 - the NtQuerySystemInformation? call seems to return a wrong result when we query only for size. So, we will keep querying until we get all the entries we need (using the do-while loop and when the call returns NT_SUCCESS). Should fix http://bbs.heidi.ie/viewtopic.php?f=2&t=8337.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/eraser/Eraser.Util.Native/OpenHandle.cpp

    r2334 r2341  
    2929        List<OpenHandle^>^ handles = gcnew List<OpenHandle^>(); 
    3030 
    31         //Get the number of handles on the system then load up the complete list. 
    32         std::auto_ptr<SYSTEM_HANDLES> handlesList(new SYSTEM_HANDLES); 
     31        //Try to load up the complete list of handles open. 
     32        std::vector<char> handlesBuffer; 
    3333        { 
    34             DWORD bufferSize = 0; 
    35             NtQuerySystemInformation(static_cast<SYSTEM_INFORMATION_CLASS>(SystemHandleInformation), 
    36                 handlesList.get(), sizeof(SYSTEM_HANDLES), &bufferSize); 
     34            DWORD bufferSize = sizeof(SYSTEM_HANDLES); 
     35            NTSTATUS result = STATUS_SUCCESS; 
     36            do 
     37            { 
     38                handlesBuffer.resize(bufferSize); 
     39                result = NtQuerySystemInformation( 
     40                    static_cast<SYSTEM_INFORMATION_CLASS>(SystemHandleInformation), 
     41                    &handlesBuffer.front(), handlesBuffer.size(), &bufferSize); 
     42            } 
     43            while (!NT_SUCCESS(result)); 
    3744 
    38             //Then get the whole list 
    39             handlesList.reset(reinterpret_cast<PSYSTEM_HANDLES>(new char[bufferSize])); 
    40             NtQuerySystemInformation(static_cast<SYSTEM_INFORMATION_CLASS>(SystemHandleInformation), 
    41                 handlesList.get(), bufferSize, &bufferSize); 
    42  
    43             if (bufferSize == 0) 
    44                 throw gcnew InvalidOperationException(S::_(L"The list of open system handles could not be retrieved.")); 
     45            if (!NT_SUCCESS(result)) 
     46                throw gcnew InvalidOperationException("The list of open system handles could not be retrieved."); 
    4547        } 
    4648 
    4749        //Iterate over the handles 
     50        SYSTEM_HANDLES* handlesList = reinterpret_cast<SYSTEM_HANDLES*>(&handlesBuffer.front()); 
    4851        for (ULONG i = 0; i != handlesList->NumberOfHandles; ++i) 
    4952        { 
Note: See TracChangeset for help on using the changeset viewer.