View Issue Details

IDProjectCategoryView StatusLast Update
0037341FPCCompilerpublic2020-07-13 15:45
ReporterBoris Matkov Assigned ToSven Barth  
PrioritynormalSeverityminorReproducibilityalways
Status resolvedResolutionno change required 
Product Version3.2.0 
Summary0037341: Regression: invalid warning message
DescriptionRegression: invalid warning message
Steps To ReproduceTry to compile this function:

function GetByteArray: TBytes;
begin
  SetLength(Result, 3);
  Result[0] := 1;
  Result[1] := 2;
  Result[2] := 3;
end;

In FPC 3.0.4 - all OK, but in FPC 3.2.0 I get the warning message:
Warning: function result variable of a managed type does not seem to be initialized
TagsNo tags attached.
Fixed in Revision
FPCOldBugId
FPCTarget-
Attached Files

Relationships

related to 0035475 closedSven Barth Unnecessary warning for functions that return dynamic arrays in the new version of FPC 

Activities

Sven Barth

2020-07-13 11:38

manager   ~0123961

The message is correct. Due to how managed result types are passed the Result can have valid data and SetLength does not change any eventual content in there. This is something that can also happen in Delphi. See my comment here: 0035475:0121253

Benito van der Zander

2020-07-13 14:42

reporter   ~0123980

That message is clearly not correct for that function.

The problem is that FPC confuses Result and Result[0]

Result[0] is uninitialized

Result itself is perfectly initialized. The whole point of calling SetLength is to initialize result.


It needs to warn if you read from result[0], but NOT when you write to it

runewalsh

2020-07-13 15:45

reporter   ~0123983

SetLength forces the array to be unique (have refcount = 1), so the warning is indeed incorrect. I personally have disabled it long ago due to this.

    type
        TLongintArray = array of longint;

        function Test1: TLongintArray;
        begin
            SetLength(result, 3);
        end;

        function Test2: TLongintArray;
        begin
            SetLength(result, 3);
        end;

    var
        s: TLongintArray;

    begin
        s := Test1;
        writeln('Test1 returned ' + HexStr(pointer(s)));
        s := Test2;
        writeln('Test2 returned ' + HexStr(pointer(s)));
    end.

Prints (for example):
    Test1 returned 00000000000AA8B0
    Test2 returned 00000000000AA970

See? SetLength acts as an initialization.

Issue History

Date Modified Username Field Change
2020-07-13 09:33 Boris Matkov New Issue
2020-07-13 11:38 Sven Barth Assigned To => Sven Barth
2020-07-13 11:38 Sven Barth Status new => resolved
2020-07-13 11:38 Sven Barth Resolution open => no change required
2020-07-13 11:38 Sven Barth FPCTarget => -
2020-07-13 11:38 Sven Barth Note Added: 0123961
2020-07-13 11:38 Sven Barth Relationship added related to 0035475
2020-07-13 14:42 Benito van der Zander Note Added: 0123980
2020-07-13 15:45 runewalsh Note Added: 0123983