Account for optional out parameters in IStream #6345
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #6344
Description
System.Windows.Media.StreamAsIStream.Write(byte[] buffer, uint cb, out uint cbWritten)
throwsNullReferenceException
when trying to setcbWritten
because theout int cbWritten
parameter has been passed in as null.According to the documentation, caller can pass
NULL
aspcbWritten
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
andIStream::CopyTo
methods.Customer Impact
Customers are unable to use WIC encoders (and potentially decoders) that call
IStream
without the optional parameters (i.e. passingNULL
). 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.