Fpsonar dataflow

← Back to fpsonar

Five rules that follow values rather than declarations: a variable read before anything gave it a value, a value written that nothing ever reads, a function result thrown away before the function returns.

These are the only rules in fpsonar that reason about every path through a routine at once — they build a control-flow graph and ask what is true where the paths meet. That is what lets them tell "assigned on one branch" apart from "assigned on all of them".

All five are off by default, and all five report only simple-typed locals: integers, booleans, enumerations, floats, pointers. Managed types — strings, interfaces, dynamic arrays — are initialised by the compiler and never carry the defect these rules look for.

Two properties are worth knowing before you enable them:

  • They stay silent when they lose track. A variable whose address is taken, or that is

passed as a var or out argument, could be written anywhere; none of these rules will

claim it was not.

  • They are per-routine. Nothing follows a value into a called routine or out through a field.

Reading before writing

edit

UninitializedVariable

edit

major · off by default

A local or program global read before any assignment reaches it. Whatever was on the stack is what you get, so the bug reproduces intermittently and changes behaviour between debug and release builds.

fpsonar complains about this

var
  lCount: Integer;
begin
  WriteLn(lCount);           // nothing assigned it

fpsonar accepts this

var
  lCount: Integer;
begin
  lCount := 0;
  WriteLn(lCount);

UninitializedVariableStrict

edit

major · off by default

The stricter sibling: a read of a local that is assigned on one incoming path and not on another. The variable does get a value sometimes, which is exactly why this survives testing — the path that skips the assignment is usually the rare one.

Enable this one or UninitializedVariable depending on how much you want to hear. Strict finds real bugs the plain rule cannot see, and also produces more findings on code where the assignment is guaranteed by logic the analyser cannot follow.

fpsonar complains about this

var
  lCount: Integer;
begin
  if aReady then
    lCount := Load;
  WriteLn(lCount);           // unassigned when aReady is False

fpsonar accepts this

var
  lCount: Integer;
begin
  if aReady then
    lCount := Load
  else
    lCount := 0;
  WriteLn(lCount);

Writing without reading

edit

DeadStore

edit

minor · off by default

A store that every path overwrites before reading. The assignment runs and its value is never observed, so the line can go — but first check why it is there, because a dead store is often a symptom rather than a tidiness issue: the value was meant to be read and something else is reading the wrong thing.

fpsonar complains about this

lTotal := ComputeSlowly;     // overwritten below, never read
lTotal := 0;
for i := 0 to aCount - 1 do
  lTotal := lTotal + Weight(i);

fpsonar accepts this

lTotal := 0;
for i := 0 to aCount - 1 do
  lTotal := lTotal + Weight(i);

SelfAssignedNeverUsed

edit

minor · off by default

A store computed from the variable's own previous value that no path ever reads — lTotal := lTotal + x where nothing afterwards looks at lTotal. The accumulation runs every iteration and the answer is discarded.

Narrower than DeadStore and more suspicious: nobody writes a self-referential update by accident, so the usual cause is a result that stopped being returned.

fpsonar complains about this

for i := 0 to aCount - 1 do
  lTotal := lTotal + Weight(i);
Result := aCount;            // lTotal never read

fpsonar accepts this

for i := 0 to aCount - 1 do
  lTotal := lTotal + Weight(i);
Result := lTotal;

ResultOverwrittenBeforeExit

edit

major · off by default

A store to a function's Result that every returning path overwrites before reading. One of the assignments to Result cannot matter — which usually means a branch that was supposed to Exit does not.

major rather than minor, because unlike an ordinary dead store this one changes what the function returns.

fpsonar complains about this

function Load(const aName: string): Boolean;
begin
  Result := False;
  if not FileExists(aName) then
    Result := False;          // meant to Exit here
  Result := Parse(aName);     // runs even when the file is missing
end;

fpsonar accepts this

function Load(const aName: string): Boolean;
begin
  Result := False;
  if not FileExists(aName) then
    Exit;
  Result := Parse(aName);
end;
edit

Declarations nothing uses at all — as opposed to values nothing reads — are in unused code; WriteOnlyVariable there is the declaration-level counterpart of DeadStore. Code no path can reach is UnreachableCode, in control flow, which is built on the same control-flow graph. Rules that follow object lifetimes along those paths are in memory and lifetime.

See also

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

← Back to fpsonar