Recent

Author Topic: [CLOSED] Code cleanup at procedure fpc_dynarray_setlength  (Read 776 times)

lagprogramming

  • Sr. Member
  • ****
  • Posts: 407
[CLOSED] Code cleanup at procedure fpc_dynarray_setlength
« on: February 08, 2024, 12:37:04 pm »
procedure fpc_dynarray_setlength(var p : pointer;pti : pointer;
  dimcount : sizeint;dims : pdynarrayindex);[Public,Alias:'FPC_DYNARR_SETLENGTH']; compilerproc;
contains the following lines:
Code: Pascal  [Select][+][-]
  1. ...
  2. newp:=getmem(size);
  3. fillchar(newp^,sizeof(tdynarray),0);
  4. ...
  5. newp^.refcount:=1;
  6. newp^.high:=dims[0]-1;
  7. ...

Because
Code: Pascal  [Select][+][-]
  1. tdynarray = { packed } record
  2.   refcount : ptrint;
  3.   high : tdynarrayindex;
  4. end;
the "fillchar(newp^,sizeof(tdynarray),0);" line is equivalent to "newp^.refcount:=0;newp^.high:=0;". Later in code we have "newp^.refcount:=1;newp^.high:=dims[0]-1;" which makes the presence of the fillchar line useless.
The following patch removes the fillchar line.
« Last Edit: February 11, 2024, 10:05:23 am by lagprogramming »

PascalDragon

  • Hero Member
  • *****
  • Posts: 5823
  • Compiler Developer
Re: Code cleanup at procedure fpc_dynarray_setlength
« Reply #1 on: February 08, 2024, 09:11:59 pm »
The following patch removes the fillchar line.

This is called safe programming in case the record should ever be extended and also to ensure that any potential padding bytes are cleared as well.

 

TinyPortal © 2005-2018