Delphi-RAG-Lint
Delphi tooling · Lint rules

Twelve Bugs the Compiler Lets Through

drag-lint 1.65 adds twelve on-by-default rules for Delphi mistakes that compile without a single warning: swapped FillChar arguments, a Byte loop counter, a thread configured after it started, SetFocus in OnCreate and eight more. We wrote one small project with all twelve in it. The Delphi 13 compiler builds it with no errors, no warnings and no hints; drag-lint reports twelve findings.

Availability

The twelve rules are new in drag-lint 1.65.0-alpha, released 2026-12-04, and on by default. Every output below is real output of 1.65.0-alpha on the two-unit fixture in section 01.

Each of these mistakes has cost someone an afternoon in a debugger, and they are typical of a code base that has grown over many years, like Micronite, the quality and production-control software for manufacturers that drag-lint is built next to. None of them is exotic. They all look right at a glance, they all compile, and most of them work in testing: the buffer is big enough, the list has fewer than 256 items, the thread is slow to start. Then one day it is not.

01 — The fixture

Two units, twelve mistakes, a clean build

The fixture is a program with two units: uSamples.pas holds eleven of the mistakes, uLogin.pas a login form whose form file binds FormCreate as OnCreate. The Win64 compiler, with its default warnings, has nothing to say:

> dcc64 -B Samples.dpr
Embarcadero Delphi for Win64 compiler version 37.0
164 lines, 0.41 seconds, 3081764 bytes code, 325284 bytes data.

drag-lint lints with its index, which knows the declared size of every variable, the class of every thread, and which handler a form file binds. Index the folder once, then lint each unit:

> drag-lint index . --db samples.sqlite
Done. Files: 4, Symbols: 44, Refs: 88, 0.33s, parse-errors=0
> drag-lint lint uSamples.pas --db samples.sqlite
...
11 finding(s)
> drag-lint lint uLogin.pas --db samples.sqlite
uLogin.pas:20:3  [warning] setfocus-in-formcreate: SetFocus in the form's OnCreate handler: the form is not visible yet, so this raises EInvalidOperation -- set ActiveControl instead
1 finding(s)

The sections below take the findings in four groups. Each shows the line and the finding as drag-lint printed it.

02 — Memory

Byte counts that are wrong

FillChar takes the count before the fill value, unlike memset. Written the other way round it fills zero bytes and returns:

var
  Buf: array[0..63] of Byte;
...
  FillChar(Buf, 0, SizeOf(Buf));
uSamples.pas:71:3  [warning] fillchar-args-swapped: FillChar / FillMemory arguments swapped: the count is 0 and the fill value is a size, so nothing is filled (CWE-665). Write FillChar(Buf, SizeOf(Buf), 0).

A Move sized from the source instead of the destination writes past the end of a smaller buffer. Here 64 bytes go into a 16-byte local, over whatever the stack holds next to it:

var
  Hdr: array[0..15] of Byte;
begin
  Move(Buf, Hdr, SizeOf(Buf));
uSamples.pas:78:3  [warning] memory-op-count-exceeds-buffer: The byte count is larger than the destination buffer: the operation writes past the end of it

SizeOf of a TBytes, a string or an interface is the size of a pointer, 8 bytes on Win64, whatever the data holds:

procedure CopyPayload(const Payload: TBytes);
var
  Raw: array[0..255] of Byte;
begin
  Move(Payload[0], Raw, SizeOf(Payload));
uSamples.pas:124:3  [warning] sizeof-managed-type: SizeOf of a string / dynamic array / interface variable is the size of its POINTER (4 or 8), not of the data it refers to: used as a byte count it copies, fills or streams the wrong amount (CWE-467). Use Length(X) * SizeOf(<element>).
03 — Loops and arithmetic

Counters too small, bounds too far

A Byte counter is fine until the list it walks holds more than 256 items:

var
  I: Byte;
begin
  for I := 0 to List.Count - 1 do
    List[I] := '';
uSamples.pas:85:3  [warning] loop-var-too-narrow: For-loop counter is a small ordinal (Byte / Word / SmallInt ...) but the bound is a runtime count: the counter overflows once the collection is larger than its range (CWE-197). Declare it Integer.

A loop with literal bounds over an array with literal bounds can be checked without running it. With range checking off, the default, the eleventh write lands in whatever follows Table:

var
  Table: array[0..9] of Integer;
...
  for I := 0 to 10 do
    Table[I] := I;
uSamples.pas:112:5  [warning] loop-index-exceeds-static-array: The loop counter runs 0 .. 10 but Table is declared [0 .. 9]: the index leaves the array (range error or memory overwrite, CWE-129)

Shifting a 32-bit value by 32 looks like it should give 0. The processor uses only the low five bits of the count, so V shr 32 shifts by nothing:

function HighBit(V: Cardinal): Cardinal;
begin
  Result := V shr 32;
uSamples.pas:117:13  [warning] shift-count-out-of-range: Shift count is not smaller than the operand width: a 32-bit value shifted by 32 or more (64-bit: 64 or more) does not give 0 -- the CPU masks the count, so the result is wrong (CWE-1335). Cast the operand first: Int64(X) shl N.

CompareText and its family promise the sign of the result, not the value. Testing for = 1 works for some pairs of strings and not for others:

  Result := CompareText(A, B) = 1;
