View Issue Details Jump to Notes ] Issue History ] Print ]
IDProjectCategoryView StatusDate SubmittedLast Update
0021899FPCCompilerpublic2012-04-30 20:012014-02-08 07:26
Reportermas 
Assigned To 
PrioritynormalSeveritytweakReproducibilityalways
StatusnewResolutionopen 
Platformdesktop and laptopOSwindows OS VersionXP & Win 7
Product Version2.4.4Product Build32559 
Target VersionFixed in Version 
Summary0021899: Tregistry access of bluetooth comm ports drops last char
Descriptionwhen accessing window's comm port registry the ReadString() works normally for USB and real hardware but drops the last character for any Bluetooth comm ports. the error is with Windows but a fix is needed anyway.

 
either the striping of the last char is fixed to check before striping or an override flag is needed in the Tregistry so the trailing Null is not striped.
Steps To Reproduceadd a Bluetooth comm port (hardware needed?). see windows fail.
or add a simulated entry elsewhere and access with TRegistry

binary view of working entry
"COM10" = [43,00,4f,00,4d,00,31,00,30,00,00,00]

binary view of bluetooth entry
"COM58" = [43,00,4f,00,4d,00,35,00,38,00]
Additional Information   reg.RootKey := HKEY_LOCAL_MACHINE;
    reg.OpenKeyReadOnly('HARDWARE\DEVICEMAP\SERIALCOMM');//, false);
    reg.GetValueNames(l);
    for n := 0 to l.Count - 1 do begin
// reg.StringSizeIncludesNulls := FALSE; // if I had set access to this..
      v.Add(reg.ReadString(l[n]));
    end;
    Result := v.CommaText;
TagsNo tags attached.
FPCOldBugId
Fixed in Revision
Attached Files

- Relationships

-  Notes
(0059135)
mas (reporter)
2012-04-30 20:06

I included some more data but I do not see it here. flaw in reporting website or is it only accessable by the repairman?
(0059227)
mas (reporter)
2012-05-04 03:16
edited on: 2012-05-04 03:16

I verified that setting StringSizeIncludesNull := FALSE just before the call does GET the full string. (kluge) adding the strings with the extra null to tstring does not hurt anything (my access).
If I fix this issure correctly, do I/can I upload a modified registry file from 2.4.4 ??

(0072896)
Thorsten Schmidt (reporter)
2014-02-07 16:50
edited on: 2014-02-07 22:37

I have the same problem still in 2.6.2

In the function TRegistry.ReadString the GetDataInfo gets type and size of the requested registry entry. As StringSizeIncludesNull ist set to true in TRegistry.Create the returned size is decremented before the string ist fetched.

But GetDataInfo fails as it uses RegQueryValueEx to determine the size. The Microsoft API documentation says for parameter lpcbData:
"... If the data has the REG_SZ, REG_MULTI_SZ or REG_EXPAND_SZ type, this size includes any terminating null character or characters unless the data was stored without them. For more information, see Remarks..."

And in Remarks:
"If the data has the REG_SZ, REG_MULTI_SZ or REG_EXPAND_SZ type, the string may not have been stored with the proper terminating null characters. Therefore, even if the function returns ERROR_SUCCESS, the application should ensure that the string is properly terminated before using it"

So the assumed StringSizeIncludesNull=true is wrong.

Microsoft recommends: "One way an application can ensure that the string is properly terminated is to use RegGetValue, which adds terminating null characters if needed".

Ugly fix:
In TRegistry.ReadString
     ...
     SetLength(Result,Info.DataSize);
// discard size "correction"
// If StringSizeIncludesNull then
// SetLength(Result, Info.DataSize-1)
// else
// SetLength(Result, Info.DataSize);
// get string WITH null instead
     GetData(Name,PChar(Result),Info.DataSize,Info.RegData);
// trim the result
     Result := Trim(Result);
     ...

(0072905)
Bart Broersma (reporter)
2014-02-07 23:37

> Result := Trim(Result);
Can't strings in the registry end in spaces?
(0072917)
Thaddy de Koning (reporter)
2014-02-08 07:12
edited on: 2014-02-08 07:26

@Bart: Yes, they can. They can even contain embedded #0's (although that can only be read back with ntApi calls or with custom code, not in userspace registry api).

You have to be very careful messing with registry code.
The embedded #0's are employed in certain protection schemes and are also used in malicious code. Microsoft itself also employs it in rare cases:temporary information storage during installs, to enable recovery on compromized systems during installs for example.

So, yes, ending spaces are allowed, and #0's can be in the middle of the content.
More information on the sysinternals website and with these utilities:
http://technet.microsoft.com/en-us/sysinternals/bb897448 [^]
http://technet.microsoft.com/en-us/sysinternals/dn194428 [^]


- Issue History
Date Modified Username Field Change
2012-04-30 20:01 mas New Issue
2012-04-30 20:06 mas Note Added: 0059135
2012-05-04 03:16 mas Note Added: 0059227
2012-05-04 03:16 mas Note Edited: 0059227
2014-02-07 16:50 Thorsten Schmidt Note Added: 0072896
2014-02-07 19:43 Thorsten Schmidt Note Edited: 0072896 View Revisions
2014-02-07 22:37 Thorsten Schmidt Note Edited: 0072896 View Revisions
2014-02-07 23:37 Bart Broersma Note Added: 0072905
2014-02-08 07:12 Thaddy de Koning Note Added: 0072917
2014-02-08 07:15 Thaddy de Koning Note Edited: 0072917 View Revisions
2014-02-08 07:22 Thaddy de Koning Note Edited: 0072917 View Revisions
2014-02-08 07:26 Thaddy de Koning Note Edited: 0072917 View Revisions



MantisBT 1.2.12[^]
Copyright © 2000 - 2012 MantisBT Group
Powered by Mantis Bugtracker