Fpsonar expressions

← Back to fpsonar

Twenty-one rules about values: constants that do not fit, arithmetic that overflows, comparisons whose answer is fixed before the program runs, and casts that cannot succeed.

Most of the constant rules are critical and off by default. Critical because when they fire they have computed the problem rather than guessed it — the constant really does not fit. Off by default only because they are new.

Constants that do not fit

edit

ConstantOutOfRangeForTarget

edit

critical · off by default

A constant assigned to a type that cannot hold it. The value is silently truncated at run time, or the compiler rejects it — depending on mode and target.

fpsonar complains about this

var
  B: Byte;
begin
  B := 300;                   // Byte holds 0..255
end;

fpsonar accepts this

var
  B: Word;
begin
  B := 300;
end;

ConstantOverflowInExpression

edit

critical · off by default

A constant expression whose result exceeds the range of the type it is computed in. Pascal evaluates constant arithmetic at compile time, and overflow there wraps quietly.

fpsonar complains about this

const
  Total = High(Integer) + 1;

fpsonar accepts this

const
  Total = Int64(High(Integer)) + 1;

DivisionByZeroConstant

edit

critical · off by default

div, mod or / with a constant zero divisor. Guaranteed to fail, and no input can save it.

fpsonar complains about this

Average := Total div 0;

fpsonar accepts this

if Count > 0 then
  Average := Total div Count;

ShiftCountExceedsWidth

edit

critical · off by default

Shifting by more bits than the type has. The result is not "zero" as people often assume — it is undefined and varies by CPU.

fpsonar complains about this

var
  X: LongWord;                // 32 bits
begin
  X := X shl 40;
end;

fpsonar accepts this

var
  X: QWord;                   // 64 bits
begin
  X := X shl 40;
end;

ArrayIndexConstantOutOfBounds

edit

critical · off by default

A constant index outside the declared bounds. Without range checking this reads or writes memory belonging to something else.

fpsonar complains about this

var
  A: array[0..3] of Integer;
begin
  A[10] := 0;
end;

fpsonar accepts this

var
  A: array[0..15] of Integer;
begin
  A[10] := 0;
end;

EnumOrdinalOutOfRange

edit

critical · off by default

Casting an integer to an enumeration whose declared values do not include it. The resulting value is not a valid member, so every case over it falls through and Ord round-trips produce nonsense.

fpsonar complains about this

type
  TLevel = (lvLow, lvMid, lvHigh);   // ordinals 0..2
begin
  L := TLevel(7);
end;

fpsonar accepts this

if (I >= Ord(Low(TLevel))) and (I <= Ord(High(TLevel))) then
  L := TLevel(I);

SetElementOutOfRange

edit

critical · off by default

A set constructor containing a value the tested operand's type cannot hold, so the membership test can never be true.

fpsonar complains about this

var
  B: Byte;
begin
  if B in [0..300] then       // Byte cannot exceed 255
    Handle;
end;

fpsonar accepts this

if B in [0..255] then
  Handle;

ComparisonAlwaysTrueForType

edit

major · off by default

A comparison whose answer is fixed by the range of the type, not by the value. A Byte is never above 255 and an unsigned value is never below zero, so these tests are dead code — and the check the author intended is missing.

fpsonar complains about this

var
  B: Byte;
begin
  if B > 255 then             // never true
    Reject;
end;

fpsonar accepts this

var
  B: Integer;
begin
  if B > 255 then
    Reject;
end;

ConstantConditionAlwaysTrueOrFalse

edit

major · off by default

An if or loop condition that folds to a fixed Boolean. In a long-lived codebase this often surfaces stale branches left behind when a constant changed.

fpsonar complains about this

const
  Debugging = False;
begin
  if Debugging or True then   // always true
    Trace;
end;

fpsonar accepts this

if Debugging then
  Trace;

Arithmetic that loses information

edit

IntegerDivisionAssignedToFloat

edit

major · off by default

div truncates before the assignment happens, so assigning its result to a float keeps the truncation. 7 div 2 is 3, and 3.0 is not what a float target suggests was wanted.

fpsonar complains about this

var
  Ratio: Double;
begin
  Ratio := Total div Count;   // truncated, then widened
end;

fpsonar accepts this

Ratio := Total / Count;

FloatEqualityComparison

edit

major · off by default

Floating-point values are approximations, so = between them is a coin toss — 0.1 + 0.2 is not 0.3. Compare against a tolerance.

fpsonar complains about this

if Total = 0.0 then
  Handle;

fpsonar accepts this

if Abs(Total) < 1E-9 then
  Handle;

MixedSignedUnsignedComparison

edit

major · off by default

Comparing a signed against an unsigned integer of the same width means one operand is converted, and a negative value becomes a very large positive one. -1 > High(LongWord) becomes true.

fpsonar complains about this

var
  I: LongInt;
  U: LongWord;
begin
  if I < U then
    Handle;
end;

fpsonar accepts this

if Int64(I) < Int64(U) then
  Handle;

SizeOfOnReferenceType

edit

major · off by default

SizeOf of a class, interface, dynamic array or long string gives the size of the reference — the pointer — not of the data. Usually spotted when a buffer turns out to be 8 bytes.

fpsonar complains about this

var
  L: TStringList;
begin
  FillChar(L, SizeOf(L), 0);  // 8 bytes: the reference
end;

fpsonar accepts this

var
  R: TMyRecord;               // a value type
begin
  FillChar(R, SizeOf(R), 0);
end;

MoveFillCharSizeMismatch

edit

critical · off by default

Move and FillChar count bytes. Passing an element count, or the size of a pointer instead of the data, copies or clears the wrong amount — the classic buffer overrun in Pascal.

fpsonar complains about this

Move(Src[0], Dst[0], Length(Src));       // elements, not bytes

fpsonar accepts this

Move(Src[0], Dst[0], Length(Src) * SizeOf(Src[0]));

Casts

edit

ObjectCastNotInHierarchy

edit

major · on by default

A hard cast between class types with no ancestry between them can never succeed. A hard cast does not check, so the program carries on with a reference of the wrong type until it touches something that is not there.

fpsonar complains about this

Reader := TFileReader(Writer);     // unrelated classes

fpsonar accepts this

if Writer is TFileReader then
  Reader := TFileReader(Writer);

RedundantCast

edit

minor · on by default

Casting a value to the type it already has.

fpsonar complains about this

N := Integer(Count);          // Count is already Integer

fpsonar accepts this

N := Count;

PlatformDependentCast

edit

major · on by default

Casting between a pointer and a fixed-width integer. On 64-bit the pointer is wider than a LongInt, so the top half is lost. Use PtrInt or PtrUInt, which are defined to be pointer-sized on every target.

fpsonar complains about this

N := LongInt(P);

fpsonar accepts this

N := PtrInt(P);

PlatformDependentTruncation

edit

major · on by default

Narrowing a pointer-sized or 64-bit value into a fixed-width type. Fine on 32-bit, silently wrong on 64-bit — which is how this class of bug survives testing.

fpsonar complains about this

var
  N: LongInt;
begin
  N := Length(BigArray);      // Length is SizeInt
end;

fpsonar accepts this

var
  N: SizeInt;
begin
  N := Length(BigArray);
end;
edit

Casts involving strings and characters — UnicodeToAnsiCast, CharToCharPointerCast — are in strings, because what goes wrong there is about encoding rather than about width. ObjectCastBeforeFree is in memory and lifetime.

See also

edit
  • fpsonar — what fpSonar is, and how to run it

← Back to fpsonar