uSamples.pas:99:13  [warning] compare-result-equals-literal: The CompareStr family returns any positive or negative value, not only 1 and -1: comparing the result with 1 / -1 misses most results. Test the sign (> 0, < 0) instead.
04 — Threads

Started too early, never checked

A thread started from its own constructor can run Execute before a descendant’s constructor has set up its fields:

constructor TWorker.Create;
begin
  inherited Create(False);
  Start;
end;
uSamples.pas:53:3  [warning] thread-start-in-constructor: Start called in the constructor of TWorker: Execute starts before a descendant's constructor has run -- create the thread with Create(False) and let AfterConstruction start it, or call Start after the Create

A thread created with Create(False) is running by the time the next line executes. Setting FreeOnTerminate there races the thread’s own end. The rule needs the index to know that TPlainWorker descends from TThread:

  W := TPlainWorker.Create(False);
  W.FreeOnTerminate := True;
uSamples.pas:94:3  [warning] thread-configured-after-start: The thread created at line 93 with Create(False) is already running when W.FreeOnTerminate is assigned here: Execute may read it first (and FreeOnTerminate set now races the thread's end) -- create it suspended (Create(True)), configure it, then call Start

A WaitFor with a timeout returns whether the event was signalled. Thrown away, a timeout looks exactly like success:

  Ev.WaitFor(5000);
uSamples.pas:104:3  [warning] waitfor-result-ignored: The TWaitResult of WaitFor(<timeout>) is discarded: a timeout or an abandoned wait goes unnoticed (CWE-252). Test the result (= wrSignaled).
05 — Classes and forms

A cast without a test, focus too soon

Assign takes any TPersistent. A hard cast assumes the caller passed the right class; when it did not, the method reads another object’s memory as if it were a TItem:

procedure TItem.Assign(Source: TPersistent);
begin
  FName := TItem(Source).FName;
end;
uSamples.pas:66:12  [warning] assign-override-unsafe-cast: Hard cast of the Assign parameter without an is test: a Source of another class corrupts memory (use Source as T, or test Source is T first)

The last one needs the form file as well as the code. uLogin.dfm binds OnCreate = FormCreate, and the form is not visible yet when OnCreate runs:

procedure TLoginForm.FormCreate(Sender: TObject);
begin
  edUser.SetFocus;
end;
uLogin.pas:20:3  [warning] setfocus-in-formcreate: SetFocus in the form's OnCreate handler: the form is not visible yet, so this raises EInvalidOperation -- set ActiveControl instead
06 — Fixes

Four of the twelve can fix the code

fillchar-args-swapped, compare-result-equals-literal, loop-var-too-narrow (widen the counter to Integer) and setfocus-in-formcreate (set ActiveControl instead) come with a fix. Each fix is marked risky because it changes what the program does, which is the point, so nothing is written until you pass --apply. Without it you get the change to review:

> drag-lint lint --in uSamples.pas --db samples.sqlite --fix --fix-line 71 --fix-rule fillchar-args-swapped
File: uSamples.pas
  replace L71:C3..32 -> "FillChar(Buf, SizeOf(Buf), 0)"
[risky] one or more fixes are behaviour-changing -- review before --apply.
autofix: 1 fixable finding(s) -- pass --apply to write

> drag-lint lint --in uSamples.pas --db samples.sqlite --fix --fix-line 99 --fix-rule compare-result-equals-literal
File: uSamples.pas
  replace L99:C13..34 -> "CompareText(A, B) > 0"
[risky] one or more fixes are behaviour-changing -- review before --apply.
autofix: 1 fixable finding(s) -- pass --apply to write

With --project and --apply, drag-lint compiles the project after the fix and keeps the change only if it still builds.

07 — Without an index

Half of them need to know the declarations

Run on the file alone, with no index, lint reports six of the twelve: the ones that can be judged from the line itself (swapped FillChar arguments, the compare, the shift, the ignored wait, the Assign cast, the loop over Table). It also says what it skipped:

drag-lint: note: no index covers this file, so the store-backed and project rules did NOT run -- pass --db <index.sqlite> for parity with lint-all. A 0 above does not mean they are clean.

The other six need facts a single line does not hold: the declared size of Hdr, the type of Payload, the type of I, the ancestry of TWorker and TPlainWorker, and the handler the form file binds. In RAD Studio and in the language server the index is already there, so all twelve run as you save.

08 — Limits

What to know before you rely on it

  • The fixture is written to show the rules, one mistake per routine. On copies of three of our own code bases the twelve rules reported nothing at all before release. That is the result we want from rules that are on by default: quiet on code that is right, and a finding worth reading when one appears.
  • memory-op-count-exceeds-buffer judges only variables whose size is known from the declaration: scalars and arrays with literal bounds. A buffer from GetMem or a dynamic array is not checked.
  • loop-index-exceeds-static-array needs literal loop bounds; a bound read at run time is not judged.
  • Two related rules came in 1.65 as well but are off by default until we have measured them on more code: sql-add-without-clear and waitfor-synchronize-deadlock.
  • 1.65 changes what the indexer extracts, so every index is rebuilt once after the update. On a large project that is heavy CPU work.

Output measured 2026-12-04 with drag-lint 1.65.0-alpha on a two-unit fixture written for this article and compiled with the Delphi 13 Win64 compiler (dcc64 37.0); long lines of the first lint run shortened to .... The figure for our own code bases is from the 1.65 pre-release measurement.