Forum > General
Q about subverting type compatibility
440bx:
Hello,
Consider the following program:
--- 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";}};} ---{$APPTYPE CONSOLE} program _dword_qword; function Va32(InAddress : DWORD) : DWORD;begin result := InAddress + 1;end; function Va64(InAddress : qword) : qword;begin result := InAddress + 1;end; var TestVar : qword; begin writeln; TestVar := (high(qword) shr 32) shl 32; writeln(Va32(TestVar)); { is there a way to make this fail ? } writeln(Va64(TestVar)); writeln('press enter/return to end this program'); readln;end. The call to Va32 is being passed a qword which is automatically truncated by the compiler to a dword. Is there a way to tell FPC to emit an error in this case ? IOW, is it possible to have FPC reject an argument that needs to be truncated to fit the parameter size ?
Thank you for your help.
Thaddy:
Range checks on....
--- 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";}};} ---{$APPTYPE CONSOLE}{$mode objfpc} {$R+}program _dword_qword; function Va32(InAddress : DWORD) : DWORD;begin result := InAddress + 1;end; function Va64(InAddress : qword) : qword;begin result := InAddress + 1;end; var TestVar : qword; begin writeln; TestVar := (high(qword) shr 32) shl 32; writeln(Va32(TestVar)); { is there a way to make this fail ? } writeln(Va64(TestVar)); writeln('press enter/return to end this program'); readln;end.It is the compiler, not me...
--- Code: Bash [+][-]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";}};} ---(24,23) Error: Range check error while evaluating constants (18446744069414584320 must be between 0 and 4294967295)Now say 'duh' a couple of times.. :D
Editor is Geany.
Btw, range checks are local, so with push/pop you can protect a specific bit of code if required.
Khrys:
--- Quote from: Thaddy on October 10, 2024, 06:05:50 am ---Range checks on....
--- End quote ---
This works at runtime, but is there a way to make FPC throw a compile-time error on implicit integer-integer conversions?
For example, Rust is extremely strict when it comes to this, so the following won't compile unless an explicit cast or conversion is added:
--- Code: C [+][-]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";}};} ---fn triple(x: i64) -> i32 { x * 3}
440bx:
As Khrys mentioned, that only works at runtime which is NOT what the question is about.
The question is about having the compiler reject the qword parameter in the first call... duh !!!
Thaddy:
No, in this case it works at compile time, duh. see screenshot.
That is the code that you provided.
(did not care to test it, did you)
The red line is in Geany that jumps to the compiler error as I pointed out.
In the case of your code it is a compile time error, but in other scenario's range checks can be runtime errors.
Here that is not the case. Better check your eyes. :P
More in general, the compiler - almost - always issues a warning or hint and you can promote that warning to error.
That will catch most truncations at compile time.
some of these related to range checks but not all of them are e.g.
--- 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";}};} ---{$warn 3036 error}{$warn 3353 error}{$warn 4036 error}{$warn 4048 error}{$warn 4109 error}{$warn 4110 error}//your case, the compiler told you so, but a compile time error in{$R+} modeYou can add to these when you encounter them.
Or the crude way: -Sewh :D
Duh.. 8-)
I really should write a wiki entry for that, sometime.
(Although I must say there are possible - rare - exceptions to the above where the compiler misses it)
Navigation
[0] Message Index
[#] Next page