Recent

Author Topic: Why my program crashed due to Access Violation when operating string type?  (Read 323 times)

TYDQ

  • Full Member
  • ***
  • Posts: 115
Due to the whole code character number exceed to the maximum of the forum,I copied my problematic code where the error occurs(The errorous function):
Code: Pascal  [Select][+][-]
  1. function c_string_generate_from_string(const str:string;pas:boolean=false):c_string;
  2. var i,j,k,m,len:SizeInt;
  3.     res:c_string;
  4.     bool:boolean;
  5.     gstr,gtempstr1,gtempstr2,gtempstr3:string;
  6. begin
  7.  gstr:=str; gtempstr1:=''; gtempstr2:=''; gtempstr3:='';
  8.  i:=1; len:=length(str); res.count:=0; bool:=false;
  9.  SetLength(res.item,0); SetLength(res.status,0);
  10.  while(i<=len) do
  11.   begin
  12.    if(pas) then
  13.     begin
  14.      if(gstr[i]=#39) or (gstr[i]='#') then
  15.       begin
  16.        j:=i;
  17.        while(j<=len)do
  18.         begin
  19.          if(Copy(gstr,j,2)='#$') then
  20.           begin
  21.            k:=j+2;
  22.            while(k<=len) and (c_string_is_hex(gstr[k])) and (k-j<=5) do inc(k);
  23.            j:=k+1; continue;
  24.           end
  25.          else if(gstr[j]='#') then
  26.           begin
  27.            k:=j+1;
  28.            while(k<=len) and (gstr[k]>='0') and (gstr[k]<='9') and (k-j<=5) do inc(k);
  29.            j:=k+1; continue;
  30.           end
  31.          else if(gstr[j]=#39) then
  32.           begin
  33.            k:=j+1;
  34.            while(k<=len) and (gstr[k]<>#39) do inc(k);
  35.            j:=k+1; continue;
  36.           end
  37.          else break;
  38.         end;
  39.        inc(res.count);
  40.        SetLength(res.item,res.count);
  41.        res.item[res.count-1]:=Copy(gstr,i,j-i+1);
  42.        i:=j+1; inc(j); m:=i;
  43.        while(j<=len) and (gstr[j]=' ') do inc(j);
  44.        if(j<=len) and (gstr[j]=#10) then
  45.         begin
  46.          SetLength(res.status,res.count);
  47.          res.status[res.count-1]:=2;
  48.         end
  49.        else if(j<=len) and (j>m) then
  50.         begin
  51.          SetLength(res.status,res.count);
  52.          res.status[res.count-1]:=1;
  53.         end
  54.        else if(j>len) or (gstr[j]>' ') then
  55.         begin
  56.          SetLength(res.status,res.count);
  57.          res.status[res.count-1]:=0;
  58.         end;
  59.        bool:=false;
  60.        continue;
  61.       end;
  62.     end
  63.    else
  64.     begin
  65.      if(gstr[i]=#39) or (gstr[i]='"') then
  66.       begin
  67.        j:=i+1;
  68.        while(j<=len) do
  69.         begin
  70.          if(j>i+3) and (gstr[j-3]<>'\') and (gstr[j-2]='\') and (gstr[j-1]='\')
  71.          and (gstr[i]=gstr[j]) then break
  72.          else if(j>i+2) and (gstr[j-2]='\') and (gstr[j-1]='\')
  73.          and (gstr[i]=gstr[j]) then break
  74.          else if(j>i+1) and (gstr[j-1]<>'\') and (gstr[i]=gstr[j]) then break
  75.          else if(j=i+1) and (gstr[i]=gstr[j]) then break;
  76.          inc(j);
  77.         end;
  78.        inc(res.count);
  79.        SetLength(res.item,res.count);
  80.        res.item[res.count-1]:=Copy(gstr,i,j-i+1);
  81.        i:=j+1; inc(j); m:=i;
  82.        while(j<=len) and (gstr[j]=' ') do inc(j);
  83.        if(j<=len) and (gstr[j]=#10) then
  84.         begin
  85.          SetLength(res.status,res.count);
  86.          res.status[res.count-1]:=2;
  87.         end
  88.        else if(j<=len) and (j>m) then
  89.         begin
  90.          SetLength(res.status,res.count);
  91.          res.status[res.count-1]:=1;
  92.         end
  93.        else if(j>len) or (gstr[j]>' ') then
  94.         begin
  95.          SetLength(res.status,res.count);
  96.          res.status[res.count-1]:=0;
  97.         end;
  98.        bool:=false;
  99.        continue;
  100.       end;
  101.     end;
  102.    if(i<=len-2) then gtempstr1:=Copy(gstr,i,3) else gtempstr1:=''; //That is where ERROR occurs.
  103.    if(i<=len-1) then gtempstr2:=Copy(gstr,i,2) else gtempstr2:='';
  104.    if(i<=len) then gtempstr3:=Copy(gstr,i,1) else gtempstr3:='';
  105.    if(pas=false) then
  106.     begin
  107.      if(i<=len-2) and (c_string_is_operator(gtempstr1)>0) then
  108.       begin
  109.        inc(res.count);
  110.        SetLength(res.item,res.count);
  111.        res.item[res.count-1]:=gtempstr1;
  112.        j:=i+3;
  113.        while(j<=len) and (gstr[j]=' ') do inc(j);
  114.        if(j<=len) and (gstr[j]=#10) then
  115.         begin
  116.          SetLength(res.status,res.count);
  117.          res.status[res.count-1]:=2;
  118.         end
  119.        else if(j<=len) and (j>i+3) then
  120.         begin
  121.          SetLength(res.status,res.count);
  122.          res.status[res.count-1]:=1;
  123.         end
  124.        else if(j>len) or (gstr[j]>' ') then
  125.         begin
  126.          SetLength(res.status,res.count);
  127.          res.status[res.count-1]:=0;
  128.         end;
  129.        inc(i,3); bool:=false; continue;
  130.       end
  131.      else if(i<=len-1) and (c_string_is_operator(gtempstr2)>0) then
  132.       begin
  133.        inc(res.count);
  134.        SetLength(res.item,res.count);
  135.        res.item[res.count-1]:=gtempstr2;
  136.        j:=i+2;
  137.        while(j<=len) and (gstr[j]=' ') do inc(j);
  138.        if(j<=len) and (gstr[j]=#10) then
  139.         begin
  140.          SetLength(res.status,res.count);
  141.          res.status[res.count-1]:=2;
  142.         end
  143.        else if(j<=len) and (j>i+2) then
  144.         begin
  145.          SetLength(res.status,res.count);
  146.          res.status[res.count-1]:=1;
  147.         end
  148.        else if(j>len) or (gstr[j]>' ') then
  149.         begin
  150.          SetLength(res.status,res.count);
  151.          res.status[res.count-1]:=0;
  152.         end;
  153.        inc(i,2); bool:=false; continue;
  154.       end
  155.      else if(c_string_is_operator(gtempstr3)>0) or (c_string_is_bracket(gstr[i])) then
  156.       begin
  157.        if((res.count>0) and (c_string_is_vaild_value(res.item[res.count-1])=false)
  158.        and (gstr[i]='-')) or ((gstr[i]='-') and (res.count=0)) then
  159.         begin
  160.          inc(res.count);
  161.          SetLength(res.item,res.count);
  162.          res.item[res.count-1]:=gtempstr3;
  163.          inc(i); bool:=true; continue;
  164.         end
  165.        else if(res.count>0) and (c_string_is_vaild_value(res.item[res.count-1])=true)
  166.        and (not c_string_is_bracket(gstr[i]))
  167.        and (gstr[i]='.') then
  168.         begin
  169.          res.item[res.count-1]:=res.item[res.count-1]+gtempstr3;
  170.          inc(i); bool:=true; continue;
  171.         end
  172.        else if(res.count>0) and (c_string_is_vaild_value(res.item[res.count-1])=true)
  173.        and (length(res.item[res.count-1])>0) and (not c_string_is_bracket(gstr[i]))
  174.        and (gstr[i]='+') and ((res.item[res.count-1][length(res.item[res.count-1])]='E')
  175.        or (res.item[res.count-1][length(res.item[res.count-1])]='e')) then
  176.         begin
  177.          res.item[res.count-1]:=res.item[res.count-1]+gtempstr3;
  178.          inc(i); bool:=true; continue;
  179.         end;
  180.        inc(res.count);
  181.        SetLength(res.item,res.count);
  182.        res.item[res.count-1]:=gtempstr3;
  183.        j:=i+1;
  184.        while(j<=len) and (gstr[j]=' ') do inc(j);
  185.        if(j<=len) and (gstr[j]=#10) then
  186.         begin
  187.          SetLength(res.status,res.count);
  188.          res.status[res.count-1]:=2;
  189.         end
  190.        else if(j<=len) and (j>i+1) then
  191.         begin
  192.          SetLength(res.status,res.count);
  193.          res.status[res.count-1]:=1;
  194.         end
  195.        else if(j>len) or (gstr[j]>' ') then
  196.         begin
  197.          SetLength(res.status,res.count);
  198.          res.status[res.count-1]:=0;
  199.         end;
  200.        inc(i); bool:=false; continue;
  201.       end;
  202.     end
  203.    else
  204.     begin
  205.      if(i<=len-2) and (c_string_is_operator(gtempstr1,true)>0) and (bool=false) then
  206.       begin
  207.        inc(res.count);
  208.        SetLength(res.item,res.count);
  209.        res.item[res.count-1]:=gtempstr1;
  210.        j:=i+3;
  211.        while(j<=len) and (gstr[j]=' ') do inc(j);
  212.        if(j<=len) and (gstr[j]=#10) then
  213.         begin
  214.          SetLength(res.status,res.count);
  215.          res.status[res.count-1]:=2;
  216.         end
  217.        else if(j<=len) and (j>i+3) then
  218.         begin
  219.          SetLength(res.status,res.count);
  220.          res.status[res.count-1]:=1;
  221.         end
  222.        else if(j>len) or (gstr[j]>' ') then
  223.         begin
  224.          SetLength(res.status,res.count);
  225.          res.status[res.count-1]:=0;
  226.         end;
  227.        inc(i,3); bool:=false; continue;
  228.       end
  229.      else if(i<=len-1) and (c_string_is_operator(gtempstr2,true)>0) and (bool=false) then
  230.       begin
  231.        inc(res.count);
  232.        SetLength(res.item,res.count);
  233.        res.item[res.count-1]:=gtempstr2;
  234.        j:=i+2;
  235.        while(j<=len) and (gstr[j]=' ') do inc(j);
  236.        if(j<=len) and (gstr[j]=#10) then
  237.         begin
  238.          SetLength(res.status,res.count);
  239.          res.status[res.count-1]:=2;
  240.         end
  241.        else if(j<=len) and (j>i+2) then
  242.         begin
  243.          SetLength(res.status,res.count);
  244.          res.status[res.count-1]:=1;
  245.         end
  246.        else if(j>len) or (gstr[j]>' ') then
  247.         begin
  248.          SetLength(res.status,res.count);
  249.          res.status[res.count-1]:=0;
  250.         end;
  251.        inc(i,2); bool:=false; continue;
  252.       end
  253.      else if(c_string_is_operator(gtempstr3,true)>0) or
  254.      (c_string_is_bracket(gstr[i])) then
  255.       begin
  256.        if((res.count>0) and (c_string_is_vaild_value(res.item[res.count-1])=false)
  257.        and (gstr[i]='-')) or ((gstr[i]='-') and (res.count=0)) then
  258.         begin
  259.          inc(res.count);
  260.          SetLength(res.item,res.count);
  261.          res.item[res.count-1]:=gtempstr3;
  262.          inc(i); bool:=true; continue;
  263.         end
  264.        else if(res.count>0) and (c_string_is_vaild_value(res.item[res.count-1])=true)
  265.        and (gstr[i]='.') and (not c_string_is_bracket(gstr[i])) then
  266.         begin
  267.          res.item[res.count-1]:=res.item[res.count-1]+gtempstr3;
  268.          inc(i); bool:=true; continue;
  269.         end;
  270.        inc(res.count);
  271.        SetLength(res.item,res.count);
  272.        res.item[res.count-1]:=gtempstr3;
  273.        j:=i+1;
  274.        while(j<=len) and (gstr[j]=' ') do inc(j);
  275.        if(j<=len) and (gstr[j]=#10) then
  276.         begin
  277.          SetLength(res.status,res.count);
  278.          res.status[res.count-1]:=2;
  279.         end
  280.        else if(j<=len) and (j>i+1) then
  281.         begin
  282.          SetLength(res.status,res.count);
  283.          res.status[res.count-1]:=1;
  284.         end
  285.        else if(j>len) or (gstr[j]>' ') then
  286.         begin
  287.          SetLength(res.status,res.count);
  288.          res.status[res.count-1]:=0;
  289.         end;
  290.        inc(i); bool:=false; continue;
  291.       end;
  292.     end;
  293.    if(i>len) then break;
  294.    if(c_string_is_vaild_char(gstr[i])) then
  295.     begin
  296.      if(bool=false) then
  297.       begin
  298.        inc(res.count);
  299.        SetLength(res.item,res.count);
  300.        res.item[res.count-1]:=gtempstr3;
  301.        bool:=true;
  302.       end
  303.      else
  304.       begin
  305.        res.item[res.count-1]:=res.item[res.count-1]+gtempstr3;
  306.       end;
  307.      if(i=len) then
  308.       begin
  309.        SetLength(res.status,res.count);
  310.        res.status[res.count-1]:=0;
  311.       end;
  312.      inc(i); continue;
  313.     end
  314.    else if(c_string_is_vaild_char(gstr[i])=false) and (bool=true) then
  315.     begin
  316.      j:=i;
  317.      while(j<=len) and (gstr[j]=' ') do inc(j);
  318.      if(j<=len) and (gstr[j]=#10) then
  319.       begin
  320.        SetLength(res.status,res.count);
  321.        res.status[res.count-1]:=2;
  322.       end
  323.      else if(j<=len) and (j>i) then
  324.       begin
  325.        SetLength(res.status,res.count);
  326.        res.status[res.count-1]:=1;
  327.       end
  328.      else if(j>len) then
  329.       begin
  330.        SetLength(res.status,res.count);
  331.        res.status[res.count-1]:=0;
  332.       end;
  333.      bool:=false; inc(i); continue;
  334.     end;
  335.    inc(i);
  336.   end;
  337.  Result.count:=res.count;
  338.  SetLength(Result.item,res.count);
  339.  for i:=1 to res.count do Result.item[i-1]:=res.item[i-1];
  340.  SetLength(Result.status,res.count);
  341.  for i:=1 to res.count do Result.status[i-1]:=res.status[i-1];
  342.  SetLength(res.item,0); SetLength(res.status,0); res.count:=0;
  343. end;
I encountered the error when testing my program using binutils/gas/app.c as source code file and binutils as the include,this program's error position is marked above,you can find the function in the whole code.As you see,I met the error when the program run a half,if you test the function solely,you cannot met this error.
You demand compiling this source code file and adding LazUtils dependencies to the project when testing my source code attached.
« Last Edit: February 08, 2025, 03:06:21 pm by TYDQ »

MarkMLl

  • Hero Member
  • *****
  • Posts: 8314
Re: Why my program crashed due to Access Violation when operating string type?
« Reply #1 on: February 08, 2025, 03:18:27 pm »
Code: Pascal  [Select][+][-]
  1.    if(i<=len-2) then gtempstr1:=Copy(gstr,i,3) else gtempstr1:=''; //That is where ERROR occurs.
  2.  

What are the values of i and len at the point of failure, and how many characters are in gstr?

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

TYDQ

  • Full Member
  • ***
  • Posts: 115
Re: Why my program crashed due to Access Violation when operating string type?
« Reply #2 on: February 08, 2025, 03:21:22 pm »
Code: Pascal  [Select][+][-]
  1.    if(i<=len-2) then gtempstr1:=Copy(gstr,i,3) else gtempstr1:=''; //That is where ERROR occurs.
  2.  

What are the values of i and len at the point of failure, and how many characters are in gstr?

MarkMLl
gstr:='(==)'; i:=2; len:=4;
It seems that the code must be correct,but you must test this program just typing 1,binutils/gas/app.c,binutils,program,out.pas and encountered this strange error when the program converted c tree to convert c tree to pascal tree and met this error.

TYDQ

  • Full Member
  • ***
  • Posts: 115
Re: Why my program crashed due to Access Violation when operating string type?
« Reply #3 on: February 08, 2025, 03:29:55 pm »
Code: Pascal  [Select][+][-]
  1.    if(i<=len-2) then gtempstr1:=Copy(gstr,i,3) else gtempstr1:=''; //That is where ERROR occurs.
  2.  

What are the values of i and len at the point of failure, and how many characters are in gstr?

MarkMLl
Tips:My Lazarus version is 4.99,fpc version is 3.3.1

 

TinyPortal © 2005-2018