From 0bd647fa4ffb3319bf3b1d044441b137910629e1 Mon Sep 17 00:00:00 2001 From: Andrei Golubev Date: Thu, 30 Jul 2020 11:50:01 +0200 Subject: [PATCH] Reorder operations to align with exception model in qarraydataops.h Fixed order of certain operations to better handle situations when exceptions occur Change-Id: Ia2075c37b4b7653067dfa6a82252cbb12b96708f Reviewed-by: Thiago Macieira --- src/corelib/tools/qarraydataops.h | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/src/corelib/tools/qarraydataops.h b/src/corelib/tools/qarraydataops.h index 369a8ae5742..57440703e5f 100644 --- a/src/corelib/tools/qarraydataops.h +++ b/src/corelib/tools/qarraydataops.h @@ -495,13 +495,17 @@ struct QGenericArrayOps // Construct new elements in array while (writeIter != step1End) { - --readIter, --writeIter; - new (writeIter) T(*readIter); + --readIter; + // If exception happens on construction, we should not call ~T() + new (writeIter - 1) T(*readIter); + --writeIter; } while (writeIter != end) { - --e, --writeIter; - new (writeIter) T(*e); + --e; + // If exception happens on construction, we should not call ~T() + new (writeIter - 1) T(*e); + --writeIter; } destroyer.commit(); @@ -538,13 +542,17 @@ struct QGenericArrayOps // Construct new elements in array while (writeIter != step1End) { - --readIter, --writeIter; - new (writeIter) T(*readIter); + --readIter; + // If exception happens on construction, we should not call ~T() + new (writeIter - 1) T(*readIter); + --writeIter; } while (writeIter != end) { - --n, --writeIter; - new (writeIter) T(t); + --n; + // If exception happens on construction, we should not call ~T() + new (writeIter - 1) T(t); + --writeIter; } destroyer.commit(); @@ -597,8 +605,9 @@ struct QGenericArrayOps // destroy the final elements at the end // here, b points to the new end and e to the actual end do { - (--e)->~T(); + // Exceptions or not, dtor called once per instance --this->size; + (--e)->~T(); } while (e != b); }