← Back to fpsonar
Five rules about code that works on the machine it was written on and fails somewhere else: a path separator typed by hand, a line ending baked into a literal, and three ways of getting a byte count wrong.
All are off by default.
Separators and line endings
editHardcodedPathSeparator
editminor · off by default
A string literal holding exactly a directory separator, used in a concatenation. '/'
works on Unix and '\' on Windows; PathDelim is whichever the target uses.
The rule only reports a literal that is the separator and is an operand of +. A
separator inside a longer literal — a URL, a regular expression, a message — is left alone.
fpsonar complains about this
lFull := lDir + '/' + lName;
fpsonar accepts this
lFull := lDir + PathDelim + lName;
IncludeTrailingPathDelimiter(lDir) + lName is also accepted, and is usually better.
HardcodedLineEnding
editminor · off by default
A #13#10 pair written as character escapes. Line endings differ by target — CRLF on
Windows, LF on Unix — and LineEnding is the one that matches.
Writing CRLF deliberately is legitimate when a protocol requires it: HTTP headers and SMTP
are CRLF regardless of host. Silence those with // NOSONAR rather than switching the rule
off, so the deliberate ones stay visible.
fpsonar complains about this
lText := 'first' + #13#10 + 'second';
fpsonar accepts this
lText := 'first' + LineEnding + 'second';
Byte counts
editThe three rules below all report the same kind of mistake: a number handed to a routine that
reads or writes raw bytes, where the number does not describe the thing being transferred.
They cover BlockRead, BlockWrite, ReadBuffer and WriteBuffer.
PackedRecordFieldAlignmentAssumption
editmajor · off by default
SizeOf of a record that is neither packed nor explicitly aligned, used as an I/O byte
count. The compiler inserts padding between fields to suit the target, so the size of such a
record is a property of the machine, not of the declaration. Write it to a file on one target
and read it on another and the fields land in the wrong places.
fpsonar complains about this
type
THeader = record
Kind: Byte;
Size: LongInt; // padded to a 4-byte boundary on most targets
end;
BlockWrite(lFile, lHeader, SizeOf(THeader));
fpsonar accepts this
type
THeader = packed record
Kind: Byte;
Size: LongInt;
end;
BlockWrite(lFile, lHeader, SizeOf(THeader));
PointerSizedDatumTruncatedByByteCount
editmajor · off by default
SizeOf of a fixed-width type used as the byte count for a buffer that is a pointer or a
pointer-sized integer. A pointer is 8 bytes on a 64-bit target and SizeOf(Integer) is 4, so
half the value is silently lost.
This shape was correct on 32-bit targets, where a pointer and an Integer were the same
width. That is exactly why it survives in ported code and fails quietly rather than loudly.
A fixed-width count for a fixed-width datum is correct and is not reported.
fpsonar complains about this
var
lPtr: Pointer;
begin
BlockWrite(lFile, lPtr, SizeOf(Integer)); // writes 4 of 8 bytes
fpsonar accepts this
var
lPtr: Pointer;
begin
BlockWrite(lFile, lPtr, SizeOf(Pointer));
Persisting a pointer value at all is rarely meaningful — the address is not valid in the next process. If this rule fires, check whether the pointer should have been dereferenced first.
AbsoluteVariableOverlay
editmajor · off by default
A variable declared absolute over another whose declared size differs from its own.
absolute places two variables at the same address; when the second is larger, reading or
writing through it runs past the end of the first.
fpsonar complains about this
var
lSmall: Word;
lWide: LongInt absolute lSmall; // 4 bytes over a 2-byte variable
fpsonar accepts this
var
lValue: LongInt;
lBytes: array[0..3] of Byte absolute lValue;
Related chapters
editCasts that lose precision on some targets — PlatformDependentTruncation and
PlatformDependentCast — are in expressions. Rules about {$ifdef}
blocks themselves are in conditional compilation.
See also
edit- fpsonar — what fpSonar is, and how to run it
← Back to fpsonar