Skip to content
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

Should we dispose EventLog if there was an Interop.Errors.ERROR_INVALID_PARAMETER? #104053

Open
omajid opened this issue Jun 26, 2024 · 3 comments
Labels
area-System.Diagnostics.EventLog needs-further-triage Issue has been initially triaged, but needs deeper consideration or reconsideration question Answer questions and provide assistance, not an issue with source code or documentation.
Milestone

Comments

@omajid
Copy link
Member

omajid commented Jun 26, 2024

EventLog log = new EventLog(logNames[i], machineName);
SafeEventLogReadHandle handle = Interop.Advapi32.OpenEventLog(machineName, logNames[i]);
if (!handle.IsInvalid)
{
handle.Close();
logs.Add(log);
}
else if (Marshal.GetLastWin32Error() != Interop.Errors.ERROR_INVALID_PARAMETER)
{
// This api should return the list of all event logs present on the system even if the current user can't open the log.
// Windows returns ERROR_INVALID_PARAMETER for special keys which were added in RS5+ but do not represent actual event logs.
logs.Add(log);
}

In the two conditions, we save the just-created log object. Should we call log.Dispose() if neither of those branches are taken?

@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Jun 26, 2024
Copy link
Contributor

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

@ericstj ericstj added question Answer questions and provide assistance, not an issue with source code or documentation. and removed untriaged New issue has not been triaged by the area owner labels Jul 18, 2024
@ericstj
Copy link
Member

ericstj commented Jul 18, 2024

If we create a disposable object and don't return it, then we should dispose it. If we still return it, then it should be the caller's responsibility to dispose.

Here we create it but don't expose it, so it could be disposed. Perhaps the original authors here "knew" that no disposable resources would be created.

I don't see any disposable resources created in

public EventLogInternal(string logName, string machineName, string source, EventLog parent)
{
ArgumentNullException.ThrowIfNull(logName);
if (!ValidLogName(logName, true))
throw new ArgumentException(SR.BadLogName);
if (!SyntaxCheck.CheckMachineName(machineName))
throw new ArgumentException(SR.Format(SR.InvalidParameter, nameof(machineName), machineName));
this.machineName = machineName;
this.logName = logName;
this.sourceName = source;
readHandle = null;
writeHandle = null;
boolFlags[Flag_forwards] = true;
this.parent = parent;
}

So probably this isn't an actual leak, but could be updated. @omajid did you notice any actual leak here?

@ericstj ericstj added this to the Future milestone Jul 18, 2024
@ericstj ericstj added the needs-further-triage Issue has been initially triaged, but needs deeper consideration or reconsideration label Jul 18, 2024
@omajid
Copy link
Member Author

omajid commented Jul 18, 2024

did you notice any actual leak here?

No. It was flagged by a code scanner.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-System.Diagnostics.EventLog needs-further-triage Issue has been initially triaged, but needs deeper consideration or reconsideration question Answer questions and provide assistance, not an issue with source code or documentation.
Projects
None yet
Development

No branches or pull requests

2 participants