The Delphi Bug List

Entry No.
607
VCL - Data Access - DBTables
Invalid parameters passed to AnsiToNativeBuf and NativeToAnsiBuf can cause application to hang
1.02 2.01 3.0 3.01 3.02 4.0 4.01 4.02 4.03 5.0 5.01 6.0 6.01 6.02 Kylix 1.0
AbsentAbsentAbsentAbsentAbsentAbsentAbsentAbsentAbsentAbsentExistsFixedFixedFixedN/A
Description
Reported by Anders Melander
The critical section used in AnsiToNativeBuf and NativeToAnsiBuf are not protected by a try..finally. This can cause an application to hang if one thread passes an invalid parameter to the function (e.g. a null pointer).

The following scenario demonstrates the bug:

  1. Thread A calls AnsiToNativeBuf with null as either Source or Dest parameter.
    AnsiToNativeBuf locks the critical section and then calls DbiAnsiToNative which throws an Access Violation.
  2. Thread B calls AnsiToNativeBuf.
    AnsiToNativeBuf attempts to lock the critical section but is blocked because thread A never unlocked it.
  3. Application (or at least the thread) is hung.
Solution / workaround
Apply the following modification to AnsiToNativeBuf in dbTables.pas (apply similar changes to NativeToAnsiBuf):
procedure AnsiToNativeBuf(Locale: TLocale; Source, Dest: PChar; Len: Integer);
var
  DataLoss: LongBool;
begin
  if Len > 0 then
    if Locale <> nil then
    begin
      EnterCriticalSection(CSAnsiToNative);
      try
        DbiAnsiToNative(Locale, Dest, Source, Len, DataLoss);
      finally
        LeaveCriticalSection(CSAnsiToNative);
      end;
    end else
      CharToOemBuff(Source, Dest, Len);
end;
User-contributed comments
Anders Melander
19 Jun 2001  10:33 AM GMT
This problem has been fixed in Delphi 6.
Latest update of this entry: 2001-06-19

Post a comment on this bug


Index page
Delphi Bug List home page
The Delphi Bug Lists are presently maintained by Jordan Russell, who has taken over this task from Reinier Sterkenburg since August 2000.
All feedback is appreciated. See also the feedback section of the Delphi Bug List home page.