当控件的类名非常非常长时,为什么会出现访问冲突?

Cos*_*und 23 delphi buffer-overflow delphi-2010

我按顺序继承了一个控件,所以我可以添加一些我需要的字段,但现在当我在运行时创建它时,我得到一个Access Violation.不幸的是,这种访问冲突不会发生在我正在创建控件的地方,甚至那些我在启用所有调试选项的情况下构建的(包括"使用调试DCU构建")堆栈跟踪根本无法帮助我!

在我尝试重现错误时,我尝试创建一个控制台应用程序,但显然这个错误只出现在Forms应用程序中,并且只有当我的控件实际显示在表单上时!

以下是重现错误的步骤.创建一个新的VCL Forms应用程序,单击一个按钮,双击以创建OnClick处理程序并写入:

type TWinControl<T,K,W> = class(TWinControl);

procedure TForm3.Button1Click(Sender: TObject);
begin
  with TWinControl<TWinControl, TWinControl, TWinControl>.Create(Self) do
  begin
    Parent := Self;
  end;
end;
Run Code Online (Sandbox Code Playgroud)

每次我尝试时,这都会连续生成访问冲突.仅在Delphi 2010上测试过,因为这是我在这台计算机上唯一的版本.

问题是:

  • 这是Delphi的Generics中的已知错误吗?
  • 这有解决方法吗?

编辑

以下是质量控制报告的链接:http://qc.embarcadero.com/wc/qcmain.aspx?d = 112101

Cos*_*und 27

首先,这与泛型无关,但在使用泛型时更有可能表现出来.事实证明,有一个缓冲区溢出错误TControl.CreateParams.如果你查看代码,你会注意到它填充了一个TCreateParams结构,尤其重要的是,它填充TCreateParams.WinClassName了当前类的名称(the ClassName).不幸的WinClassName是只有64char的固定长度缓冲区,但是需要包含NULL终止符; 所以有效的64char ClassName会溢出那个缓冲区!

它可以使用以下代码进行测试:

TLongWinControlClassName4567890123456789012345678901234567891234 = class(TWinControl)
end;

procedure TForm3.Button1Click(Sender: TObject);
begin
  with TLongWinControlClassName4567890123456789012345678901234567891234.Create(Self) do
  begin
    Parent := Self;
  end;
end;
Run Code Online (Sandbox Code Playgroud)

该类名称长度恰好为 64个字符.让它缩短一个字符,错误消失!

这是一个很多更容易使用,因为这样的Delphi泛型时发生的构造ClassName:包括在参数类型声明的单位名称,再加上一个点,那么参数类型的名称.例如,TWinControl<TWinControl, TWinControl, TWinControl>该类具有以下ClassName:

TWinControl<Controls.TWinControl,Controls.TWinControl,Controls.TWinControl>
Run Code Online (Sandbox Code Playgroud)

这个75角色很长,超过63极限.

解决方法

我从潜在错误生成类中采用了一个简单的错误消息.这样的东西,来自构造函数:

constructor TWinControl<T, K, W>.Create(aOwner: TComponent);
begin
  {$IFOPT D+}
  if Length(ClassName) > 63 then raise Exception.Create('The resulting ClassName is too    long: ' + ClassName);
  {$ENDIF}
  inherited;
end;
Run Code Online (Sandbox Code Playgroud)

至少这显示了一个可以立即采取行动的体面错误信息.

稍后编辑,真正的解决方法

以前的解决方案(引发错误)适用于具有实际长名称的非泛型类; 人们很可能能够缩短它,使其成为63个或更少.泛型类型的情况并非如此:我遇到了一个带有2个类型参数的TWinControl后代遇到这个问题,所以它的格式如下:

TMyControlName<Type1, Type2>
Run Code Online (Sandbox Code Playgroud)

基于此泛型类型的具体类型的gnerate ClassName采用以下形式:

TMyControlName<UnitName1.Type1,UnitName2.Type2>
Run Code Online (Sandbox Code Playgroud)

