Skip to content

Sanitize Reg Entries in SerialPort.GetPortNames (Issue 93240) #93242

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ public static string[] GetPortNames()
string[] result = serialKey.GetValueNames();
for (int i = 0; i < result.Length; i++)
{
// Replace the name in the array with its value.
result[i] = (string)serialKey.GetValue(result[i]);
// Replace the name in the array with its value, trimming at the first null character if it exists
var temp = (string)serialKey.GetValue(result[i]);
var end = temp.IndexOf('\0');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure whether we should try to workaround the null characters here. Ideally RegistryKey.GetValueNames should do it out of the box.

@dotnet/area-microsoft-win32 thoughts?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GetValueNames looks like better choice to me. can you please give it try @ctacke ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what the suggestion is. The Value Name is not the issue, the Value itself is. I can't "try GetValueNames" as a resolution, because it doesn't give the data I need.

You could certainly argue that Registry.GetValue should be stopping at the first null, but I don't know if there are other instances where a null-containing string might be valid data. If null-containing strings are generally considered invalid data for registry entries, then yes, the bug would be in Registry.GetValue

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I may be wrong but it seems like this is what the method does e.g. terminates on null (while the GetValue does not)
https://learn.microsoft.com/en-us/dotnet/api/microsoft.win32.registrykey.getvaluenames?view=net-8.0#microsoft-win32-registrykey-getvaluenames

Perhaps @adamsitnik can explain more.

result[i] = temp.Substring(0, end < 0 ? temp.Length : end);
}
return result;
}
Expand Down