From 3e6a3c078b38c46482b61e949f0406aaab6f5a1b Mon Sep 17 00:00:00 2001
From: Matthieu Schaller <matthieu.schaller@durham.ac.uk>
Date: Fri, 17 Jan 2020 15:58:38 +0000
Subject: [PATCH] Fix an undefined behaviour problem in
 engine_split_particles() by using the more expensive (but correct) functions
 to relink the gpart to other particles.

---
 src/engine_split_particles.c | 19 +++----------------
 1 file changed, 3 insertions(+), 16 deletions(-)

diff --git a/src/engine_split_particles.c b/src/engine_split_particles.c
index 65ceb1d42d..58d7f90558 100644
--- a/src/engine_split_particles.c
+++ b/src/engine_split_particles.c
@@ -140,26 +140,13 @@ void engine_split_gas_particles(struct engine *e) {
                        sizeof(struct gpart) * s->size_gparts) != 0)
       error("Failed to allocate new gpart data.");
 
-    /* Offset of the new array */
-    const ptrdiff_t offset = gparts_new - s->gparts;
-
     /* Copy the particles */
     memcpy(gparts_new, s->gparts, sizeof(struct gpart) * s->nr_gparts);
     swift_free("gparts", s->gparts);
 
-    /* We now need to correct all the pointers */
-    for (size_t i = 0; i < s->nr_parts; ++i) {
-      if (s->parts[i].time_bin <= num_time_bins) s->parts[i].gpart += offset;
-    }
-    /* We now need to correct all the pointers */
-    for (size_t i = 0; i < s->nr_sparts; ++i) {
-      if (s->sparts[i].time_bin <= num_time_bins) s->sparts[i].gpart += offset;
-    }
-    /* We now need to correct all the pointers */
-    for (size_t i = 0; i < s->nr_bparts; ++i) {
-      if (s->bparts[i].time_bin <= num_time_bins) s->bparts[i].gpart += offset;
-    }
-
+    /* We now need to correct all the pointers of the other particle arrays */
+    part_relink_all_parts_to_gparts(gparts_new, s->nr_gparts, s->parts,
+                                    s->sparts, s->bparts);
     s->gparts = gparts_new;
   }
 
-- 
GitLab