Jeroen Demeyer on Mon, 03 Apr 2017 15:28:05 +0200


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

When enlarging stack in new_chunk_resize(), add 1/8 to stack size


This patch improves the implementation of new_chunk_resize(): it ensures that some stack space remains after calling new_chunk() and increasing the stack size. With the old code, after the stack was enlarged, one could immediately get a stack overflow.
>From d2c10cbe313e4104052e84c8172f964c33abc943 Mon Sep 17 00:00:00 2001
From: Jeroen Demeyer <jdemeyer@cage.ugent.be>
Date: Mon, 3 Apr 2017 14:06:22 +0200
Subject: [PATCH] When enlarging stack in new_chunk_resize(), add 1/8 to stack
 size

---
 src/language/init.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/src/language/init.c b/src/language/init.c
index 2a30482..6b5f013 100644
--- a/src/language/init.c
+++ b/src/language/init.c
@@ -809,7 +809,7 @@ parivstack_reset(void)
 }
 
 /* Enlarge the stack if needed such that the unused portion of the stack
- * (between bot and avma) is large enough to contain x longs. */
+ * (between bot and avma) is large enough to contain at least x longs. */
 void
 new_chunk_resize(size_t x)
 {
@@ -817,11 +817,12 @@ new_chunk_resize(size_t x)
   avail = (avma - pari_mainstack->bot) / sizeof(long);
   if (avail >= x) return;
 
-  /* We need to enlarge the stack. We try to at least double the
-   * stack, to avoid increasing the stack a lot of times by a small
-   * amount. */
+  /* We need to enlarge the stack. We compute the minimum size needed
+   * and add 1/8 to avoid increasing the stack a lot of times by a
+   * small amount. */
   size = pari_mainstack->size;
-  newsize = size + maxuu((x - avail) * sizeof(long), size);
+  newsize = size + (x - avail) * sizeof(long);
+  newsize += newsize / 8;
   paristack_resize(newsize);
 
   /* Verify that we have enough space. Using a division here instead
-- 
2.7.3