You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[System.Drawing.Common] Work around libgdiplus use after free (#43074)
* [System.Drawing.Common] Work around libgdiplus use after free
On Windows, both of the following are legal
Metafile mf = ... ; // get a metafile instance
Graphics g = Graphics.FromImage(mf);
g.Dispose(); mf.Dispose();
and
Metafile mf = ... ; // get a metafile instance
Graphics g = Graphics.FromImage(mf);
mf.Dispose(); g.Dispose();
On Unix, libgdiplus has a use after free bug for the second form - the metafile
native image is disposed, but the graphics instance still has a pointer to the
memory that it will use during cleanup. If the memory is reused, the graphics
instance will see garbage values and crash.
The workaround is to add a MetadataHolder class and to transfer responsibility
for disposing of the native image instance to it if the Metafile is disposed
before the Graphics.
Note that the following is not allowed (throws OutOfMemoryException on GDI+ on
Windows), so there's only ever one instance of Graphics associated with a
Metafile at a time.
Graphics g = Graphics.FromImage(mf);
Graphics g2 = Graphics.FromImage(mf); // throws
Addresses #37838
* Formatting fixes
Co-authored-by: Santiago Fernandez Madero <safern@microsoft.com>
* Address review feedback
* Inilne unhelpful helper
* formatting
Co-authored-by: Santiago Fernandez Madero <safern@microsoft.com>
0 commit comments