View Issue Details

IDProjectCategoryView StatusLast Update
0033993FPCFCLpublic2018-08-21 16:05
ReporterBart BroersmaAssigned ToMichael Van Canneyt 
PrioritynormalSeverityminorReproducibilityalways
Status closedResolutionfixed 
Platformi386OSWindowsOS Version10
Product Version3.1.1Product Buildr39352 
Target Version3.2.0Fixed in Version3.3.1 
Summary0033993: TRegIniFile crash on WriteXXX if Filename in constructor starts with '\'
DescriptionIf you Create a TRegIni instance and the FN paramter in Create() starts with a backslash, then any attempt to write a value to the registry will fail with a message like:
ERegistryException: Failed to create key: "\\Software\FSI2\RegIni\NewSection"
Steps To ReproduceGiven the following example program:

program rtest;

{$apptype console}
{$ifdef fpc}{$mode objfpc}{$endif}

uses
  registry, sysutils, classes {$ifndef fpc}, windows{$endif};

var
  RegIni: TRegIniFile;
  WFileName, S: String;
  i: Integer;
begin
  RegIni := TRegIniFile.Create('\Software\FSI2\RegIni');
  try
    writeln('Created RegIni');
    RegIni.RootKey := HKEY_CURRENT_USER;
    writeln(format('Filename = "%s"',[RegIni.FileName]));
    S := RegIni.ReadString('NewSection','NewString','Failure!');
    writeln(format('S = "%s"',[S]));
    RegIni.WriteString('NewSection','NewString','Success');
    S := RegIni.ReadString('NewSection','NewString','Failure!');
    writeln(format('S = "%s"',[S]));
  finally
    RegIni.Free;
  end;
end.

Build and run with Delphi (7):

C:\Users\Bart\LazarusProjecten\bugs\Console\regini>dcc32 rtest.lpr
Borland Delphi Version 15.0
Copyright (c) 1983,2002 Borland Software Corporation
rtest.lpr(44)
45 lines, 0.05 seconds, 74344 bytes code, 3625 bytes data.

C:\Users\Bart\LazarusProjecten\bugs\Console\regini>rtest
Created RegIni
Filename = "\Software\FSI2\RegIni"
S = "Failure!" //expected since the key does not exist yet.
S = "Success"


Build and run with fpc:

C:\Users\Bart\LazarusProjecten\bugs\Console\regini>fpc rtest.lpr
Free Pascal Compiler version 3.1.1 [2018/06/03] for i386
Copyright (c) 1993-2018 by Florian Klaempfl and others
Target OS: Win32 for i386
Compiling rtest.lpr
Linking rtest.exe
43 lines compiled, 0.2 sec, 151680 bytes code, 6628 bytes data

C:\Users\Bart\LazarusProjecten\bugs\Console\regini>rtest
Created RegIni
Filename = "\Software\FSI2\RegIni"
S = "Failure!"
An unhandled exception occurred at $0040F9DB:
ERegistryException: Failed to create key: "\\Software\FSI2\RegIni\NewSection"
  $0040F9DB
  $004100D0
  $0040FF4D
  $0040185D
TagsNo tags attached.
Fixed in Revision39659
FPCOldBugId
FPCTarget
Attached Files
  • regini-remove-starting-backslash-fpath.diff (451 bytes)
    Index: packages/fcl-registry/src/regini.inc
    ===================================================================
    --- packages/fcl-registry/src/regini.inc	(revision 39352)
    +++ packages/fcl-registry/src/regini.inc	(working copy)
    @@ -14,6 +14,8 @@
       fFileName := FN;
       if fFileName<>'' then begin
         fPath := fFileName + '\';
    +    if fPath[1]='\' then
    +      System.Delete(fPath,1,1);
         OpenKey(fFileName, aaccess <> KEY_READ);
       end
       else
    

Activities

Bart Broersma

2018-07-15 19:21

reporter   ~0109496

Last edited: 2018-07-15 19:21

View 2 revisions

function TRegIniFile.OpenSection(const Section: string; CreateSection : Boolean = false): boolean;
var
  k: HKEY;
  S : String;

begin
  S:=Section;
  If (S<>'') and (S[1] = '\') then
    Delete(S,1,1);
  if CreateSection then
    CreateKey('\'+FPath+S); <<== here the extra backslash is added
  if Section <> '' then
    begin
    k:=GetKey('\'+FPath+S);
    if k = 0 then
      begin
      Result:=False;
      exit;
      end;
    SetCurrentKey(k);
  end;
  Result:=True;
end;

Bart Broersma

2018-07-15 22:54

reporter  

regini-remove-starting-backslash-fpath.diff (451 bytes)
Index: packages/fcl-registry/src/regini.inc
===================================================================
--- packages/fcl-registry/src/regini.inc	(revision 39352)
+++ packages/fcl-registry/src/regini.inc	(working copy)
@@ -14,6 +14,8 @@
   fFileName := FN;
   if fFileName<>'' then begin
     fPath := fFileName + '\';
+    if fPath[1]='\' then
+      System.Delete(fPath,1,1);
     OpenKey(fFileName, aaccess <> KEY_READ);
   end
   else

Bart Broersma

2018-07-15 22:54

reporter   ~0109503

Possible patch attached.

Bart Broersma

2018-08-18 14:31

reporter   ~0110125

Can I get some feedback on the patch?

Michael Van Canneyt

2018-08-21 10:46

administrator   ~0110186

Checked and applied the patch.

I didn't see it when you submitted it.
Feel free to mail if you see no-one replies.

Bart Broersma

2018-08-21 16:05

reporter   ~0110197

This has been around for ages, so I had time.
Care to take a look at related 0033980?

Issue History

Date Modified Username Field Change
2018-07-15 19:18 Bart Broersma New Issue
2018-07-15 19:21 Bart Broersma Note Added: 0109496
2018-07-15 19:21 Bart Broersma Note Edited: 0109496 View Revisions
2018-07-15 22:54 Bart Broersma File Added: regini-remove-starting-backslash-fpath.diff
2018-07-15 22:54 Bart Broersma Note Added: 0109503
2018-08-18 14:31 Bart Broersma Note Added: 0110125
2018-08-21 10:28 Michael Van Canneyt Assigned To => Michael Van Canneyt
2018-08-21 10:28 Michael Van Canneyt Status new => assigned
2018-08-21 10:46 Michael Van Canneyt Fixed in Revision => 39659
2018-08-21 10:46 Michael Van Canneyt Note Added: 0110186
2018-08-21 10:46 Michael Van Canneyt Status assigned => resolved
2018-08-21 10:46 Michael Van Canneyt Fixed in Version => 3.3.1
2018-08-21 10:46 Michael Van Canneyt Resolution open => fixed
2018-08-21 10:46 Michael Van Canneyt Target Version => 3.2.0
2018-08-21 16:05 Bart Broersma Note Added: 0110197
2018-08-21 16:05 Bart Broersma Status resolved => closed