ReadString from registry.pp does not read the last character of the string if the string does not end with a nil character.
Original Reporter info from Mantis: pYskal
-
Reporter name:
Original Reporter info from Mantis: pYskal
- Reporter name:
Description:
Usually when strings are added to the registry their last character is a nil (#0) character, which is automatically removed by the ReadString function. Sometimes that nil character might not be added, but ReadString still removes the last one. To compare both cases, see image attached. Most common situation is shown on the right, and the one, during which the bug occurs is shown on the left.
The code causing the problem in function
TRegistry.ReadString(const Name: string): string;
is:
If StringSizeIncludesNull then<br/>
SetLength(Result, Info.DataSize-1)
else
SetLength(Result, Info.DataSize);
I could not find where StringSizeIncludesNull (or FStringSizeIncludesNull) is initialized or set, but it always returns, too.
I fixed the bug the following way:
function TRegistry.ReadString(const Name: string): string;
Var
Info : TRegDataInfo;
begin
GetDataInfo(Name,Info);
if info.datasize>0 then
begin
If Not (Info.RegData in [rdString,rdExpandString]) then
Raise ERegistryException.CreateFmt(SInvalidRegType, [Name]);
SetLength(Result,Info.DataSize);
GetData(Name,PChar(Result),Info.DataSize,Info.RegData);
FStringSizeIncludesNull:=(RightStr(Result,1)=#0);
If StringSizeIncludesNull then
SetLength(Result, Info.DataSize-1)
else
SetLength(Result, Info.DataSize);
end
else
Result:='';
end;
Still I suspect that FStringSizeIncludesNull is intended to be set by some other function rather than s.o. started have doing something without completing it.
Additional information:
ReadString function is a part of registry.pp which is located at:
https://raw.githubusercontent.com/macek/fpc/master/packages/fcl-registry/src/registry.pp
See also: http://forum.lazarus.freepascal.org/index.php/topic,20481.msg161144.html#msg161144 and later posts.
Mantis conversion info:
- Mantis ID: 27002
- OS: Windows
- OS Build: XP 32 bit
- Platform: i386
- Version: 2.6.4
- Fixed in version: 3.0.0
- Fixed in revision: 29217 (#3e24a9eb)
- Monitored by: » dezlov (Denis Kozlov)