From 96bd77de5ad7b7a13f7e48e0f95c05ef49255aa0 Mon Sep 17 00:00:00 2001 From: Mike Kaganski Date: Mon, 13 Sep 2021 11:29:37 +0300 Subject: Use implementing XUnoTunnel part 5 - Revise uses of getSomething to use getFromUnoTunnel Where that is impossible, use getSomething_cast to unify casting, and minimize number of places doing low-level transformations. The change keeps the existing tunnel references that last for the duration of the pointers' life, because sometimes destroying such reference may destroy the pointed object, and result in use after free. Change-Id: I291c33223582c34cd2c763aa8aacf0ae899ca4c0 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/122101 Tested-by: Jenkins Reviewed-by: Mike Kaganski --- sw/source/filter/xml/xmlexp.cxx | 3 +-- sw/source/filter/xml/xmlimp.cxx | 13 +++---------- sw/source/filter/xml/xmltble.cxx | 8 +------- sw/source/filter/xml/xmltbli.cxx | 17 ++++------------- sw/source/filter/xml/xmltexte.cxx | 3 +-- sw/source/filter/xml/xmltexti.cxx | 21 +++++++-------------- 6 files changed, 17 insertions(+), 48 deletions(-) (limited to 'sw/source/filter/xml') diff --git a/sw/source/filter/xml/xmlexp.cxx b/sw/source/filter/xml/xmlexp.cxx index f0255f7833f2..437a1a9c76f6 100644 --- a/sw/source/filter/xml/xmlexp.cxx +++ b/sw/source/filter/xml/xmlexp.cxx @@ -501,8 +501,7 @@ SwDoc* SwXMLExport::getDoc() Reference < XText > xText = xTextDoc->getText(); Reference xTextTunnel( xText, UNO_QUERY); assert( xTextTunnel.is()); - SwXText *pText = reinterpret_cast< SwXText *>( - sal::static_int_cast< sal_IntPtr >( xTextTunnel->getSomething( SwXText::getUnoTunnelId() ))); + SwXText* pText = comphelper::getFromUnoTunnel(xTextTunnel); assert( pText != nullptr ); m_pDoc = pText->GetDoc(); assert( m_pDoc != nullptr ); diff --git a/sw/source/filter/xml/xmlimp.cxx b/sw/source/filter/xml/xmlimp.cxx index 6bfdcf0f0698..eea1ca435720 100644 --- a/sw/source/filter/xml/xmlimp.cxx +++ b/sw/source/filter/xml/xmlimp.cxx @@ -374,12 +374,7 @@ sal_Int64 SAL_CALL SwXMLImport::getSomething( const Sequence< sal_Int8 >& rId ) static OTextCursorHelper *lcl_xml_GetSwXTextCursor( const Reference < XTextCursor >& rTextCursor ) { - Reference xCursorTunnel( rTextCursor, UNO_QUERY ); - OSL_ENSURE( xCursorTunnel.is(), "missing XUnoTunnel for Cursor" ); - if( !xCursorTunnel.is() ) - return nullptr; - OTextCursorHelper *pTextCursor = reinterpret_cast< OTextCursorHelper *>( - sal::static_int_cast< sal_IntPtr >( xCursorTunnel->getSomething( OTextCursorHelper::getUnoTunnelId() ))); + OTextCursorHelper* pTextCursor = comphelper::getFromUnoTunnel(rTextCursor); OSL_ENSURE( pTextCursor, "SwXTextCursor missing" ); return pTextCursor; } @@ -648,8 +643,7 @@ void SwXMLImport::endDocument() Reference xCursorTunnel( GetTextImport()->GetCursor(), UNO_QUERY); assert(xCursorTunnel.is() && "missing XUnoTunnel for Cursor"); - OTextCursorHelper *pTextCursor = reinterpret_cast< OTextCursorHelper *>( - sal::static_int_cast< sal_IntPtr >( xCursorTunnel->getSomething( OTextCursorHelper::getUnoTunnelId() ))); + OTextCursorHelper* pTextCursor = comphelper::getFromUnoTunnel(xCursorTunnel); assert(pTextCursor && "SwXTextCursor missing"); SwPaM *pPaM = pTextCursor->GetPaM(); if( IsInsertMode() && m_pSttNdIdx->GetIndex() ) @@ -1655,8 +1649,7 @@ SwDoc* SwXMLImport::getDoc() Reference < XText > xText = xTextDoc->getText(); Reference xTextTunnel( xText, UNO_QUERY); assert( xTextTunnel.is()); - SwXText *pText = reinterpret_cast< SwXText *>( - sal::static_int_cast< sal_IntPtr >( xTextTunnel->getSomething( SwXText::getUnoTunnelId() ))); + SwXText* pText = comphelper::getFromUnoTunnel(xTextTunnel); assert( pText != nullptr ); m_pDoc = pText->GetDoc(); assert( m_pDoc != nullptr ); diff --git a/sw/source/filter/xml/xmltble.cxx b/sw/source/filter/xml/xmltble.cxx index 4787961efcb4..3a3409cd1b46 100644 --- a/sw/source/filter/xml/xmltble.cxx +++ b/sw/source/filter/xml/xmltble.cxx @@ -1212,14 +1212,8 @@ void SwXMLTextParagraphExport::exportTable( OSL_ENSURE( xTextTable.is(), "text table missing" ); if( xTextTable.is() ) { - SwXTextTable *pXTable = nullptr; Reference xTableTunnel( rTextContent, UNO_QUERY); - if( xTableTunnel.is() ) - { - pXTable = reinterpret_cast< SwXTextTable * >( - sal::static_int_cast< sal_IntPtr >( xTableTunnel->getSomething( SwXTextTable::getUnoTunnelId() ))); - OSL_ENSURE( pXTable, "SwXTextTable missing" ); - } + SwXTextTable* pXTable = comphelper::getFromUnoTunnel(xTableTunnel); if( pXTable ) { SwFrameFormat *const pFormat = pXTable->GetFrameFormat(); diff --git a/sw/source/filter/xml/xmltbli.cxx b/sw/source/filter/xml/xmltbli.cxx index 439c7df0f631..a071ce35ec67 100644 --- a/sw/source/filter/xml/xmltbli.cxx +++ b/sw/source/filter/xml/xmltbli.cxx @@ -595,8 +595,7 @@ void SwXMLTableCellContext_Impl::endFastElement(sal_Int32 ) // Until we have an API for copying we have to use the core. Reference xSrcCursorTunnel( xSrcTextCursor, UNO_QUERY); assert(xSrcCursorTunnel.is() && "missing XUnoTunnel for Cursor"); - OTextCursorHelper *pSrcTextCursor = reinterpret_cast< OTextCursorHelper * >( - sal::static_int_cast< sal_IntPtr >( xSrcCursorTunnel->getSomething( OTextCursorHelper::getUnoTunnelId() ))); + OTextCursorHelper *pSrcTextCursor = comphelper::getFromUnoTunnel(xSrcTextCursor); assert(pSrcTextCursor && "SwXTextCursor missing"); SwDoc *pDoc = pSrcTextCursor->GetDoc(); const SwPaM *pSrcPaM = pSrcTextCursor->GetPaM(); @@ -608,8 +607,7 @@ void SwXMLTableCellContext_Impl::endFastElement(sal_Int32 ) Reference xDstCursorTunnel( GetImport().GetTextImport()->GetCursor(), UNO_QUERY); assert(xDstCursorTunnel.is() && "missing XUnoTunnel for Cursor"); - OTextCursorHelper *pDstTextCursor = reinterpret_cast< OTextCursorHelper * >( - sal::static_int_cast< sal_IntPtr >( xDstCursorTunnel->getSomething( OTextCursorHelper::getUnoTunnelId() )) ); + OTextCursorHelper *pDstTextCursor = comphelper::getFromUnoTunnel(GetImport().GetTextImport()->GetCursor()); assert(pDstTextCursor && "SwXTextCursor missing"); SwPaM aSrcPaM(*pSrcPaM->GetMark(), *pSrcPaM->GetPoint()); SwPosition aDstPos( *pDstTextCursor->GetPaM()->GetPoint() ); @@ -1220,13 +1218,7 @@ SwXMLTableContext::SwXMLTableContext( SwXMLImport& rImport, // xml:id for RDF metadata GetImport().SetXmlId(xTable, sXmlId); - Reference xTableTunnel( xTable, UNO_QUERY); - if( xTableTunnel.is() ) - { - pXTable = reinterpret_cast< SwXTextTable * >( - sal::static_int_cast< sal_IntPtr >( xTableTunnel->getSomething( SwXTextTable::getUnoTunnelId() ))); - OSL_ENSURE( pXTable, "SwXTextTable missing" ); - } + pXTable = comphelper::getFromUnoTunnel(xTable); Reference < XCellRange > xCellRange( xTable, UNO_QUERY ); Reference < XCell > xCell = xCellRange->getCellByPosition( 0, 0 ); @@ -2698,8 +2690,7 @@ const SwStartNode *SwXMLTableContext::InsertTableSection( Reference xCursorTunnel( GetImport().GetTextImport()->GetCursor(), UNO_QUERY); OSL_ENSURE( xCursorTunnel.is(), "missing XUnoTunnel for Cursor" ); - OTextCursorHelper *pTextCursor = reinterpret_cast< OTextCursorHelper * >( - sal::static_int_cast< sal_IntPtr >( xCursorTunnel->getSomething( OTextCursorHelper::getUnoTunnelId() ))); + OTextCursorHelper *pTextCursor = comphelper::getFromUnoTunnel(xCursorTunnel); OSL_ENSURE( pTextCursor, "SwXTextCursor missing" ); if( m_bFirstSection ) diff --git a/sw/source/filter/xml/xmltexte.cxx b/sw/source/filter/xml/xmltexte.cxx index 0210db520e6d..e5b8dc181aef 100644 --- a/sw/source/filter/xml/xmltexte.cxx +++ b/sw/source/filter/xml/xmltexte.cxx @@ -72,8 +72,7 @@ SwNoTextNode *SwXMLTextParagraphExport::GetNoTextNode( { Reference xCursorTunnel( rPropSet, UNO_QUERY ); assert(xCursorTunnel.is() && "missing XUnoTunnel for embedded"); - SwXFrame *pFrame = reinterpret_cast< SwXFrame * >( - sal::static_int_cast< sal_IntPtr >( xCursorTunnel->getSomething( SwXFrame::getUnoTunnelId() ))); + SwXFrame* pFrame = comphelper::getFromUnoTunnel(xCursorTunnel); assert(pFrame && "SwXFrame missing"); SwFrameFormat *pFrameFormat = pFrame->GetFrameFormat(); const SwFormatContent& rContent = pFrameFormat->GetContent(); diff --git a/sw/source/filter/xml/xmltexti.cxx b/sw/source/filter/xml/xmltexti.cxx index 0c10b3ba9eb0..f73ef35be731 100644 --- a/sw/source/filter/xml/xmltexti.cxx +++ b/sw/source/filter/xml/xmltexti.cxx @@ -191,8 +191,7 @@ bool SwXMLTextImportHelper::IsInHeaderFooter() const uno::Reference xCursorTunnel( const_cast(this)->GetCursor(), UNO_QUERY ); assert(xCursorTunnel.is() && "missing XUnoTunnel for Cursor"); - OTextCursorHelper *pTextCursor = reinterpret_cast< OTextCursorHelper * >( - sal::static_int_cast< sal_IntPtr >( xCursorTunnel->getSomething( OTextCursorHelper::getUnoTunnelId() ))); + OTextCursorHelper* pTextCursor = comphelper::getFromUnoTunnel(xCursorTunnel); SAL_WARN_IF(!pTextCursor, "sw.uno", "SwXTextCursor missing"); SwDoc *pDoc = pTextCursor ? pTextCursor->GetDoc() : nullptr; @@ -235,8 +234,7 @@ uno::Reference< XPropertySet > SwXMLTextImportHelper::createAndInsertOLEObject( uno::Reference xCursorTunnel( GetCursor(), UNO_QUERY ); assert(xCursorTunnel.is() && "missing XUnoTunnel for Cursor"); - OTextCursorHelper *pTextCursor = reinterpret_cast< OTextCursorHelper * >( - sal::static_int_cast< sal_IntPtr >( xCursorTunnel->getSomething( OTextCursorHelper::getUnoTunnelId() ))); + OTextCursorHelper* pTextCursor = comphelper::getFromUnoTunnel(xCursorTunnel); SAL_WARN_IF(!pTextCursor, "sw.uno", "SwXTextCursor missing"); SwDoc *pDoc = SwImport::GetDocFromXMLImport( rImport ); @@ -547,8 +545,7 @@ uno::Reference< XPropertySet > SwXMLTextImportHelper::createAndInsertOOoLink( uno::Reference xCursorTunnel( GetCursor(), UNO_QUERY ); assert(xCursorTunnel.is() && "missing XUnoTunnel for Cursor"); - OTextCursorHelper *pTextCursor = reinterpret_cast< OTextCursorHelper * >( - sal::static_int_cast< sal_IntPtr >( xCursorTunnel->getSomething( OTextCursorHelper::getUnoTunnelId() ))); + OTextCursorHelper* pTextCursor = comphelper::getFromUnoTunnel(xCursorTunnel); OSL_ENSURE( pTextCursor, "SwXTextCursor missing" ); SwDoc *pDoc = SwImport::GetDocFromXMLImport( rImport ); @@ -639,8 +636,7 @@ uno::Reference< XPropertySet > SwXMLTextImportHelper::createAndInsertApplet( uno::Reference < XPropertySet > xPropSet; uno::Reference xCursorTunnel( GetCursor(), UNO_QUERY ); assert(xCursorTunnel.is() && "missing XUnoTunnel for Cursor"); - OTextCursorHelper *pTextCursor = reinterpret_cast< OTextCursorHelper * >( - sal::static_int_cast< sal_IntPtr >( xCursorTunnel->getSomething( OTextCursorHelper::getUnoTunnelId() ))); + OTextCursorHelper* pTextCursor = comphelper::getFromUnoTunnel(xCursorTunnel); OSL_ENSURE( pTextCursor, "SwXTextCursor missing" ); SwDoc *pDoc = pTextCursor->GetDoc(); @@ -685,8 +681,7 @@ uno::Reference< XPropertySet > SwXMLTextImportHelper::createAndInsertPlugin( uno::Reference < XPropertySet > xPropSet; uno::Reference xCursorTunnel( GetCursor(), UNO_QUERY ); assert(xCursorTunnel.is() && "missing XUnoTunnel for Cursor"); - OTextCursorHelper *pTextCursor = reinterpret_cast< OTextCursorHelper * >( - sal::static_int_cast< sal_IntPtr >( xCursorTunnel->getSomething( OTextCursorHelper::getUnoTunnelId() ))); + OTextCursorHelper* pTextCursor = comphelper::getFromUnoTunnel(xCursorTunnel); OSL_ENSURE( pTextCursor, "SwXTextCursor missing" ); SwDoc *pDoc = pTextCursor->GetDoc(); @@ -766,8 +761,7 @@ uno::Reference< XPropertySet > SwXMLTextImportHelper::createAndInsertFloatingFra uno::Reference < XPropertySet > xPropSet; uno::Reference xCursorTunnel( GetCursor(), UNO_QUERY ); assert(xCursorTunnel.is() && "missing XUnoTunnel for Cursor"); - OTextCursorHelper *pTextCursor = reinterpret_cast< OTextCursorHelper * >( - sal::static_int_cast< sal_IntPtr >( xCursorTunnel->getSomething( OTextCursorHelper::getUnoTunnelId() ))); + OTextCursorHelper* pTextCursor = comphelper::getFromUnoTunnel(xCursorTunnel); OSL_ENSURE( pTextCursor, "SwXTextCursor missing" ); SwDoc *pDoc = pTextCursor->GetDoc(); @@ -916,8 +910,7 @@ void SwXMLTextImportHelper::endAppletOrPlugin( uno::Reference xCursorTunnel( rPropSet, UNO_QUERY ); assert(xCursorTunnel.is() && "missing XUnoTunnel for embedded"); - SwXFrame *pFrame = reinterpret_cast< SwXFrame * >( - sal::static_int_cast< sal_IntPtr >( xCursorTunnel->getSomething( SwXFrame::getUnoTunnelId() ))); + SwXFrame* pFrame = comphelper::getFromUnoTunnel(xCursorTunnel); OSL_ENSURE( pFrame, "SwXFrame missing" ); SwFrameFormat *pFrameFormat = pFrame->GetFrameFormat(); const SwFormatContent& rContent = pFrameFormat->GetContent(); -- cgit