Recent

Author Topic: fpc_help_destructor related  (Read 2656 times)

lagprogramming

  • Full Member
  • ***
  • Posts: 159
fpc_help_destructor related
« on: October 28, 2015, 10:34:13 pm »
fpc_help_destructor can be found within generic.inc

Code: Pascal  [Select]
  1. procedure fpc_help_destructor(_self,_vmt:pointer;vmt_pos:cardinal);[public,alias:'FPC_HELP_DESTRUCTOR'];  compilerproc;
  2. begin
  3.    { already released? }
  4.    if (_self=nil) or
  5.       (_vmt<>pointer(-1)) or
  6.       (ppointer(_self+vmt_pos)^=nil) then
  7.      exit;
  8.    if (pobjectvmt(ppointer(_self+vmt_pos)^)^.size=0) or
  9.       (pobjectvmt(ppointer(_self+vmt_pos)^)^.size+pobjectvmt(ppointer(_self+vmt_pos)^)^.msize<>0) then
  10.      RunError(210);
  11.    { reset vmt to nil for protection }
  12.    ppointer(_self+vmt_pos)^:=nil;
  13.    freemem(_self);
  14. end;

   The following line brought attention to me.
Code: Pascal  [Select]
  1. if (pobjectvmt(ppointer(_self+vmt_pos)^)^.size=0) or
  2.    (pobjectvmt(ppointer(_self+vmt_pos)^)^.size+pobjectvmt(ppointer(_self+vmt_pos)^)^.msize<>0) then
  3.    RunError(210);

   Both .size and .msize are unsigned integers. This means that we should always hit the "RunError(210);". Am I wrong?

Jonas Maebe

  • Hero Member
  • *****
  • Posts: 669
Re: fpc_help_destructor related
« Reply #1 on: October 28, 2015, 11:32:40 pm »
FPC evaluates arithmetic using the native integer size. Those values have the native integer size and have values x and -x. Whether they are treated as signed or unsigned values doesn't matter, because in two's complement arithmetic using the native integer size, the sum will always be zero.

lagprogramming

  • Full Member
  • ***
  • Posts: 159
Re: fpc_help_destructor related
« Reply #2 on: October 30, 2015, 02:10:58 pm »
FPC evaluates arithmetic using the native integer size. Those values have the native integer size and have values x and -x. Whether they are treated as signed or unsigned values doesn't matter, because in two's complement arithmetic using the native integer size, the sum will always be zero.

   I'm still confused. How are overflow checks managed there, does "compilerproc" automatically turn off overflow checking?
I think that with overflow checking turned on, "ppointer(_self+vmt_pos)^:=nil;" would never get executed.
What assures that this check type is turned off there?

   Example:

Code: Pascal  [Select]
  1. function test(const a,b:sizeuint):boolean;
  2. begin
  3.   result:=(a+b=0);//Fails with overflow checks turned on
  4. end;

   when called with:

Code: Pascal  [Select]
  1. var
  2.   x,y: sizeuint;
  3. begin
  4.   x:=1;
  5.   y:=sizeint(-1);
  6.   test(x,y);
  7. end;

Jonas Maebe

  • Hero Member
  • *****
  • Posts: 669
Re: fpc_help_destructor related
« Reply #3 on: October 30, 2015, 02:39:12 pm »
The system unit is compiled without range and overflow checking, see systemh.inc.

lagprogramming

  • Full Member
  • ***
  • Posts: 159
Re: fpc_help_destructor related
« Reply #4 on: October 30, 2015, 05:49:45 pm »
The system unit is compiled without range and overflow checking, see systemh.inc.

I've searched for the compiler directive within wrong files.  %) Now everything is crystal clear.
Thank you Jonas!