为什么此代码会生成异常?

时间:2011-06-29 09:55:51

标签: delphi pointers dynamic-arrays

我今天编写了一些代码,列出了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;

1 个答案:

答案 0 :(得分: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一旦数组超出范围,delphi就会尝试减少refcount并可能释放数组。并且该部分访问数组指向的数据前面的引用信息,这在您的情况下无效。

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

--------------------------
|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有点生疏,因此可能会有一些小的语法错误)

相关问题