无效的浮点运算

时间:2015-11-26 22:52:35

标签: delphi point floating operation

我一直收到此错误"无效的浮点操作"。

我在Delphi 7上。

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
TlHelp32, Dialogs, StdCtrls, ExtCtrls, Buttons, ComCtrls;

var //global
PHandle, cancel, bytes, scantype: integer;

...

procedure Tmain.scanbtnClick(Sender: TObject);
    var max, address: Integer;
    floatinput, floatinput1, floatinput2, datafloat: Real;
    x: cardinal;
    itm: TListItem;
begin

  floatinput := StrToFloat(Trim(valueinput.Text));
  floatinput1 := StrToFloat(Trim(valueinput1.Text));
  floatinput2 := StrToFloat(Trim(valueinput2.Text));

  if floatinput2 < floatinput1 then
  begin
    floatinput1 := floatinput1 + floatinput2;
    floatinput2 := floatinput1 - floatinput2;
    floatinput1 := floatinput1 - floatinput2;
  end;

  result.Show;

  x := 0;
  address := 0;

  result.resultlist.Clear;

  repeat
    Application.ProcessMessages;
    statusbar1.Panels.Items[1].Text := 'Searching... ' + IntToStr(address * 100 div max) + '% (' + IntToStr(address div bytes) + ' out of ' + IntToStr(max div bytes) + ').';

    if ReadprocessMemory(PHandle, Ptr(address), @datafloat, bytes, x) then
      begin
        if (x > 0) then
        begin
          if scantype = 0 then
          begin
            if datafloat = floatinput then             //error here
            begin
              itm := result.resultlist.Items.Add;
              itm.Caption := '0x' + IntToHex(address,8);
              itm.SubItems.Add(FormatFloat('0.0#########', datafloat));
            end;
          end;
          if scantype = 1 then
          begin
            if datafloat > floatinput              //also here
            then begin
              itm := result.resultlist.Items.Add;
              itm.Caption := '0x' + IntToHex(address,8);
              itm.SubItems.Add(FormatFloat('0.0#########', datafloat));
            end;
          end;
          if scantype = 2 then
          begin
            if datafloat < floatinput     //here too
            then begin
              itm := result.resultlist.Items.Add;
              itm.Caption := '0x' + IntToHex(address,8);
              itm.SubItems.Add(FormatFloat('0.0#########', datafloat));
            end;
          end;
          if scantype = 2 then
          begin
            if (dataint <= intinput2) and (dataint >= intinput1)    //even here
            then begin
              itm := result.resultlist.Items.Add;
              itm.Caption := '0x' + IntToHex(address,8);
              itm.SubItems.Add(FormatFloat('0.0#########', datafloat));
            end;
          end;

        end;
      end;

    if x <> 0
    then address := address + x
    else address := address + bytes;

  until (address >= Max) or (cancel = 1);

end;

我甚至检查了cpu窗口,它发生了,因为它试图从指向空值的指针加载浮点值。

它不是ReadMemory,因为这段代码在while循环中,它会在遇到此错误之前返回几个有效值。

我该怎么办?

提前致谢。

1 个答案:

答案 0 :(得分:4)

我们可以看到代码存在两个潜在问题。

首先,您不检查ReadProcessMemory的返回值。该呼叫可能由于各种原因而失败。由于您没有检查错误,因此您无法知道函数调用是否成功。始终检查API调用是否成功。阅读MSDN上的文档以了解如何执行此操作。通常这涉及检查函数返回值,如此处的情况。如果函数失败,则浮点变量可能包含未初始化的数据,并且可能会发生错误。

另一个问题是通过从另一个进程读取字节来填充datafloat。如果这些字节不表示有效的浮点值,那么如果您尝试对该值进行操作,则会引发异常。并非所有位模式都表示有效的浮点值。例如,您可能已经遇到信号NaN值。也许您应该与CompareMem进行比较,因为您似乎正在阅读任意内存,而这似乎正在努力对其他程序进行逆向工程。通过按位比较进行测试将避免将无效值加载到浮动寄存器中的风险。

最后,我不确定你对null测试浮点值是什么意思,无论null是什么。浮点值不可为空。你很可能在那里有一个重大的误解。

相关问题