The Delphi Bug List

Entry No.
424
VCL - General - IniFiles - TIniFile
In TIniFile.WriteInteger and TIniFile.WriteString, passing a section argument that ends with a space character causes "an invalid page fault in module KRNL386.EXE at 0002:00005c53"
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
AbsentGotchaGotchaGotchaGotchaGotchaGotchaGotchaGotchaGotchaGotchaGotchaGotchaGotchaN/A
Description
Reported by Alan M. Cohen; checked by Reinier Sterkenburg
Extra information on this 'bug' was provided by Jordan Russell, Eddie Churchill (Borland), and Erwin Dokter

See also bug #676.

The bug can be reliably reproduced with the following code attached to a button click:

  IniFile := TIniFile.Create('Test.ini');
  IniFile.WriteString('Test ', 'Key1', 'Val1');
  IniFile.Free;
Note that the last character in the 'Test ' argument on the second line is a ' '. Deleting the ' ' ends the problem.
Erwin Dokter explains:
The fact this bug does not appear in Delphi 1 is because Delphi 1 does not use the PChar typecast. In Delphi 1, all calls to an API function which requires PChar parameters used the following method to convert a string to a PChar:
function SomeCall(const parm: string): Word;
var
  buf: array[0..255] of Char;
begin
  SomeApiCall(StrPCopy(buf, parm);
end;

In Delphi 2, the PChar typecast was introduced, and all calls changed to:

function SomeCall(const parm: string): Word;
begin
  SomeApiCall(PChar(parm));
end;

This explains the manifestation in Delphi 2 onward. The page fault occurs because WritePrivateProfileString tries to strip the trailing space from a typecasted constant. Passing a real PChar, as in Delphi 1, poses no problem.

Some other notes:

  1. This bug is Windows version dependent: Windows 95 and 98 have the bug, but NT 4.0 SP3 does not have it (although there the space gets truncated from the section name). The bug does not appear in NT because WritePrivateProfileString uses its own buffer.
  2. TIniFile.WriteBool inherits this bug because it calls WriteString.
  3. WriteString uses WritePrivateProfileString and that in turn seems to indicate (according to MSDN) that spaces are not allowed in the first param. Mind you it actually says that it can contain uppercase and lowercase letters but numbers seem to work as well. While Delphi should strip out any spaces before calling WritePrivateProfileString (and who knows that is what it may do in the future) for the time being users should avoid using spaces.
Solution / workaround

Nothing better (yet?) than: avoid using spaces at the end of INI sections.

Latest update of this entry: 2002-03-18

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.