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
This commit is contained in:
Artur Wieczorek
2017-02-04 00:24:51 +01:00
parent 6aff1c8c64
commit f7e88c0db6

View File

@@ -1832,10 +1832,13 @@ void wxPostScriptDCImpl::EndDoc ()
#endif #endif
#ifndef __WXMSW__ #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 *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; wxString command;
command += data->GetPrinterCommand(); command += data->GetPrinterCommand();
@@ -1860,7 +1863,8 @@ void wxPostScriptDCImpl::StartPage()
#if 0 #if 0
wxPostScriptPrintNativeData *data = 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_x = (wxCoord)data->GetPrinterTranslateX();
wxCoord translate_y = (wxCoord)data->GetPrinterTranslateY(); wxCoord translate_y = (wxCoord)data->GetPrinterTranslateY();
@@ -1931,15 +1935,18 @@ void wxPostScriptDCImpl::PsPrint( const wxString& str )
{ {
const wxCharBuffer psdata(str.utf8_str()); const wxCharBuffer psdata(str.utf8_str());
wxPostScriptPrintNativeData *data =
(wxPostScriptPrintNativeData *) m_printData.GetNativeData();
switch (m_printData.GetPrintMode()) switch (m_printData.GetPrintMode())
{ {
#if wxUSE_STREAMS #if wxUSE_STREAMS
// append to output stream // append to output stream
case wxPRINT_MODE_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(); wxOutputStream* outputstream = data->GetOutputStream();
wxCHECK_RET( outputstream, wxT("invalid outputstream") ); wxCHECK_RET( outputstream, wxT("invalid outputstream") );
outputstream->Write( psdata, strlen( psdata ) ); outputstream->Write( psdata, strlen( psdata ) );