From f7e88c0db67c3c97837182efbed5e35a51eef8a2 Mon Sep 17 00:00:00 2001 From: Artur Wieczorek Date: Sat, 4 Feb 2017 00:24:51 +0100 Subject: [PATCH] Check if printer native data object is of type wxPostScriptPrintNativeData prior to call its methods Under wxGTK (for instance), wxPrintData::GetNativeData() returns pointer to wxGtkPrintNativeData so calling wxPostScriptPrintNativeData methods on it crashes. Closes #13536 --- src/generic/dcpsg.cpp | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/generic/dcpsg.cpp b/src/generic/dcpsg.cpp index f318c1f913..494b602316 100644 --- a/src/generic/dcpsg.cpp +++ b/src/generic/dcpsg.cpp @@ -1832,10 +1832,13 @@ void wxPostScriptDCImpl::EndDoc () #endif #ifndef __WXMSW__ + // Pointer to PrintNativeData not always points to wxPostScriptPrintNativeData, + // e.g. under wxGTK it can point to wxGtkPrintNativeData and so calling + // wxPostScriptPrintNativeData methods on it crashes. wxPostScriptPrintNativeData *data = - (wxPostScriptPrintNativeData *) m_printData.GetNativeData(); + wxDynamicCast(m_printData.GetNativeData(), wxPostScriptPrintNativeData); - if (m_ok && (m_printData.GetPrintMode() == wxPRINT_MODE_PRINTER)) + if (m_ok && data && (m_printData.GetPrintMode() == wxPRINT_MODE_PRINTER)) { wxString command; command += data->GetPrinterCommand(); @@ -1860,7 +1863,8 @@ void wxPostScriptDCImpl::StartPage() #if 0 wxPostScriptPrintNativeData *data = - (wxPostScriptPrintNativeData *) m_printData.GetNativeData(); + wxDynamicCast(m_printData.GetNativeData(), wxPostScriptPrintNativeData); + wxCHECK_RET( data, wxS("No PostScript print data") ); wxCoord translate_x = (wxCoord)data->GetPrinterTranslateX(); wxCoord translate_y = (wxCoord)data->GetPrinterTranslateY(); @@ -1931,15 +1935,18 @@ void wxPostScriptDCImpl::PsPrint( const wxString& str ) { const wxCharBuffer psdata(str.utf8_str()); - wxPostScriptPrintNativeData *data = - (wxPostScriptPrintNativeData *) m_printData.GetNativeData(); - switch (m_printData.GetPrintMode()) { #if wxUSE_STREAMS // append to output stream case wxPRINT_MODE_STREAM: { + // Pointer to PrintNativeData not always points to wxPostScriptPrintNativeData, + // e.g. under wxGTK it can point to wxGtkPrintNativeData and so calling + // wxPostScriptPrintNativeData methods on it crashes. + wxPostScriptPrintNativeData *data = + wxDynamicCast(m_printData.GetNativeData(), wxPostScriptPrintNativeData); + wxCHECK_RET( data, wxS("Cannot obtain output stream") ); wxOutputStream* outputstream = data->GetOutputStream(); wxCHECK_RET( outputstream, wxT("invalid outputstream") ); outputstream->Write( psdata, strlen( psdata ) );