Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.

PVS-Studio: fixed vulnerability CWE-476 (NULL Pointer Dereference) #16807

Merged
merged 10 commits into from Mar 10, 2017
Merged

PVS-Studio: fixed vulnerability CWE-476 (NULL Pointer Dereference) #16807

merged 10 commits into from Mar 10, 2017

Conversation

IvanKishchenko
Copy link
Contributor

@IvanKishchenko IvanKishchenko commented Mar 7, 2017

We have found and fixed a vulnerability CWE-476 (NULL Pointer Dereference) using PVS-Studio tool.
Analyzer warning: V3080 and V3019.
PVS-Studio is a static code analyzer for C, C++ and C#.

@dnfclas
Copy link

dnfclas commented Mar 7, 2017

@IvanKishchenko,
Thanks for your contribution.
To ensure that the project team has proper rights to use your work, please complete the Contribution License Agreement at https://cla2.dotnetfoundation.org.

It will cover your contributions to all Microsoft-managed open source projects.
Thanks,
.NET Foundation Pull Request Bot

@dnfclas
Copy link

dnfclas commented Mar 7, 2017

@IvanKishchenko, thanks for signing the contribution license agreement. We will now validate the agreement and then the pull request.

Thanks, .NET Foundation Pull Request Bot

@stephentoub
Copy link
Member

Thanks, @IvanKishchenko. Most of these changes look fine, but at least one not: they broke ~2500 tests. Can you please fix?

@IvanKishchenko
Copy link
Contributor Author

IvanKishchenko commented Mar 7, 2017

Dear, @stephentoub . Now I am working on fixing these.

@@ -43,7 +43,7 @@ internal class CaseInsensitiveAscii : IEqualityComparer, IComparer
public int GetHashCode(object myObject)
{
string myString = myObject as string;
if (myObject == null)
if (myString == null)
Copy link
Contributor

Choose a reason for hiding this comment

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

It is better to use object.ReferenceEquals to avoid calling the equality operator

Copy link
Contributor

Choose a reason for hiding this comment

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

Roslyn is smart enough to avoid the string equality operator when comparing null, so object.ReferenceEquals isn't necessary.

Copy link
Contributor Author

@IvanKishchenko IvanKishchenko left a comment

Choose a reason for hiding this comment

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

Theoretically before I made any changes, this code should have been failing with NullReferenceException.
But in fact it breaks tests.
I think that this piece of code should be reconsidered because it does not make any sense and NullReferenceException can be thrown in more simple way.

@@ -106,7 +106,7 @@ private void RecordType(AggregateType type, Symbol sym)
// If it is first, record it.
if (_swtFirst == null)
{
_swtFirst.Set(sym, type);
_swtFirst.Set(sym, type); // NullReferenceException possible!
Copy link
Contributor

Choose a reason for hiding this comment

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

_swtFirst actually is never null since it is initialized only once in constructor.
The wierd thing here is that SymWithType overloads the equality operator so that it equals to null when one of its field is null.

@IvanKishchenko
Copy link
Contributor Author

All unit tests are successful.

@@ -103,7 +103,7 @@ internal CompilerInfo(CompilerParameters compilerParams, string codeDomProviderT
public override bool Equals(object o)
{
CompilerInfo other = o as CompilerInfo;
if (o == null)
if (other == null)
Copy link
Member

Choose a reason for hiding this comment

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

The fact that all of our tests were passing before this change suggests that we're missing some test coverage. To go along with your fixes, can you add tests that would have failed before the changes?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I would be glad to do this, but I doubt that my manager would approve these actions. Most likely he will say something "We are a small company and cannot develop Microsoft projects for free" :) If you wish, I could give you contacts of our CEO and you could discuss variants of paid collaboration. We really could fix all the warnings and write tests, we do such tasks. Here is the example of how we did it for Unreal Engine.

Copy link
Member

Choose a reason for hiding this comment

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

In general, we consider src/tests to be a unit, such that we ask contributors changing src to ensure that the tests for the thing being changed are appropriate. Doing so helps to ensure that the fix is actually a fix (and not a regression, as it was in the case of one of your changes you backed out... imagine how bad things could have been if we didn't have those tests and we merged the change). We can skip doing so this time for the few remaining modifications, but for any future contributions please do so. Thanks for the interest in contributing!

@stephentoub stephentoub merged commit 44eb1db into dotnet:master Mar 10, 2017
@karelz karelz modified the milestone: 2.0.0 Mar 15, 2017
picenka21 pushed a commit to picenka21/runtime that referenced this pull request Feb 18, 2022
…otnet/corefx#16807)

* PVS-Studio: fixed vulnerability CWE-476 (NULL Pointer Dereference)

* Undo changes for understand what is breaking tests.


Commit migrated from dotnet/corefx@44eb1db
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
6 participants