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

Comments

  1. 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;

    ReplyDelete
  2. T 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

    ReplyDelete
  3. Carlos Barreto Feitoza Filho Yes, it's a right solution.
    As I can see, RSP-20956 is opened already, so we can hope that the Delphi compiler will become more useful in such cases.

    ReplyDelete

Post a Comment