Drop non-const Sequence::operator[] in internal code
commitfb3c04bd1930eedacd406874e1a285d62bbf27d9
authorMike Kaganski <mike.kaganski@collabora.com>
Fri, 29 Oct 2021 07:30:22 +0000 (29 10:30 +0300)
committerMike Kaganski <mike.kaganski@collabora.com>
Fri, 5 Nov 2021 11:30:28 +0000 (5 12:30 +0100)
treef201620bda3d0dcf0309adc73419aedc7d96a026
parent1a62041a44be435b2588680c94294c0f0cbf13c8
Drop non-const Sequence::operator[] in internal code

This makes all non-const operations on Sequence explicit, to avoid
repeated COW checks in loops. Generally it would be desirable to
replace uses of Sequence with general-purpose containers wherever
possible, and only use Sequence for UNO API calls.

This change uncovered a very serious pre-existing problem inherent
to the Sequences used e.g. in range-based loops in our code: taking
a non-const reference to elements of a sequence, and then modifying
them at some later stage, brings a danger to also modify copies of
the Sequence, that were created between the points of taking the
reference and modifying it. This caused the change to
oox/source/drawingml/customshapeproperties.cxx, where
CustomShapeProperties::pushToPropSet took begin()/end() non-const
iterators to aGeoPropSeq at the start of the loop, and then in the
loop modified its elements and copied the Sequence passing it to
XPropertySet::setPropertyValue. This was the same also prior to
2484de6728bd11bb7949003d112f1ece2223c7a1, and only happened to not
cause problems because of *accidental* use of non-const operator[]
on the copy of the Sequence inside SdrCustomShapeGeometryItem ctor,
which *inadvertently* triggered COW, detaching the two copies one
from another.
This only emphasizes that we should minimize use of Sequences in
the codebase. I anticipate other similar problems uncovered by this
change, that happened to not break existing unit tests.

Change-Id: Id691d994a06eb14297c487ebb84d8e062e29fd47
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/123725
Tested-by: Mike Kaganski <mike.kaganski@collabora.com>
Reviewed-by: Mike Kaganski <mike.kaganski@collabora.com>
include/com/sun/star/uno/Sequence.h
include/com/sun/star/uno/Sequence.hxx
oox/source/drawingml/customshapeproperties.cxx