Skip to content

Pointer helper causes a dereference to get missed

Original Reporter info from Mantis: runewalsh
  • Reporter name:

Description:

Code from ‘Steps to reproduce’ prints for me, both on x86-32 and x86-64, and seemingly independent on compiler options:

(1) (PPointer(ptrs) + 1)^.ToString:
0040D014 (?!)
(2) HexStr((PPointer(ptrs) + 1)^):
00000456
(3) ptrs[1].ToString:
00000456

@ptrs = 0040D010

As far as I understand the Free Pascal language itself, all of the 3 are equivalent and can be expected to print the hexadecimal value of ptrs[1], which is 456. Yet the (1) looks as though it missed a dereference; note the ‘@ptrs’ value.

Steps to reproduce:

{$mode objfpc} {$h+} {$modeswitch typehelpers}

type
	PointerHelper = type helper for pointer
		function ToString: string;
	end;

	function PointerHelper.ToString: string;
	begin
		result := HexStr(self);
	end;

var
	ptrs: array[0 .. 1] of pointer;

begin
	ptrs[0] := pointer($123);
	ptrs[1] := pointer($456);
	writeln('(1) (PPointer(ptrs) + 1)^.ToString: ', (PPointer(ptrs) + 1)^.ToString, ' (?!)');
	writeln('(2) HexStr((PPointer(ptrs) + 1)^):  ', HexStr((PPointer(ptrs) + 1)^));
	writeln('(3) ptrs[1].ToString:               ', ptrs[1].ToString);
	writeln(LineEnding, '@ptrs = ', HexStr(PPointer(ptrs)));
end.

Mantis conversion info:

  • Mantis ID: 39169
  • Build: r49579 [2021/07/06]
  • Version: 3.3.1
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information