I wanted to create a replacement for IsBadReadPtr and came up with this solution. Could anyone with a fresh mind have a quick review of it? Thank you :)

I wanted to create a replacement for IsBadReadPtr and came up with this solution. Could anyone with a fresh mind have a quick review of it? Thank you :)

function IsBadReadPtr(APointer: Pointer; ASize: DWORD): Boolean;
const
  CMask = PAGE_READONLY or PAGE_READWRITE or PAGE_WRITECOPY
    or PAGE_EXECUTE_READ or PAGE_EXECUTE_READWRITE or PAGE_EXECUTE_WRITECOPY;
var
  mbi: TMemoryBasicInformation;
begin
    if VirtualQuery(APointer, mbi, SizeOf(MEMORY_BASIC_INFORMATION)) = 0 then
    Result := False
  else begin
    Result := (mbi.Protect and CMask) = 0;
    if (mbi.Protect and PAGE_GUARD) <> 0 then
      Result := True;
    if (mbi.Protect and PAGE_NOACCESS) <> 0 then
      Result := True;
  end;
end;

Comments

  1. Is this a shortened version of your routine, or is ASize never used?
    You could shorten the function to:

     Result := (VirtualQuery(APointer, mbi, SizeOf(MEMORY_BASIC_INFORMATION)) <> 0)
           and ((mbi.Protect and (PAGE_GUARD or PAGE_NOACCESS)) <> 0);

    ReplyDelete
  2. Thanks about pointing me to the unused parameter.

    ReplyDelete

Post a Comment