guile-commits
[Top][All Lists]
Advanced

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

[Guile-commits] 01/02: Work around unwanted stack retention when using p


From: Ludovic Courtès
Subject: [Guile-commits] 01/02: Work around unwanted stack retention when using prompts.
Date: Sun, 20 Nov 2022 12:27:11 -0500 (EST)

civodul pushed a commit to branch main
in repository guile.

commit e47a153317c046ea5d335940412999e7dc604c33
Author: Ludovic Courtès <ludo@gnu.org>
AuthorDate: Sun Nov 20 18:07:24 2022 +0100

    Work around unwanted stack retention when using prompts.
    
    Fixes <https://bugs.gnu.org/59021>.
    
    Previously, the stack allocated in 'capture_stack' and stored in
    'p->stack_bottom' could be retained, leading to heap growth.
    
    * libguile/vm.c (capture_stack): Make a single 'scm_gc_malloc' call
    instead of two.
---
 libguile/vm.c | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)

diff --git a/libguile/vm.c b/libguile/vm.c
index 6fd5c554f..b565db970 100644
--- a/libguile/vm.c
+++ b/libguile/vm.c
@@ -1,4 +1,4 @@
-/* Copyright 2001,2009-2015,2017-2020
+/* Copyright 2001,2009-2015,2017-2020,2022
      Free Software Foundation, Inc.
 
    This file is part of Guile.
@@ -165,11 +165,18 @@ capture_stack (union scm_vm_stack_element *stack_top,
                scm_t_dynstack *dynstack, uint32_t flags)
 {
   struct scm_vm_cont *p;
+  size_t stack_size;
+
+  stack_size = stack_top - sp;
+
+  /* Allocate the 'scm_vm_cont' struct and the stack at once.  That way,
+     keeping a pointer to 'p->stack_bottom' around won't retain it.
+     See <https://bugs.gnu.org/59021>.  */
+  p = scm_gc_malloc (sizeof (*p) + stack_size * sizeof (*p->stack_bottom),
+                     "capture_vm_cont");
 
-  p = scm_gc_malloc (sizeof (*p), "capture_vm_cont");
-  p->stack_size = stack_top - sp;
-  p->stack_bottom = scm_gc_malloc (p->stack_size * sizeof (*p->stack_bottom),
-                                   "capture_vm_cont");
+  p->stack_size = stack_size;
+  p->stack_bottom = (void *) ((char *) p + sizeof (*p));
   p->vra = vra;
   p->mra = mra;
   p->fp_offset = stack_top - fp;



reply via email to

[Prev in Thread] Current Thread [Next in Thread]