Can you spot the error in the following (somewhat artificial) Delphi code?
.. function GetNumberOfGuests : Cardinal; var Num : Cardinal; begin .. Result := Num; end; function CalcRevenue : Integer; var Guests : Integer; begin .. Guests := GetNumberOfGuests; .. end; ..
This was quite easy, right?
Solution: The problem is the local variable Guests. It is declared as an Integer. But it is assigned by the result from a call to GetNumberOfGuests. This function returns a Cardinal. So the value which is returned may be out-of-range for an Integer. Remember, an Integer is defined as a number -2147483648..2147483647, whereas a Cardinal is a number 0..4294967295. At runtime this may give a range check error if range checking is turned on ($R+). Otherwise the error will go unnoticed and Guests will just get an incorrect value.
The good news is that with Pascal Analyzer, our popular static code analyzer products can detect these situations automatically. There is a section WARN52 in the Warnings Report that does this. It is called "Possible bad assignment".
Not so hard to spot! But imagine finding this problem among thousands of lines of code spread over hundreds of units! And consider also a code base that is evolving all the time, so the code must constantly be rechecked.
Pascal Analyzer has a total of 53 reports. Those are divided into over 230 sections, all providing statistics, references, errors and warnings for your code. Pascal Analyzer can be run as a standalone Windows application, or as a command-line program. The second choice is suitable if you want to integrate static code analysis with your build process.
Or use Pascal Expert, our RAD Studio plugin, to find the problems immediately while coding. Pascal Expert contains much of the same capabilities like Pascal Analyzer, and is obtainable with a discount when buying both products.
Can you see the Delphi coding error? (Part 6) Can you see the Delphi coding error? (Part 5) Can you see the Delphi coding error? (Part 4) Can you see the Delphi coding error? (Part 3) Can you see the Delphi coding error? (Part 2) Can you see the Delphi coding error? (Part 1)