Skip to content

Conversation

@marek-safar
Copy link
Contributor

No description provided.

@ghost ghost assigned marek-safar Dec 20, 2021
@ghost ghost added the area-Meta label Dec 20, 2021
@ghost
Copy link

ghost commented Dec 20, 2021

Tagging subscribers to this area: @dotnet/area-meta
See info in area-owners.md if you want to be subscribed.

Issue Details

null

Author: marek-safar
Assignees: marek-safar
Labels:

area-Meta

Milestone: -

@ghost
Copy link

ghost commented Dec 20, 2021

Tagging subscribers to this area: @dotnet/area-system-runtime
See info in area-owners.md if you want to be subscribed.

Issue Details

null

Author: marek-safar
Assignees: marek-safar
Labels:

area-System.Runtime

Milestone: -

Copy link
Member

@eerhardt eerhardt left a comment

Choose a reason for hiding this comment

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

Is there some sort of static analysis we can enable so more of these don't get reintroduced?

@marek-safar
Copy link
Contributor Author

Is there some sort of static analysis we can enable so more of these don't get reintroduced?

Yep, it's IDE0059 analyzer enabled but there is still more work in other libraries to enable it

Copy link
Member

@eerhardt eerhardt left a comment

Choose a reason for hiding this comment

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

LGTM

@marek-safar marek-safar merged commit 07af5b3 into dotnet:main Dec 23, 2021
@marek-safar marek-safar deleted the ide0059spc branch December 23, 2021 08:24
RuntimeTypeHandle typeOfFirstParameterIfInstanceDelegate;

IntPtr funcPtr = del.GetFunctionPointer(out typeOfFirstParameterIfInstanceDelegate, out bool _, out bool _);
IntPtr funcPtr = del.GetFunctionPointer(out RuntimeTypeHandle _, out bool _, out bool _);
Copy link
Member

Choose a reason for hiding this comment

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

Are the types necessary here (e.g. for disambiguation) or could this just have been:

IntPtr funcPtr = del.GetFunctionPointer(out _, out _, out _);

?

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 believe your simpler version would work (I just kept the syntax to match)

public static void* AllocZeroed(nuint elementCount, nuint elementSize)
{
void* result = null;
void* result;
Copy link
Member

Choose a reason for hiding this comment

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

Nit:
Could also be condensed to something like, e.g.

void* result = (elementCount != 0) && (elementSize != 0) ?
    Interop.Sys.Calloc(elementCount, elementSize) :
    Interop.Sys.Malloc(1); // the C standard does not define what happens when num == 0 or size == 0, we want an "empty" allocation

if (_numBytes == Uninitialized)
throw NotInitialized();

pointer = null;
Copy link
Member

Choose a reason for hiding this comment

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

This is changing behavior. If DangerousAddRef throws, previously the pointer that was passed in would have been nulled out, but now it won't. I think this should be reverted.

Copy link
Member

Choose a reason for hiding this comment

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

(I opened an issue on Roslyn for this exact case almost two years ago: dotnet/roslyn#42761)

}

bool retVal = true;
bool retVal;
Copy link
Member

Choose a reason for hiding this comment

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

Rather than defining this all the way up here, each of the two call sites later on that use this could be changed, e.g. from

                    retVal = WaitOnEvent(_waitUpgradeEvent, ref _numWriteUpgradeWaiters, timeout, EnterLockType.UpgradeToWrite);
                    // The lock is not held in case of failure.
                    if (!retVal)
                        return false;

to

                    bool retVal = WaitOnEvent(_waitUpgradeEvent, ref _numWriteUpgradeWaiters, timeout, EnterLockType.UpgradeToWrite);
                    // The lock is not held in case of failure.
                    if (!retVal)
                        return false;

and then the local could be further removed entirely:

                    // The lock is not held in case of failure.
                    if (!WaitOnEvent(_waitUpgradeEvent, ref _numWriteUpgradeWaiters, timeout, EnterLockType.UpgradeToWrite))
                        return false;

@ghost ghost locked as resolved and limited conversation to collaborators Feb 2, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants