View Issue Details

IDProjectCategoryView StatusLast Update
0037233FPCRTLpublic2021-04-11 19:04
ReporterZamrony P. Juhara Assigned ToMarco van de Voort  
PrioritynormalSeverityminorReproducibilityalways
Status resolvedResolutionfixed 
Platformx86_64OSubuntu 
Product Version3.2.0 
Target Version3.2.2Fixed in Version3.2.2 
Summary0037233: getlongopts() with empty short option cause access violation
DescriptionOn Free Pascal 3.2.0, getLongOpts() function with empty string shortopts cause access violation.

Following code does not cause access violation,

c:=getlongopts('abc:d:012',@theopts[1],optionindex);

Following code does,

c:=getlongopts('',@theopts[1],optionindex);
Steps To Reproduce1) Create testopt program from example on https://www.freepascal.org/docs-html/rtl/getopts/getopt.html

program testopt;

{ Program to depmonstrate the getopts function. }

{
  Valid calls to this program are
  optex --verbose --add me --delete you
  optex --append --create child
  optex -ab -c me -d you
  and so on
}
uses getopts;

var c : char;
    optionindex : Longint;
    theopts : array[1..7] of TOption;

begin
  with theopts[1] do
   begin
    name:='add';
    has_arg:=1;
    flag:=nil;
    value:=#0;
  end;
  with theopts[2] do
   begin
    name:='append';
    has_arg:=0;
    flag:=nil;
    value:=#0;
  end;
  with theopts[3] do
   begin
    name:='delete';
    has_arg:=1;
    flag:=nil;
    value:=#0;
  end;
  with theopts[4] do
   begin
    name:='verbose';
    has_arg:=0;
    flag:=nil;
    value:=#0;
  end;
  with theopts[5] do
   begin
    name:='create';
    has_arg:=1;
    flag:=nil;
    value:='c'
  end;
  with theopts[6] do
   begin
    name:='file';
    has_arg:=1;
    flag:=nil;
    value:=#0;
  end;
  with theopts[7] do
   begin
    name:='';
    has_arg:=0;
    flag:=nil;
  end;
  c:=#0;
  repeat
    c:=getlongopts('abc:d:012',@theopts[1],optionindex);
    case c of
      '1','2','3','4','5','6','7','8','9' :
        begin
        writeln ('Got optind : ',c)
        end;
      #0 : begin
           write ('Long option : ',theopts[optionindex].name);
           if theopts[optionindex].has_arg>0 then
             writeln (' With value : ',optarg)
           else
             writeln
           end;
      'a' : writeln ('Option a.');
      'b' : writeln ('Option b.');
      'c' : writeln ('Option c : ', optarg);
      'd' : writeln ('Option d : ', optarg);
      '?',':' : writeln ('Error with opt : ',optopt);
   end; { case }
 until c=endofoptions;
 if optind<=paramcount then
    begin
    write ('Non options : ');
    while optind<=paramcount do
      begin
      write (paramstr(optind),' ');
      inc(optind)
      end;
    writeln
    end
end.


2) Modify
c:=getlongopts('abc:d:012',@theopts[1],optionindex);
to become
c:=getlongopts('',@theopts[1],optionindex);

3) Compile with FPC 3.2.0
$ fpc testopt.pas

4) Run
$ ./testopt
Runtime error 216 at $0000000000425D9A
  $0000000000425D9A
  $00000000004223EC
Additional InformationOn Free Pascal 3.0.4, shortopts with value of empty string does not cause access violation
TagsNo tags attached.
Fixed in Revision45683 merged 45703
FPCOldBugId
FPCTarget3.2.2
Attached Files

Activities

Marco van de Voort

2020-06-22 12:37

manager   ~0123514

$0040D430 GETOPT_INIT, line 223 of ../inc/getopts.pp
 $0040E059 GETLONGOPTS, line 515 of ../inc/getopts.pp
 $00401644 main, line 70 of lopts.pp
 $0040A037 EXE_ENTRY, line 171 of system.pp

Marco van de Voort

2020-06-23 11:01

manager   ~0123526

fixed in svn, thanks

Issue History

Date Modified Username Field Change
2020-06-21 16:32 Zamrony P. Juhara New Issue
2020-06-22 12:37 Marco van de Voort Note Added: 0123514
2020-06-23 11:01 Marco van de Voort Assigned To => Marco van de Voort
2020-06-23 11:01 Marco van de Voort Status new => resolved
2020-06-23 11:01 Marco van de Voort Resolution open => fixed
2020-06-23 11:01 Marco van de Voort Fixed in Revision => 45683
2020-06-23 11:01 Marco van de Voort FPCTarget => -
2020-06-23 11:01 Marco van de Voort Note Added: 0123526
2021-04-11 18:29 Marco van de Voort Product Version => 3.2.0
2021-04-11 18:29 Marco van de Voort Fixed in Version => 3.2.2
2021-04-11 18:29 Marco van de Voort Target Version => 3.2.2
2021-04-11 18:29 Marco van de Voort FPCTarget - => 3.2.2
2021-04-11 19:04 Marco van de Voort Fixed in Revision 45683 => 45683 merged 45703