QLinkedList: fix incorrect size if operator+=() throws
The logic in operator+=() tried to restore the original list if
appending a new element threw an exception.
However, the implementation was wrong, because it updated the size
before all the elements were added, and the restore logic was comparing
to d->size, while also calling removeLast() in a loop, which modified
d->size on its own.
This could cause a problem that the container reported an incorrect
size() - larger or smaller than the actual amount of elements, depending
on the sizes of the original and appended containers, and the index of
the element that threw an exception.
In practice that could lead to an infinite loop in a code like:
while (!list.isEmpty())
list.removeLast();
and also to dereferencing an invalid iterator when accessing front()
or last() on a container that was actually empty, but reported a
non-zero size.
Fix it by updating the append logic - increase the actual size of
the container one-by-one, when a new element is actually added. When
handling the exception - calculate the number of elements that were
already added, and call removeLast() exactly that amount of times.
Add a new data-driven unit-test that uses a custom class that can be
configured to throw exceptions in the copy-constructor. The test
checks various sizes of the combined containers.
Fixes: QTBUG-149006
Pick-to: 6.12 6.11 6.8
Change-Id: I916c11bb2f845f113d8674750cc47c53bcb614ee
Reviewed-by: Marc Mutz <marc.mutz@qt.io>
Reviewed-by: MÃ¥rten Nordheim <marten.nordheim@qt.io>