From e35257d2b6fdc301c1f76f514350191bedebf0bd Mon Sep 17 00:00:00 2001 From: Stefan Ziegler Date: Wed, 5 Jul 2023 16:52:41 +0200 Subject: [PATCH] Fix possible crash in wxWindowsPrintNativeData initialization If the printer name is specified, avoid fall back to the default printer via the use of PRINTDLG, as the default printer DEVMODE may be incompatible with that of the printer we're actually going to use and could possible result in a crash if their size differs. Use a temporary printer if necessary to retrieve the correct DEVMODE for the given printer name. Closes #23685. --- src/msw/printdlg.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/msw/printdlg.cpp b/src/msw/printdlg.cpp index 6325c4dc87..29e8dce686 100644 --- a/src/msw/printdlg.cpp +++ b/src/msw/printdlg.cpp @@ -423,8 +423,13 @@ void wxWindowsPrintNativeData::InitializeDevMode(const wxString& printerName, Wi // this replaces the PrintDlg function which creates the DEVMODE filled only with data from default printer. if ( !m_devMode && !printerName.IsEmpty() ) { + // ensure that we have a printer object here, otherwise we are unable to determine m_devMode + WinPrinter fallbackPrinter; + if (!printer) + printer = &fallbackPrinter; + // Open printer - if ( printer && printer->Open( printerName ) == TRUE ) + if ( printer->Open( printerName ) == TRUE ) { DWORD dwNeeded, dwRet; @@ -468,7 +473,7 @@ void wxWindowsPrintNativeData::InitializeDevMode(const wxString& printerName, Wi } } - if ( !m_devMode ) + if ( !m_devMode && printerName.IsEmpty() ) { // Use PRINTDLG as a way of creating a DEVMODE object PRINTDLG pd;