4

我今天写了一些代码,将列出 PE 文件中的所有部分...该代码有效,但最后它给出了一个异常:无效的指针操作...我不知道为什么...有人可以找到错误

这是代码

procedure TForm1.Button1Click(Sender: TObject);
var
  IDH:PImageDosHeader;
  buf:Pointer;
  INH:PImageNtHeaders;
  ISH:array of TImageSectionHeader;
  FS:TFileStream;
  i,total:Word;
begin
  if OpenDialog1.Execute then
    begin
        Self.Caption:=OpenDialog1.FileName;
        FS:=TFileStream.Create(OpenDialog1.FileName,fmOpenRead or fmShareDenyNone);
        GetMem(buf,FS.Size);
        FS.Read(buf^,FS.Size);
        FS.Free;
        IDH:=buf;
        INH:=Ptr(Cardinal(buf)+Cardinal(IDH^._lfanew));
        ISH:=Ptr(Cardinal(buf)+Cardinal(IDH^._lfanew) + Sizeof(TImageNtHeaders));
        total:=INH^.FileHeader.NumberOfSections - 1 ;
        for i:=0 to total  do
        begin
              ListBox1.Items.Add(PAnsichar(@ISH[i].Name));
              Application.ProcessMessages;
        end;

    end;
end;
4

1 回答 1

11
ISH:array of TImageSectionHeader;

这声明了一个动态数组。虽然动态数组是指针,但它们需要在它们指向的数据之前添加额外的数据,包括长度和引用计数。

因此,让它指向 PE 标头中的某些数据是没有意义的:

ISH:=Ptr(Cardinal(buf)+Cardinal(IDH^._lfanew) + Sizeof(TImageNtHeaders));

虽然这部分似乎出于某种原因编译,但访问数组可能会出现错误:

ISH[i]//This might not work correctly since ISH does not point to a valid dynamic array.

或者,如果代码在该部分仍然存在(也许您禁用了数组边界检查或长度信息恰好足够大),那么一旦数组超出范围,delphi 就会尝试减少引用计数并可能释放数组。并且该部分访问数组指向的数据前面的引用计数信息,这在您的情况下无效。

如果我没记错的话,动态数组的内存布局类似于:

--------------------------
|refcount|length|data....|
--------------------------
                ^ pointer goes here

这意味着您会遇到问题,因为 refcount/length 字段包含垃圾。


我认为您想将其声明为:

type TImageSectionHeaderArray=array[0..70000]TImageSectionHeader;//No idea what the limit on section headers is
     PImageSectionHeaderArray=^TImageSectionHeaderArray;
...
var ISH:PImageSectionHeaderArray;

(我的delphi有点生疏,所以可能有一些小的语法错误)

于 2011-06-29T10:13:52.570 回答