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

Account for optional out parameters in IStream #6345

Merged
merged 1 commit into from
May 9, 2022

Conversation

miloush
Copy link
Contributor

@miloush miloush commented Apr 3, 2022

Fixes #6344

Description

System.Windows.Media.StreamAsIStream.Write(byte[] buffer, uint cb, out uint cbWritten) throws NullReferenceException when trying to set cbWritten because the out int cbWritten parameter has been passed in as null.

According to the documentation, caller can pass NULL as pcbWritten to IStream::Write when they are not interested in the number of bytes written.

The CManagedStreamWrapper does not expect that and simply passes the pointer to the managed assembly, however, CLR is not happy with writing to null out parameters.

The PR fixes the issue by always passing a valid reference to the managed assembly. If an output argument was omitted, a reference to a local variable on stack is passed instead, and the output value is thrown away.

The same convention applies to IStream::Seek and IStream::CopyTo methods.

Customer Impact

Customers are unable to use WIC encoders (and potentially decoders) that call IStream without the optional parameters (i.e. passing NULL). In the case of #6344, customers cannot save HEIF files using a paid encoder provided by Microsoft (built-in in previous Windows builds).

Regression

No.

Testing

Compiled an updated wpfgfx_cor3.dll and tested in .NET 6.0.2 x86 WPF application with the repro code in #6344.

Risk

I am not aware of any risks, the fix does not affect any currently working scenario.

@miloush miloush requested a review from a team as a code owner April 3, 2022 17:02
@ghost ghost added the PR metadata: Label to tag PRs, to facilitate with triage label Apr 3, 2022
@ghost ghost requested review from dipeshmsft, singhashish-wpf and SamBent April 3, 2022 17:02
@ghost ghost added the Community Contribution A label for all community Contributions label Apr 3, 2022
Copy link
Member

@lindexi lindexi left a comment

Choose a reason for hiding this comment

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

Thank you and look good to me.

@ghost ghost assigned miloush May 5, 2022
@dipeshmsft dipeshmsft merged commit 6b4faa7 into dotnet:main May 9, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Jun 8, 2022
@miloush miloush deleted the StreamAsIStream-OptionalOutArgs branch November 1, 2022 01:57
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Community Contribution A label for all community Contributions PR metadata: Label to tag PRs, to facilitate with triage
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BitmapEncoder.Save throws NullReferenceException
4 participants