Forum > FPC development

Possible Bug (Mac M1/AArch)?

(1/3) > >>

Grahame Grieve:
Consider this code:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure foo(s : TStream);var  b : TBytes;begin  SetLength(b, 4000);  s.read(b, 4000);end; My code has plenty of examples of this, and I believe it has always worked for me before (Delphi, and FPC windows, unix, and Mac). But on my current Mac m1 with Lazarus 2.2.5 (rev lazarus_2_2_4-3-g564e1e8244) FPC 3.2.2 aarch64-darwin-cocoa, that results in memory corruption and complete failure of the executable thereafter. If I change it to


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure foo(s : TStream);var  b : TBytes;begin  SetLength(b, 4000);  s.read(b[0], 4000);end;
that works fine. This feels like a compiler bug to me, should I report it? You can see real world fixes here: https://github.com/HealthIntersections/fhirserver/pull/190/files

Leledumbo:
I believe it should not instead. TStream.Read expects untyped parameter, which is treated differently based on actual TStream descendant used. But the semantic should be similar to pure array of bytes, no metadata whatsoever to skip. Therefore, b and b[0] are totally different. The former will point to the start of the TBytes structure, which contains metadata (length, refcount, etc.) while the latter is the start of the data part only, which is what you want.

domasz:

--- Quote from: Grahame Grieve on November 03, 2022, 07:02:48 pm ---

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure foo(s : TStream);var  b : TBytes;begin  SetLength(b, 4000);  s.read(b[0], 4000);end;

--- End quote ---

I've always (over 10 years) used this way in Delphi. This is the only one that makes sense to me.
Also you can read to strings:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---b: AnsiString;[...]S.Read(b[1], 4000);

ASerge:

--- Quote from: domasz on November 03, 2022, 08:29:09 pm ---I've always (over 10 years) used this way in Delphi. This is the only one that makes sense to me.

--- End quote ---
Delphi has the method
--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---TStream.function Read(var Buffer: TBytes; Count: Longint): Longint;But FPC does not have it.

PascalDragon:

--- Quote from: ASerge on November 04, 2022, 05:29:26 am ---
--- Quote from: domasz on November 03, 2022, 08:29:09 pm ---I've always (over 10 years) used this way in Delphi. This is the only one that makes sense to me.

--- End quote ---
Delphi has the method
--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---TStream.function Read(var Buffer: TBytes; Count: Longint): Longint;But FPC does not have it.

--- End quote ---

FPC 3.3.1 has it as well. For 3.2.2 you could introduce a class helper that adds that method after all it only does a Read(Buffer[0], Count) in the end (though yes, it goes through the method that provides the Offset parameter, but that doesn't change the principle).

Navigation

[0] Message Index

[#] Next page

Go to full version