tdf#153858 sw: crash fix for chart with deleted data table

Using mouse, e.g. simply moving the mouse pointer
during editing a chart with a deleted data table
crashed Writer immediately.

Follow-up to commit 5b9855acc7fa6d1e4a5f53ff0bc47e1dd4729827
"tdf#132714 sw: fix crash at table row deletion associated
to a chart".

Change-Id: I6d89eabc84565c548e2d9ded922789d623367ce4
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/147882
Tested-by: László Németh <nemeth@numbertext.org>
Reviewed-by: László Németh <nemeth@numbertext.org>
(cherry picked from commit 4f2dcc4bc70c3602e2612dab611b610410637920)
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/147898
Tested-by: Jenkins
Reviewed-by: Caolán McNamara <caolanm@redhat.com>
diff --git a/chart2/source/controller/dialogs/ObjectNameProvider.cxx b/chart2/source/controller/dialogs/ObjectNameProvider.cxx
index 2bf6af62..c60ff1c 100644
--- a/chart2/source/controller/dialogs/ObjectNameProvider.cxx
+++ b/chart2/source/controller/dialogs/ObjectNameProvider.cxx
@@ -124,13 +124,15 @@ OUString lcl_getDataPointValueText( const rtl::Reference< DataSeries >& xSeries,
        uno::Reference<data::XDataSequence>  xDataSequence( aDataSequences[nN]->getValues());
        if( !xDataSequence.is() )
            continue;
        Sequence< Any > aData( xDataSequence->getData() );
        if( nPointIndex >= aData.getLength() )
            continue;
        uno::Reference<beans::XPropertySet> xProp(xDataSequence, uno::UNO_QUERY );
        if( xProp.is())

        try
        {
            try
            Sequence< Any > aData( xDataSequence->getData() );

            if( nPointIndex >= aData.getLength() )
                continue;
            uno::Reference<beans::XPropertySet> xProp(xDataSequence, uno::UNO_QUERY );
            if( xProp.is())
            {
                uno::Any aARole = xProp->getPropertyValue( "Role" );
                OUString aRole;
@@ -179,10 +181,14 @@ OUString lcl_getDataPointValueText( const rtl::Reference< DataSeries >& xSeries,
                    a_Size = aNumberFormatterWrapper.getFormattedString( nNumberFormatKey, fValue, nLabelColor, bColorChanged );
                }
            }
            catch( const uno::Exception& )
            {
                TOOLS_WARN_EXCEPTION("chart2", "" );
            }
        }
        catch (const lang::DisposedException&)
        {
            TOOLS_WARN_EXCEPTION( "chart2", "unexpected exception caught" );
        }
        catch( const uno::Exception& )
        {
            TOOLS_WARN_EXCEPTION("chart2", "" );
        }
    }

diff --git a/chart2/source/tools/DataSeriesHelper.cxx b/chart2/source/tools/DataSeriesHelper.cxx
index e2bc3cf..47b3ad9 100644
--- a/chart2/source/tools/DataSeriesHelper.cxx
+++ b/chart2/source/tools/DataSeriesHelper.cxx
@@ -208,10 +208,17 @@ uno::Reference< chart2::data::XLabeledDataSequence >
    if( ! xSource.is())
        return aNoResult;
    const Sequence< Reference< chart2::data::XLabeledDataSequence > > aLabeledSeq( xSource->getDataSequences());
    for (auto const & i : aLabeledSeq)
    try
    {
        if (lcl_MatchesRole(aRole, bMatchPrefix)(i))
            return i;
        for (auto const & i : aLabeledSeq)
        {
            if (lcl_MatchesRole(aRole, bMatchPrefix)(i))
                return i;
        }
    }
    catch (const lang::DisposedException&)
    {
        TOOLS_WARN_EXCEPTION( "chart2", "unexpected exception caught" );
    }

    return aNoResult;
diff --git a/sw/qa/uitest/writer_tests7/tdf132714.py b/sw/qa/uitest/writer_tests7/tdf132714.py
index 971db5f..7ccedbe 100644
--- a/sw/qa/uitest/writer_tests7/tdf132714.py
+++ b/sw/qa/uitest/writer_tests7/tdf132714.py
@@ -8,6 +8,9 @@
#
from uitest.framework import UITestCase
from uitest.uihelper.common import get_url_for_data_file
from libreoffice.uno.propertyvalue import mkPropertyValues
from com.sun.star.awt import MouseButton
from com.sun.star.awt import MouseEvent

