xalloc: delay setting size until success
authorPaul Eggert <eggert@cs.ucla.edu>
Thu, 1 Apr 2021 07:59:54 +0000 (00:59 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Thu, 1 Apr 2021 08:04:45 +0000 (01:04 -0700)
* lib/xalloc.h (x2nrealloc): Don’t change *PN until after xrealloc
succeeds, in case xalloc_die or one of its callees or longjmp
targets uses *PN.  Similar code in xpalloc already does this.

ChangeLog
lib/xalloc.h

index 78bf3a5a749ef4af21d0c1623988e5f66b841d88..784409dba9c9d3747fa0cc7e17f58d6fdbb1eddf 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2021-03-31  Paul Eggert  <eggert@cs.ucla.edu>
+
+       xalloc: delay setting size until success
+       * lib/xalloc.h (x2nrealloc): Don’t change *PN until after xrealloc
+       succeeds, in case xalloc_die or one of its callees or longjmp
+       targets uses *PN.  Similar code in xpalloc already does this.
+
 2021-03-28  Paul Eggert  <eggert@cs.ucla.edu>
 
        xalloc: new function xpalloc, from dfa
index 76d83c63c11c2a83530985b721bafec6a5f6437c..5633fdf3c43e26a327c64db45c9bf6f7f169d7e0 100644 (file)
@@ -206,8 +206,9 @@ x2nrealloc (void *p, size_t *pn, size_t s)
       n += n / 2 + 1;
     }
 
+  p = xrealloc (p, n * s);
   *pn = n;
-  return xrealloc (p, n * s);
+  return p;
 }
 
 /* Return a pointer to a new buffer of N bytes.  This is like xmalloc,