tdf#130857 formula: Only use selection indices if selection exists

The bool return value of weld::TextWidget::get_selection_bounds
indicates whether a text selection actually exists.

Only use the start and end indices from the out params
if that is the case and otherwise return a default-constructed
Selection object (i.e. one using start and end indices of 0).

Otherwise, this would result in an assertion due to the use of
invalid string indices as seen with the qt6 vcl plugin and native
widgets enabled (SAL_VCL_QT_USE_WELDED_WIDGETS=1) for the following
scenario with upcoming commit

    Change-Id: I10fcd9c326a047cf4abd34369f02be1847d77ae1
    Author: Michael Weghorn <m.weghorn@posteo.de>
    Date:   Wed Jul 22 14:20:53 2026 +0200

        tdf#130857 qt weld: Support Calc "Edit Print Ranges" dialog

in place:

* start Calc
* "Format" -> "Print Ranges" -> "Edit..."
* click on the "Shrink" button in the "Print Range" section
* click anywhere in the sheet

Assertion:

    soffice.bin: /home/michi/development/git/libreoffice/sal/rtl/strtmpl.hxx:1059: void rtl::str::newReplaceStrAt(rtl_tString **, rtl_tString *, sal_Int32, sal_Int32, rtl_tString *) [rtl_tString = _rtl_uString]: Assertion `nIndex >= 0 && nIndex <= pStr->length' failed.

This is because QtInstanceEntry::get_selection_bounds
sets the indices to -1 if no selection exists.

Change-Id: Ifc8fd6f6a6d676fe26364da9077cb10acb439a5a
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/208474
Tested-by: Jenkins
Reviewed-by: Michael Weghorn <m.weghorn@posteo.de>
This commit is contained in:
Michael Weghorn
2026-07-25 10:34:14 +02:00
parent 2c6f49415d
commit a881b303b3
2 changed files with 10 additions and 6 deletions
+9
View File
@@ -337,6 +337,15 @@ void RefEdit::SetReferences(IControlReferenceHandler* pDlg, weld::Frame* pFrame)
maGetLabelTextForShrinkModeFunc = [pFrame] { return pFrame->get_label(); };
}
Selection RefEdit::GetSelection() const
{
int nStartPos, nEndPos;
if (mxEntry->get_selection_bounds(nStartPos, nEndPos))
return Selection(nStartPos, nEndPos);
return Selection();
}
OUString RefEdit::GetLabelTextForShrinkMode()
{
if (maGetLabelTextForShrinkModeFunc)
+1 -6
View File
@@ -117,12 +117,7 @@ public:
mxEntry->set_position(-1);
}
Selection GetSelection() const
{
int nStartPos, nEndPos;
mxEntry->get_selection_bounds(nStartPos, nEndPos);
return Selection(nStartPos, nEndPos);
}
Selection GetSelection() const;
OUString GetLabelTextForShrinkMode();