class tdf132714(UITestCase):
    def test_tdf132714(self):
@@ -20,4 +23,47 @@ class tdf132714(UITestCase):
            # Without the fix in place, at this point crash occurs.
            self.xUITest.executeCommand(".uno:DeleteRows")

    def test_delete_table(self):
        with self.ui_test.load_file(get_url_for_data_file("tdf132714.odt")) as document:

            # delete second row (first data row) in the associated text table of the chart
            self.xUITest.executeCommand(".uno:GoDown")
            self.xUITest.executeCommand(".uno:GoDown")
            # Without the fix in place, at this point crash occurs.
            self.xUITest.executeCommand(".uno:DeleteTable")

            # select embedded chart
            self.assertEqual(1, document.EmbeddedObjects.Count)
            document.CurrentController.select(document.getEmbeddedObjects().getByIndex(0))
            self.assertEqual("SwXTextEmbeddedObject", document.CurrentSelection.getImplementationName())

            xChartMainTop = self.xUITest.getTopFocusWindow()
            xWriterEdit = xChartMainTop.getChild("writer_edit")
            # edit object by pressing Enter
            xWriterEdit.executeAction("TYPE", mkPropertyValues({"KEYCODE":"RETURN"}))

            # create mouse event in the chart area
            xFrame = document.getCurrentController().getFrame()
            self.assertIsNotNone(xFrame)
            xWindow = xFrame.getContainerWindow()
            self.assertIsNotNone(xWindow)

            xMouseEvent = MouseEvent()
            xMouseEvent.Modifiers = 0
            xMouseEvent.Buttons = MouseButton.LEFT
            xMouseEvent.X = 1000
            xMouseEvent.Y = 400
            xMouseEvent.ClickCount = 1
            xMouseEvent.PopupTrigger = False
            xMouseEvent.Source = xWindow

            # send mouse event
            xToolkitRobot = xWindow.getToolkit()
            self.assertIsNotNone(xToolkitRobot)

            # Click in the chart area

            # Without the fix in place, this test would have crashed here
            xToolkitRobot.mouseMove(xMouseEvent)

# vim: set shiftwidth=4 softtabstop=4 expandtab:
diff --git a/sw/source/core/unocore/unochart.cxx b/sw/source/core/unocore/unochart.cxx
index 87dbbcc..33ceb02 100644
--- a/sw/source/core/unocore/unochart.cxx
+++ b/sw/source/core/unocore/unochart.cxx
@@ -24,6 +24,7 @@
#include <com/sun/star/chart2/data/LabelOrigin.hpp>
#include <com/sun/star/embed/XEmbeddedObject.hpp>
#include <com/sun/star/frame/XModel.hpp>
#include <comphelper/diagnose_ex.hxx>
#include <cppuhelper/supportsservice.hxx>
#include <o3tl/deleter.hxx>
#include <o3tl/string_view.hxx>
@@ -2057,14 +2058,22 @@ uno::Sequence< OUString > SAL_CALL SwChartDataSequence::getTextualData()
uno::Sequence< uno::Any > SAL_CALL SwChartDataSequence::getData()
{
    SolarMutexGuard aGuard;
    auto vCells(GetCells());
    uno::Sequence< uno::Any > vAnyData(vCells.size());
    std::transform(vCells.begin(),
        vCells.end(),
        vAnyData.getArray(),
        [] (decltype(vCells)::value_type& xCell)
            { return static_cast<SwXCell*>(xCell.get())->GetAny(); });
    return vAnyData;
    try
    {
        auto vCells(GetCells());
        uno::Sequence< uno::Any > vAnyData(vCells.size());
        std::transform(vCells.begin(),
            vCells.end(),
            vAnyData.getArray(),
            [] (decltype(vCells)::value_type& xCell)
                { return static_cast<SwXCell*>(xCell.get())->GetAny(); });
        return vAnyData;
    }
    catch (const lang::DisposedException&)
    {
        TOOLS_WARN_EXCEPTION( "sw", "unexpected exception caught" );
    }
    return uno::Sequence< uno::Any >{};
}

uno::Sequence< double > SAL_CALL SwChartDataSequence::getNumericalData()