crashtesting: fix PDF export of fdo45193-1.docx

This started with commit 1d3d2a995239c3c71432006cb795324c56a0412a
(tdf#148687 tdf#149173 tdf#149546 sw: fix crash with textboxes,
2022-06-20), the trouble is that once anchored objects are inserted to
the anchored object list of a layout frame, the list has to be re-sorted
if something that influences the sort key changes.

Normally the way to do this is similar to commit
0a6a151c4b7c78a363fb64598fbda39db4f42d07 (Related: tdf#70062 keep
drawing anchor objects sorted, 2015-02-11), but here the problematic
call is SetFormatAttr(SwFormatAnchor(...)) in
SwTextBoxHelper::changeAnchor(), and we don't have a layout by the time
that happens, so using UpdateObjInSortedList() is not something we can
do.

In any case looks like no proper fix is coming for this soon, so at
least fix it up so we don't try to do binary search on a vector that's
not yet sorted.

This also seems to help 3 other documents from crashtesting, and also
the tdf#157571 bugdoc (related to sw floattable), which is why I started
looking at this in the first place.

Change-Id: I9b532a39f6c28604304fa9bbecf9faf9842739a1
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/157691
Reviewed-by: Miklos Vajna <vmiklos@collabora.com>
Tested-by: Jenkins
(cherry picked from commit d530651e4e70febb1046727e85a8edcda610d722)
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/157710
Reviewed-by: Michael Stahl <michael.stahl@allotropia.de>
diff --git a/sw/CppunitTest_sw_core_layout.mk b/sw/CppunitTest_sw_core_layout.mk
index ae945c89ee..94ef1f0 100644
--- a/sw/CppunitTest_sw_core_layout.mk
+++ b/sw/CppunitTest_sw_core_layout.mk
@@ -18,6 +18,7 @@ $(eval $(call gb_CppunitTest_add_exception_objects,sw_core_layout, \
    sw/qa/core/layout/layact \
    sw/qa/core/layout/layout \
    sw/qa/core/layout/paintfrm \
    sw/qa/core/layout/sortedobjs \
    sw/qa/core/layout/tabfrm \
))

diff --git a/sw/qa/core/layout/data/sorted-objs-insert.docx b/sw/qa/core/layout/data/sorted-objs-insert.docx
new file mode 100644
index 0000000..4041ab9
--- /dev/null
+++ b/sw/qa/core/layout/data/sorted-objs-insert.docx
Binary files differ
diff --git a/sw/qa/core/layout/sortedobjs.cxx b/sw/qa/core/layout/sortedobjs.cxx
new file mode 100644
index 0000000..471dd48
--- /dev/null
+++ b/sw/qa/core/layout/sortedobjs.cxx
@@ -0,0 +1,35 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#include <swmodeltestbase.hxx>

namespace
{
/// Covers sw/source/core/layout/sortedobjs.cxx fixes.
class Test : public SwModelTestBase
{
public:
    Test()
        : SwModelTestBase("/sw/qa/core/layout/data/")
    {
    }
};

CPPUNIT_TEST_FIXTURE(Test, testSortedObjsInsert)
{
    // Given a document with two anchored objects, one is a fly frame in the header and the other is
    // a group shape where one inner shape has an associated fly frame:
    // When importing that document:
    // Then make sure that we don't try to do binary search on an unsorted container that leads to a
    // crash in debug builds:
    createSwDoc("sorted-objs-insert.docx");
}
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/sw/source/core/layout/sortedobjs.cxx b/sw/source/core/layout/sortedobjs.cxx
index da388bd..ce581bb 100644
--- a/sw/source/core/layout/sortedobjs.cxx
+++ b/sw/source/core/layout/sortedobjs.cxx
@@ -207,6 +207,12 @@ bool SwSortedObjs::is_sorted() const

bool SwSortedObjs::Insert( SwAnchoredObject& _rAnchoredObj )
{
    if (!is_sorted())
    {
        SAL_WARN("sw.core", "SwSortedObjs::Insert: object list is not sorted");
        UpdateAll();
    }

    // #i51941#
    if ( Contains( _rAnchoredObj ) )
    {