← Back to fpsonar
Nine rules. Object Pascal has a lot of string types (probably more than any other language)
ShortString, AnsiString with a code page, RawByteString, UnicodeString,
WideString, UTF8String — and conversions between them happen silently. These rules
catch the conversions that lose data and the Format calls that fail at run time.
What string actually means depends on the compiler mode: {$mode objfpc} with the
default RTL gives you an AnsiString, {$mode delphiunicode} gives you a
UnicodeString. Several rules below therefore behave differently per mode, which is
deliberate.
UnicodeToAnsiCast
editmajor · on by default
Casting a Unicode or wide string to an ANSI type converts through a code page, and
every character the code page cannot represent becomes ? — silently, with no error.
fpsonar complains about this
var
W: UnicodeString;
A: AnsiString;
begin
W := 'naïve café';
A := AnsiString(W); // characters lost if the code page cannot hold them
end;
fpsonar accepts this
var
W: UnicodeString;
U: UTF8String;
begin
W := 'naïve café';
U := UTF8Encode(W); // explicit, lossless
end;
CharToCharPointerCast
editmajor · on by default
Casting a single character to PChar does not give you a one-character string — it
reinterprets the character's value as an address. The result points at whatever
lives at that address.
fpsonar complains about this
var
C: Char;
begin
Log(PChar(C)); // C's code point used as an address
end;
fpsonar accepts this
var
C: Char;
begin
Log(PChar(string(C))); // a real one-character string
end;
StringFirstCharByIndex
editminor · on by default
Reading the first character with S[1] works but says nothing about intent, and it
crashes on an empty string. Prefer a form that states what you are doing and handles
empty input.
fpsonar complains about this
if S[1] = '-' then
Negate;
fpsonar accepts this
if (S <> '') and (S[Low(S)] = '-') then
Negate;
Note Low(S) rather than 1: it is 1 for AnsiString but 0 for a
zero-based string type, so Low keeps the code correct in both.
ImplicitTEncodingDefault
editminor · on by default
An overload that falls back to TEncoding.Default reads or writes using whatever
encoding the machine happens to be configured for. The same program then produces
different files on different machines. Name the encoding.
fpsonar complains about this
Lines.SaveToFile(AName); // encoding depends on the machine
fpsonar accepts this
Lines.SaveToFile(AName, TEncoding.UTF8);
DefaultFormatSettingsInDateFormat
editminor · on by default
The same argument for numbers and dates. FormatDateTime and friends default to the
global DefaultFormatSettings, which follows the machine's locale — so a decimal
point becomes a comma somewhere else, and a file your program writes it cannot read
back.
fpsonar complains about this
S := FormatDateTime('yyyy-mm-dd', Now);
fpsonar accepts this
var
FS: TFormatSettings;
begin
FS := DefaultFormatSettings;
FS.DateSeparator := '-';
S := FormatDateTime('yyyy-mm-dd', Now, FS);
end;
ValidFormatString
editmajor · on by default
A malformed conversion specifier in a Format string. Format parses its template
at run time, so a bad specifier is an exception rather than a compile error — usually
found by a user.
fpsonar complains about this
S := Format('total: %q', [Total]); // %q is not a specifier
fpsonar accepts this
S := Format('total: %d', [Total]);
FormatArgumentCount
editmajor · on by default
The number of arguments does not match the number of specifiers. Too few raises at run time; too many silently ignores the extras, which usually means one of them was meant to appear.
fpsonar complains about this
S := Format('%s scored %d', [AName]); // %d has no argument
fpsonar accepts this
S := Format('%s scored %d', [AName, Score]);
FormatArgumentType
editmajor · on by default
An argument whose type does not match its specifier — a string where %d expects an
integer. Format takes an array of variants, so the compiler cannot check this; the
mismatch surfaces as a run-time exception or as garbage in the output.
fpsonar complains about this
S := Format('count: %d', [AName]); // AName is a string
fpsonar accepts this
S := Format('count: %s', [AName]);
StringListDuplicatesNeedsSorted
editminor · on by default
TStringList.Duplicates is only consulted while inserting into a sorted list.
Setting it on an unsorted list has no effect at all, so the duplicates you meant to
reject are accepted.
fpsonar complains about this
L := TStringList.Create;
L.Duplicates := dupIgnore; // ignored: L is not sorted
L.Add('a');
L.Add('a');
fpsonar accepts this
L := TStringList.Create;
L.Sorted := True;
L.Duplicates := dupIgnore;
L.Add('a');
L.Add('a'); // now genuinely ignored
Indices, lengths and counts
editPascal strings are indexed from 1, and Length counts characters. Most of the rules
below are about code that forgot one of those two facts.
All are off by default.
CopyWithZeroIndex
editmajor · off by default
A string Copy whose start index works out to 0. String indices start at 1, so Copy(s, 0, n)
returns one character fewer than intended from one position too early — an off-by-one that
produces plausible-looking output rather than an error.
The rule folds constant expressions, so Copy(s, i - i, n) is reported too.
fpsonar complains about this
lHead := Copy(lText, 0, 5);
fpsonar accepts this
lHead := Copy(lText, 1, 5);
PosResultComparedToZeroBased
editmajor · off by default
A Pos result compared to 0 or -1 in a shape that reads as though the function were
zero-based. Pos returns 0 when the substring is absent and a 1-based index when it is
present — so Pos(...) >= 0 is always true, and Pos(...) = -1 never is.
Anyone arriving from C, Python or C# has this wrong by habit.
fpsonar complains about this
if Pos('=', lLine) = -1 then
Exit; // never taken
fpsonar accepts this
if Pos('=', lLine) = 0 then
Exit;
LengthUsedAsByteCount
editmajor · off by default
A Move or FillChar whose byte count is the Length of a string. Length counts
characters; those routines want bytes. For a UnicodeString or WideString that is half the
data, and for a UTF8String holding non-ASCII it is fewer bytes than the text occupies.
fpsonar complains about this
Move(lSource[1], lDest[1], Length(lSource)); // characters, not bytes
fpsonar accepts this
Move(lSource[1], lDest[1], Length(lSource) * SizeOf(Char));
SetLengthWithoutFill
editmajor · off by default
A local is indexed after SetLength with no write in between. SetLength reserves room; it
does not clear it. What you read back is whatever the allocator had.
fpsonar complains about this
SetLength(lBuffer, 16);
if lBuffer[1] = #0 then // uninitialised
Exit;
fpsonar accepts this
SetLength(lBuffer, 16);
FillChar(lBuffer[1], 16 * SizeOf(Char), 0);
if lBuffer[1] = #0 then
Exit;
ShortStringTruncation
editmajor · off by default
A string constant assigned to a string[N] longer than N. The compiler truncates silently:
the tail is gone, and nothing at run time says so.
fpsonar complains about this
var
lCode: string[4];
begin
lCode := 'ABCDEFGH'; // silently becomes 'ABCD'
fpsonar accepts this
var
lCode: string[8];
begin
lCode := 'ABCDEFGH';
Types and encodings
editPCharOfTemporaryString
editcritical · off by default
A character-pointer cast whose operand is a function result or another temporary. The temporary is freed at the end of the statement, so the pointer dangles immediately — and the memory usually still reads correctly for a while, which is what makes this so hard to find by testing.
The only critical rule in this chapter, and it earns it.
fpsonar complains about this
lP := PChar(BuildName(aId)); // the result dies at the semicolon
UseLater(lP);
fpsonar accepts this
lName := BuildName(aId); // a named local outlives the statement
lP := PChar(lName);
UseLater(lP);
ImplicitStringConversionWithDataLoss
editmajor · off by default
An assignment of a wide-encoded string to an ANSI-encoded one. Characters outside the target code page are replaced or dropped, without a warning at the assignment.
fpsonar complains about this
var
lWide: UnicodeString;
lAnsi: AnsiString;
begin
lAnsi := lWide; // anything outside the code page is lost
fpsonar accepts this
lUtf8 := UTF8Encode(lWide); // keeps every character
RawByteStringCodePageMix
editmajor · off by default
A RawByteString mixed with a code-page-carrying string, with no conversion written.
RawByteString deliberately carries no code page — it is a container for bytes whose
encoding you track yourself — so combining it with an encoded string leaves the compiler to
assume one.
fpsonar complains about this
var
lRaw: RawByteString;
lText: UTF8String;
begin
lText := lRaw; // whose code page?
fpsonar accepts this
SetCodePage(lRaw, CP_UTF8, False);
lText := lRaw;
CharComparedToString
editmajor · off by default
An equality comparison between a char and a string constant of a length other than one.
Such a comparison can never be true, so the branch is dead. Usually a typo in the literal.
fpsonar complains about this
if lCh = 'ab' then // a char is never equal to two characters
Skip;
fpsonar accepts this
if lCh = 'a' then
Skip;
WideStringOnNonWindows
editminor · off by default
A declaration typed WideString. On Windows that is a COM BSTR, allocated by the OLE
allocator; everywhere else it is an ordinary reference-counted wide string. Code that uses it
for its COM behaviour is portable only by accident.
Unless you are talking to COM, UnicodeString is the type you want.
fpsonar complains about this
var
lName: WideString;
fpsonar accepts this
var
lName: UnicodeString;
Calls and loops
editStrToIntWithoutGuard
editmajor · off by default
StrToInt on an argument that is not a constant, with no handler guarding the call.
StrToInt raises on input it cannot parse — so this is the shape that turns a malformed
config line or a user typing into the wrong box into an unhandled exception.
fpsonar complains about this
lCount := StrToInt(lField);
fpsonar accepts this
lCount := StrToIntDef(lField, 0);
TryStrToInt is equally fine when you need to tell "absent" from "zero".
StringConcatInLoop
editminor · off by default
A string assigned the concatenation of itself and something else, inside a loop. Each iteration allocates a new string and copies everything accumulated so far, so the cost grows with the square of the number of iterations. Fine for ten items; not fine for ten thousand.
fpsonar complains about this
for i := 0 to aCount - 1 do
lText := lText + Render(i);
fpsonar accepts this
lBuilder := TStringList.Create;
try
for i := 0 to aCount - 1 do
lBuilder.Add(Render(i));
lText := lBuilder.Text;
finally
lBuilder.Free;
end;
Related chapters
editCasts that lose width rather than characters — pointer-to-integer, 64-bit
narrowing — are in expressions. AssertWithoutMessage and the
other RTL-call rules are in calling RTL routines.
See also
edit- fpsonar — what fpSonar is, and how to run it
← Back to fpsonar