sw/qa/uitest/ui/misc/misc.py      |    9 +++++----
 sw/source/uibase/wrtsh/wrtsh1.cxx |   27 +++++++++++++++------------
 2 files changed, 20 insertions(+), 16 deletions(-)

New commits:
commit 9186e01a0656cef3961e1495670c5f9114ee22ed
Author:     Attila Szűcs <attila.sz...@collabora.com>
AuthorDate: Fri Jul 12 14:51:41 2024 +0200
Commit:     Caolán McNamara <caolan.mcnam...@collabora.com>
CommitDate: Fri Jul 12 15:32:29 2024 +0200

    SW: random ID for new content controls
    
    Make the generated unique ID random for new content controls.
    This way if we copy paste them from different documents,
    they may still be unique.
    
    Change-Id: I2adb6577e08b5798ae88a63b6178352aa76280d6
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/170407
    Tested-by: Jenkins CollaboraOffice <jenkinscollaboraoff...@gmail.com>
    Tested-by: Caolán McNamara <caolan.mcnam...@collabora.com>
    Reviewed-by: Caolán McNamara <caolan.mcnam...@collabora.com>

diff --git a/sw/qa/uitest/ui/misc/misc.py b/sw/qa/uitest/ui/misc/misc.py
index 8af00054a5e6..94fd60bff16f 100644
--- a/sw/qa/uitest/ui/misc/misc.py
+++ b/sw/qa/uitest/ui/misc/misc.py
@@ -44,9 +44,10 @@ class TestTmpdlg(UITestCase):
                 type_text(xTag, "new tag ")
                 xAdd = xDialog.getChild("add")
 
-                xId = xDialog.getChild("idspinbutton")
-                self.assertEqual(get_state_as_dict(xId)['Text'], "0")
-                type_text(xId, "429496729") # added in front, making it 
4294967290
+                # Id is a random number now, not 0.
+                # xId = xDialog.getChild("idspinbutton")
+                # self.assertEqual(get_state_as_dict(xId)['Text'], "0")
+                # type_text(xId, "429496729") # added in front, making it 
4294967290
 
                 xTabIndex = xDialog.getChild("tabindexspinbutton")
                 self.assertEqual(get_state_as_dict(xTabIndex)['Text'], "1")
@@ -67,7 +68,7 @@ class TestTmpdlg(UITestCase):
             self.assertEqual(listItems[1][1].Value, "foo-bar")
             self.assertEqual(contentControl.Alias, "new alias my alias")
             self.assertEqual(contentControl.Tag, "new tag my tag")
-            self.assertEqual(contentControl.Id, -6) # stored as signed, 
displays as unsigned
+            # self.assertEqual(contentControl.Id, -6) # stored as signed, 
displays as unsigned
             self.assertEqual(contentControl.TabIndex, 4294967295) # stored as 
unsigned, displays as signed
 
 
diff --git a/sw/source/uibase/wrtsh/wrtsh1.cxx 
b/sw/source/uibase/wrtsh/wrtsh1.cxx
index 16c49cdd7d89..004aa95ad127 100644
--- a/sw/source/uibase/wrtsh/wrtsh1.cxx
+++ b/sw/source/uibase/wrtsh/wrtsh1.cxx
@@ -42,6 +42,7 @@
 #include <vcl/graph.hxx>
 #include <unotools/charclass.hxx>
 #include <comphelper/storagehelper.hxx>
+#include <comphelper/random.hxx>
 #include <svx/svxdlg.hxx>
 #include <svx/extrusionbar.hxx>
 #include <svx/fontworkbar.hxx>
@@ -1065,25 +1066,27 @@ void 
SwWrtShell::InsertContentControl(SwContentControlType eType)
 
     auto pContentControl = std::make_shared<SwContentControl>(nullptr);
 
-    // Search for a non used ID for the new ContentControl, to make it unique
+    // Make Random ID.. cehcek if it is unique
+    // warning: possible infinite loop if there would be billions of content 
controls.
     SwContentControlManager& pManager = GetDoc()->GetContentControlManager();
     size_t nCCCount = pManager.GetCount();
-    std::vector<bool> aIdMap(nCCCount);
-    aIdMap.resize(nCCCount, false);
-    size_t nIdToCheck;
-    for (nIdToCheck = 0; nIdToCheck < nCCCount; nIdToCheck++)
+    sal_Int32 nIdToCheck;
+    nIdToCheck
+        = comphelper::rng::uniform_uint_distribution(1, 
std::numeric_limits<sal_Int32>::max());
+    size_t nIdx = 0;
+    while (nIdx < nCCCount)
     {
         sal_Int32 nID
-            = 
pManager.UnsortedGet(nIdToCheck)->GetContentControl().GetContentControl()->GetId();
-        if (nID >= 0 && nID < static_cast<sal_Int32>(nCCCount))
+            = 
pManager.UnsortedGet(nIdx)->GetContentControl().GetContentControl()->GetId();
+        if (nID == nIdToCheck)
         {
-            aIdMap[nID] = true;
+            nIdToCheck = comphelper::rng::uniform_uint_distribution(
+                1, std::numeric_limits<sal_Int32>::max());
+            nIdx = 0;
         }
+        else
+            nIdx++;
     }
-    // Find the first ID that was not used
-    nIdToCheck = 0;
-    while (nIdToCheck < nCCCount && aIdMap[nIdToCheck])
-        nIdToCheck++;
     pContentControl->SetId(nIdToCheck);
 
     OUString aPlaceholder;

Reply via email to