浏览代码

* fix #39447: when checking whether the parameters are valid for pointer arithmetic check the *correct* parameter for being valid pointer-like type (I'd say that this was a copy&paste mistake...)
+ added test

Sven/Sarah Barth 2 年之前
父节点
当前提交
c994b5efe8
共有 2 个文件被更改,包括 32 次插入2 次删除
  1. 2 2
      compiler/nadd.pas
  2. 30 0
      tests/webtbs/tw39447.pp

+ 2 - 2
compiler/nadd.pas

@@ -3068,9 +3068,9 @@ implementation
                 if (rt=niln) then
                 if (rt=niln) then
                   CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,'NIL');
                   CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,'NIL');
                 if (not(cs_extsyntax in current_settings.moduleswitches) and not(nf_internal in flags))  or
                 if (not(cs_extsyntax in current_settings.moduleswitches) and not(nf_internal in flags))  or
-                   (not (is_pchar(ld) or is_chararray(ld) or is_open_chararray(ld) or is_widechar(ld) or is_widechararray(ld) or is_open_widechararray(ld)) and
+                   (not (is_pchar(rd) or is_chararray(rd) or is_open_chararray(rd) or is_widechar(rd) or is_widechararray(rd) or is_open_widechararray(rd)) and
                     not(cs_pointermath in current_settings.localswitches) and
                     not(cs_pointermath in current_settings.localswitches) and
-                    not((ld.typ=pointerdef) and tpointerdef(ld).has_pointer_math)) then
+                    not((rd.typ=pointerdef) and tpointerdef(rd).has_pointer_math)) then
                   CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
                   CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
                 if (rd.typ=pointerdef) and
                 if (rd.typ=pointerdef) and
                    (tpointerdef(rd).pointeddef.size>1) then
                    (tpointerdef(rd).pointeddef.size>1) then

+ 30 - 0
tests/webtbs/tw39447.pp

@@ -0,0 +1,30 @@
+unit tw39447;
+{$IFDEF FPC}
+{$MODE Delphi}
+{$ENDIF}
+
+interface
+uses sysutils;
+
+function ptr_twist() : boolean;
+
+implementation
+
+function ptr_twist() : boolean;
+var
+  x : pchar;
+  x2 : pchar;
+const
+  str : ShortString = '123456789';
+begin
+
+  x := @(str[2]);
+  x := x + 5;
+  x2:= 5 + x;             // FPC fails here
+
+  result := (x = x2);
+
+end;
+
+end.
+