RDC11018 — Integer arithmetic of Abs, Int or Fix that can overflow
Severity: Warning · Family: RDC (Rubberduck Core analyzers) ·
Raised by: ArgumentTypedArithmeticAnalyzer
An addition, a subtraction or a multiplication of whole numbers uses Abs, Int or Fix of a number, and can overflow where the language specification
says it would not.
MS-VBAL declares Abs, Int and Fix As Variant. MS-VBA gives the call the type of the number it is given instead: Fix(n) of an Integer is an
Integer. So Fix(n) * 7000, with n = 5, multiplies two Integer values, and the result does not fit in one: run-time error 6 (Overflow). Had the call been a
Variant, as MS-VBAL declares it, the product would have widened to a Long and been 35000 (recorded in Excel, as vb6.exe does).
RD-VBA does as MS-VBA does, and warns where the difference can show.
How it is raised
| Situation | Example | Resolution |
|---|---|---|
+, - or * of Byte, Integer or Long values, one of which is Abs, Int or Fix of a variable of one of those types. |
Dim n As Integer … x = Fix(n) * 7000 |
Convert to a larger type before the operation: CLng(Fix(n)) * 7000, or declare n as a Long. |
Notes
A call given a Variant, a String, a Boolean or a Date is a Variant, as MS-VBAL declares it, and is not reported. Neither is an operation of Single,
Double or Currency values, which overflows as a Variant operation would.
Sgn returns an Integer and Asc an Integer, as MS-VBAL declares them: an Integer operation of either is what the specification says, and is not
reported.
See also
ACCUEIL • HOME | ℹ️ BIENVENUE • WELCOME | 🧩 BÂTISSONS • BUILD | RD-VBAL | SDK | 🌐 rubberduckvba.ca