From 46b9ecebc54c26e4616e98673b6684cf81dc1e53 Mon Sep 17 00:00:00 2001
From: "Peter W. Draper" <p.w.draper@durham.ac.uk>
Date: Mon, 12 Sep 2016 12:42:14 +0100
Subject: [PATCH] Add some sanity checking when splitting cells.

This depth is way beyond our precision.
---
 src/space.c | 6 ++++++
 src/space.h | 3 +++
 2 files changed, 9 insertions(+)

diff --git a/src/space.c b/src/space.c
index a9958f6fbd..9c163fede0 100644
--- a/src/space.c
+++ b/src/space.c
@@ -1269,6 +1269,12 @@ void space_split_mapper(void *map_data, int num_elements, void *extra_data) {
       atomic_cas(&s->maxdepth, maxdepth, c->depth);
     }
 
+    /* If the depth is too large, we have a problem and should stop. */
+    if (s->maxdepth > space_cell_maxdepth) {
+      error("Exceeded maximum depth (%d) when splitting cells, aborting",
+            space_cell_maxdepth);
+    }
+
     /* Split or let it be? */
     if (count > space_splitsize || gcount > space_splitsize) {
 
diff --git a/src/space.h b/src/space.h
index 72b17405f1..5332bc9fb5 100644
--- a/src/space.h
+++ b/src/space.h
@@ -44,6 +44,9 @@
 #define space_stretch 1.10f
 #define space_maxreldx 0.25f
 
+/* Maximum allowed depth of cell splits. */
+#define space_cell_maxdepth 52
+
 /* Split size. */
 extern int space_splitsize;
 extern int space_maxsize;
-- 
GitLab