所以它包括5个标识符(2x单位标识符+ 3x类型标识符)+5个符号(<.,.>); 这5个标识符的平均长度需要小于12个字符,否则总长度超过63:5x12 + 5 = 65.每个标识符只使用11-12个字符非常少,并且违背了最佳实践(即:使用长描述性名称,因为击键是免费的!).同样,在我的情况下,我根本无法使我的标识符变短.

考虑到缩短ClassName并不总是可能,我想我会尝试消除问题的原因(缓冲区溢出).不幸的是,这非常困难,因为错误源于层次结构TWinControl.CreateParams的底部CreateParams.我们不能调用inherited因为CreateParams在继承链中使用它来构建窗口创建参数.不调用它将需要复制基本TWinControl.CreateParamsPLUS中的所有代码中间类中的所有代码; 它也不是非常便携,因为任何代码可能会随着未来版本VCL(或者我们可能是子类化的第三方控件的未来版本)而改变.

以下解决方案不会停止TWinControl.CreateParams溢出缓冲区,但会使其无害,然后(当inherited调用返回时)修复问题.我正在使用一个新的记录(所以我可以控制布局),包括原始TCreateParams但是填充它有很多空间TWinControl.CreateParams可以溢出.TWinControl.CreateParams溢出它想要的一切,然后我阅读完整的文本并使其符合记录的原始边界,同时确保得到的缩短名称可能是唯一的.我在WndName中包含原始ClassName的HASH以帮助解决唯一性问题:

type
  TWrappedCreateParamsRecord = record
    Orignial: TCreateParams;
    SpaceForCreateParamsToSafelyOverflow: array[0..2047] of Char;
  end;

procedure TExtraExtraLongWinControlDescendantClassName_0123456789_0123456789_0123456789_0123456789.CreateParams(var Params: TCreateParams);
var Wrapp: TWrappedCreateParamsRecord;
    Hashcode: Integer;
    HashStr: string;
begin
  // Do I need to take special care?
  if Length(ClassName) >= Length(Params.WinClassName) then
    begin
      // Letting the code go through will cause an Access Violation because of the
      // Buffer Overflow in TWinControl.CreateParams; Yet we do need to let the
      // inherited call go through, or else parent classes don't get the chance
      // to manipulate the Params structure. Since we can't fix the root cause (we
      // can't stop TWinControl.CreateParams from overflowing), let's make sure the
      // overflow will be harmless.
      ZeroMemory(@Wrapp, SizeOf(Wrapp));
      Move(Params, Wrapp.Orignial, SizeOf(TCreateParams));
      // Call the original CreateParams; It'll still overflow, but it'll probably be hurmless since we just
      // padded the orginal data structure with a substantial ammount of space.
      inherited CreateParams(Wrapp.Orignial);
      // The data needs to move back into the "Params" structure, but before we can do that
      // we should FIX the overflown buffer. We can't simply trunc it to 64, and we don't want
      // the overhead of keeping track of all the variants of this class we might encounter.
      // Note: Think of GENERIC classes, where you write this code once, but there might
      // be many-many different ClassNames at runtime!
      //
      // My idea is to FIX this by keeping as much of the original name as possible, but
      // including the HASH value of the full name into the window name; If the HASH function
      // is any good then the resulting name as a very high probability of being Unique. We'll
      // use the default Hash function used for Delphi's generics.
      HashCode := TEqualityComparer<string>.Default.GetHashCode(PChar(@Wrapp.Orignial.WinClassName));
      HashStr := IntToHex(HashCode, 8);
      Move(HashStr[1], Wrapp.Orignial.WinClassName[High(Wrapp.Orignial.WinClassName)-8], 8*SizeOf(Char));
      Wrapp.Orignial.WinClassName[High(Wrapp.Orignial.WinClassName)] := #0;
      // Move the TCreateParams record back were we've got it from
      Move(Wrapp.Orignial, Params, SizeOf(TCreateParams));
    end
  else
    inherited;
end;
Run Code Online (Sandbox Code Playgroud)

  • API允许256个字符,我想知道为什么有人会提出64个限制.. (9认同)