File 0001-Fix-dangling-pointers.patch of Package libkolab-old

From 4bda8a1cb3befa5dc4b03c6590640053afb74ef5 Mon Sep 17 00:00:00 2001
From: Christoph Erhardt <kolab@sicherha.de>
Date: Tue, 25 May 2021 08:57:22 +0200
Subject: [PATCH 1/2] Fix dangling pointers

std::vector gives zero guarantees that pointers to its elements remain
valid when the vector's size changes. In particular, pushing new
elements into the vector may trigger reallocation of the underlying heap
area.

Consequently, Event::delegate() needs to ensure that any modifications
to the d->attendees vector are performed before pointers to its elements
are taken and collected.

Found with Valgrind.


Reviewers: mollekopf

Reviewed By: mollekopf

Subscribers: mollekopf

Differential Revision: https://git.kolab.org/D2548
---
 calendaring/event.cpp | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)

diff --git a/calendaring/event.cpp b/calendaring/event.cpp
index bcdf833..ea9ead1 100644
--- a/calendaring/event.cpp
+++ b/calendaring/event.cpp
@@ -76,17 +76,22 @@ bool contains(const Kolab::ContactReference &delegatorRef, const std::vector <Ko
 void Event::delegate(const std::vector< Attendee >& delegators, const std::vector< Attendee >& delegatees)
 {
 
-    //First build a list of attendee references, and insert any missing attendees
-    std::vector<Kolab::Attendee*> delegateesRef;
+    //First insert any missing attendees
     foreach(const Attendee &a, delegatees) {
-        if (Attendee *attendee = getAttendee(a.contact())) {
-            delegateesRef.push_back(attendee);
-        } else {
+        if (!getAttendee(a.contact())) {
             d->attendees.push_back(a);
-            delegateesRef.push_back(&d->attendees.back());
         }
     }
 
+    //Build a list of attendee references
+    //These are pointers into d->attendees, so we MUST NOT modify that vector after this point!
+    std::vector<Kolab::Attendee*> delegateesRef;
+    foreach(const Attendee &a, delegatees) {
+        Attendee *attendee = getAttendee(a.contact());
+        Q_ASSERT(attendee);
+        delegateesRef.push_back(attendee);
+    }
+
     std::vector<Kolab::Attendee*> delegatorsRef;
     foreach(const Attendee& a, delegators) {
         if (Attendee *attendee = getAttendee(a.contact())) {
-- 
2.31.1