Open rip-mm opened 8 months ago
Tagging subscribers to this area: @dotnet/area-system-configuration See info in area-owners.md if you want to be subscribed.
Author: | rip-mm |
---|---|
Assignees: | - |
Labels: | `area-System.Configuration`, `untriaged` |
Milestone: | - |
Tagging subscribers to this area: @dotnet/area-meta See info in area-owners.md if you want to be subscribed.
Author: | rip-mm |
---|---|
Assignees: | - |
Labels: | `area-Meta`, `untriaged` |
Milestone: | - |
Fragment 1
This code dates back all the way to the .NET Framework. Unfortunately, it seems to be something that slipped through the API review. The SettingsManageability
enum definition doesn't make any sense as it is. That said, you can still achieve the desired effect by including the SettingsManageabilityAttribute
(implies SettingsManageability.Roaming
) or not including it (implies local profile). The code error resulting from this API definition error is quite harmless. Ideally we could fix the API to add SettingsManageability.Local
with non-zero value but since it's a legacy API I don't think that is worth the effort. We may just remove the ((manageAttr.Manageability & SettingsManageability.Roaming) == SettingsManageability.Roaming)
condition and add a comment instead. Thoughts?
Fragment 8/9
Submitted PRs to fix those since they are obvious cut & paste errors.
Fragment 11
It was pointed out in the original PR (https://github.com/dotnet/runtime/pull/73738/files#r943221072) and never fixed.
Hi! I have checked the .NET 8 release version with the PVS-Studio static analyzer. If you are interested, you could take a look at the errors and suspicious code fragments we found:
[ ] Fragment 1
In this case, the value of the
SettingsManageability.Roaming
enumeration constant is 0. Since the result of a bitwise AND with an operand equal to 0 is always 0, it means that 0 is compared to 0. The output of the ((manageAttr.Manageability &SettingsManageability.Roaming) == SettingsManageability.Roaming)
expression is always true. Link to the method.[ ] Fragment 2
The analyzer reports an incorrect format string in the first argument of the Trace method. Let's take a look at the method:
Indeed, the first argument is used as the format string. Arguments are substituted into this line. However, the arguments are to be substituted into placeholders of the format {0}, {1}, etc. There are no such placeholders in this string. Link to the method.
[ ] Fragment 3
We can see the
ResPrec
variable is assigned values twice here. SinceResPrec
isn't used between these two operations, it indicates an error. Link to the method.[ ] Fragment 4
Analyzer has detected that the last
if (_validationState == ValidatingReaderState.OnReadBinaryContent)
condition is always false. Let's see why it may happen. Let's take a look at the firstif
statement. There, the_validationState
field is assigned: • in then branch —ValidatingReaderState.OnAttribute
• in else branch —ValidatingReaderState.OnDefaultAttribute
Therefore, the field value can't be equal toValidatingReaderState.OnReadBinaryContent
, and the code inside if isn't executed. Link to the method.[x] Fragment 5
Here, maybe the following string is created from information about type: ConsoleApp1.Program.MyClass<string, int, double>. However, the
type.Instantiation
object is accessed in the loop by a constant index of 0. It is possible that it works as it should, but it looks odd becauseGetTypeNameDebug(type.Instantiation[i])
is expected. Link to the method.[ ] Fragment 6
The developer used the '?.' operator, suggesting that the
stack_instr
field could benull
. Everything seemed fine, there was a check, but... no such luck. It was possible to dereference a null reference in the specified line. Most likely, the developer thought that thestack_instr?.Count < length
expression withstack_instr
equal tonull
would return true, and the method would exit. But no — the result would be false. Moreover, the developer suppressed the compiler message about possible dereference of a null reference with '!'. They thought that the static analysis of the compiler just failed and didn't understand the check. Link to the method.[ ] Fragment 7
The
resolvedType
parameter is used first but is checked fornull
before being added to the cache. That's kind of weird. The analyzer points toresolvedType.Name
, but issues will arise even earlier. The TryGetValue method throws an exception if the firstresolvedType
argument isnull
. Link to the method.[x] Fragment 8
The analyzer has detected that two identical subexpressions are compared here. It's a simple yet frustrating error. Instead of being compared to
type.Namespace
,tr.Namespace
is being compared totr.Namespace
. Link to the method.[x] Fragment 9
The developer used the string interpolation '$' character. It simply substitutes 0 into the string, and the format string will be equal to
" CATCH: 0"
. As a result, the text they wanted to substitute for the {0} placeholder isn't used. The same error occurs in the next case. Link to the method.[ ] Fragment 10
The
_outerReader
argument is passed to the constructor twice. If we look at the constructor declaration, we can see that the constructor has the metadataReader parameter:The
_metadataReader
field is available when calling the constructor. Perhaps it would be better to use this field as the third argument. Link to the method.[x] Fragment 11
The expression checks
context.Target.PointerSize == 4
twice. In theGetObjectAlignment
instance method,context.Target.PointerSize
isn't changed. It's possible that something else should be checked here, or maybe it's just an unnecessary check. Link to the method.