View Issue Details

IDProjectCategoryView StatusLast Update
0036740FPCCompilerpublic2020-03-03 15:51
ReporterCudaText man Assigned To 
PrioritynormalSeverityminorReproducibilityalways
Status newResolutionopen 
PlatformLinux x64 
Product Version3.3.1 
Summary0036740: It's possible to pass weird pointer as "array" parameter
DescriptionFree Pascal Compiler version 3.3.1-r43118 [2019/10/04] for x86_64

It's possible to pass weird pointer to array's element as "const ar: TIntArray" param!
This gave me work today to find a bug in CudaText.

Run the proj, it gives crap
[0]: 40
[1]: 50
[2]: 60
[3]: 70
[4]: 80
[5]: 90
[6]: 1278017
[7]: 0
[8]: 65536
[9]: 32660
[10]: 1
[11]: 0
[12]: 2
[13]: 0
[14]: 13361
[15]: 0
[16]: 0
[17]: 0
[18]: 0
[19]: 0
[20]: 0
user@user-Notebook:~/Documents/tst-int-array-fpc3$
TagsNo tags attached.
Fixed in Revision
FPCOldBugId
FPCTarget
Attached Files

Activities

CudaText man

2020-02-27 20:06

reporter  

project1.lpr (514 bytes)   
program project1;

{$mode objfpc}{$H+}

uses sysutils;

type
  TIntArray = array of integer;

procedure TestAr(const ar: TIntArray; var res: TIntArray);
var
  i: integer;
begin
  SetLength(res, 2);
  res[0]:= 2;
  res[1]:= 3;

  for i:= 0 to Length(ar)-1 do
    WriteLn('['+inttostr(i)+']: '+inttostr(ar[i]));
end;

var
  ar, res: TIntArray;
  i: integer;
begin
  SetLength(ar, 10);
  for i:= 0 to high(ar) do
     ar[i]:= i*10;

  //it's possible to pass weird pointer to procedure!
  TestAr(@ar[4], res);

end.

project1.lpr (514 bytes)   

Dmitry Boyarintsev

2020-02-29 06:53

developer   ~0121271

Last edited: 2020-02-29 06:54

View 3 revisions

Safer version of the project.

Unfortunately there's no version of Slice() that can take the offset index

project1.pas (656 bytes)   
program project1;

{$mode objfpc}{$H+}

uses sysutils;

type
  TIntArray = array of integer;
  TArrayOfInteger = array [word] of Integer;
  PArrayOfInteger = ^TArrayOfInteger;

procedure TestAr(const ar: array of integer; var res: TIntArray);
var
  i: integer;
begin
  writeln('length=',length(ar));
  SetLength(res, 2);
  res[0]:= 2;
  res[1]:= 3;

  for i:= 0 to Length(ar)-1 do
    WriteLn('['+inttostr(i)+']: '+inttostr(ar[i]));
end;

var
  ar, res: TIntArray;
  i: integer;
begin
  SetLength(ar, 10);
  for i:= 0 to high(ar) do
     ar[i]:= i*10;

  TestAr(Slice(PArrayOfInteger(@ar[4])^, high(ar)-4+1), res);
end.

project1.pas (656 bytes)   

Serge Anvarov

2020-02-29 11:03

reporter   ~0121274

An error should be issued, such as in Delphi: Incompatible types: 'TIntArray' and 'Pointer'

Sven Barth

2020-03-02 09:51

manager   ~0121305

@Dmitry Boyarintsev: Why not use the slice operator instead of the Slice intrinsic?

=== code begin ===

TestAr(ar[4..High(ar)], res);

=== code end ===

Dmitry Boyarintsev

2020-03-02 14:33

developer   ~0121314

Last edited: 2020-03-02 14:37

View 2 revisions

@Sven Barth.
poor PR of ".." operator. if I knew it, I'd likely suggest it.
I didn't even know it existed (even though it's available in 3.0.4)
Don't see it in operators list either https://www.freepascal.org/docs-html/ref/refse84.html#x147-16900012.8
(and if it's not there, may be a function-like to be introduced? How about overloaded version of Slice(), i.e. Slice(arr, ofs, count))

Another potential problem - the operator (or overloaded Slice) is not delphi compatible anyway.
For libraries that target both compilers that might be a sort of an issue.

Sven Barth

2020-03-02 21:49

manager   ~0121321

Well, it's not really an operator per se, because it's not overloadable. However it's mentioned in the context of open array parameters: https://www.freepascal.org/docs-html/ref/refsu68.html#x180-20200014.4.5

Dmitry Boyarintsev

2020-03-03 01:39

developer   ~0121331

@Sven
Since Slice() exists in Delphi (even though it seems to have more limitations than FPC slice), it makes sense to look for SliceEx or SliceOfs. Just for something that's near by.
Can both options exist?

Sven Barth

2020-03-03 15:51

manager   ~0121340

Technically both options can exist, but practically I see no use as we already have something for this.

Issue History

Date Modified Username Field Change
2020-02-27 20:06 CudaText man New Issue
2020-02-27 20:06 CudaText man File Added: project1.lpr
2020-02-29 06:53 Dmitry Boyarintsev File Added: project1.pas
2020-02-29 06:53 Dmitry Boyarintsev Note Added: 0121271
2020-02-29 06:54 Dmitry Boyarintsev Note Edited: 0121271 View Revisions
2020-02-29 06:54 Dmitry Boyarintsev Note Edited: 0121271 View Revisions
2020-02-29 11:03 Serge Anvarov Note Added: 0121274
2020-03-02 09:51 Sven Barth Note Added: 0121305
2020-03-02 14:33 Dmitry Boyarintsev Note Added: 0121314
2020-03-02 14:37 Dmitry Boyarintsev Note Edited: 0121314 View Revisions
2020-03-02 21:49 Sven Barth Note Added: 0121321
2020-03-03 01:39 Dmitry Boyarintsev Note Added: 0121331
2020-03-03 15:51 Sven Barth Note Added: 0121340