I found a bug that had been sitting in my program for a long time, and the compiler did not give any run-time errors for it. The compiler does not perform a range check when assigning a value from the Variant variable. Be careful!
I found a bug that had been sitting in my program for a long time, and the compiler did not give any run-time errors for it. The compiler does not perform a range check when assigning a value from the Variant variable. Be careful!
Demo code:
program VariantError;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils;
var
v: Variant;
w: Word;
begin
v := $10000;
w := v; // No Range check error
Writeln(w); // "0" ???
end.
https://quality.embarcadero.com/browse/RSP-20956
https://quality.embarcadero.com/browse/RSP-20956
Demo code:
program VariantError;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils;
var
v: Variant;
w: Word;
begin
v := $10000;
w := v; // No Range check error
Writeln(w); // "0" ???
end.
https://quality.embarcadero.com/browse/RSP-20956
https://quality.embarcadero.com/browse/RSP-20956
Carlos Barreto Feitoza Filho In that place of code there can be numbers outside the range of type Word. Therefore, the solution is to change the declaration: w: Integer;
ReplyDeleteT n T you mean negative numbers, right? If so, then the solution is to use the integer (signed) If negative numbers is not a problem, use DWORD instead
ReplyDeleteCarlos Barreto Feitoza Filho Yes, it's a right solution.
ReplyDeleteAs I can see, RSP-20956 is opened already, so we can hope that the Delphi compiler will become more useful in such cases.