emacs-diffs
[Top][All Lists]
Advanced

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

[Emacs-diffs] /srv/bzr/emacs/trunk r111066: Fix xpalloc confusion after


From: Paul Eggert
Subject: [Emacs-diffs] /srv/bzr/emacs/trunk r111066: Fix xpalloc confusion after memory is exhausted.
Date: Sun, 02 Dec 2012 15:11:42 -0800
User-agent: Bazaar (2.5.0)

------------------------------------------------------------
revno: 111066
committer: Paul Eggert <address@hidden>
branch nick: trunk
timestamp: Sun 2012-12-02 15:11:42 -0800
message:
  Fix xpalloc confusion after memory is exhausted.
  
  * alloc.c (xpalloc): Comment fix.
  * charset.c (Fdefine_charset_internal): If xpalloc exhausts memory
  and signals an error, do not clear charset_table_size, as
  charset_table is still valid.
  * doprnt.c (evxprintf): Clear *BUF after freeing it.
modified:
  src/ChangeLog
  src/alloc.c
  src/charset.c
  src/doprnt.c
=== modified file 'src/ChangeLog'
--- a/src/ChangeLog     2012-12-02 19:16:45 +0000
+++ b/src/ChangeLog     2012-12-02 23:11:42 +0000
@@ -1,5 +1,12 @@
 2012-12-02  Paul Eggert  <address@hidden>
 
+       Fix xpalloc confusion after memory is exhausted.
+       * alloc.c (xpalloc): Comment fix.
+       * charset.c (Fdefine_charset_internal): If xpalloc exhausts memory
+       and signals an error, do not clear charset_table_size, as
+       charset_table is still valid.
+       * doprnt.c (evxprintf): Clear *BUF after freeing it.
+
        Use execve to avoid need to munge environ (Bug#13054).
        * callproc.c (Fcall_process):
        * process.c (create_process):

=== modified file 'src/alloc.c'
--- a/src/alloc.c       2012-11-22 09:32:32 +0000
+++ b/src/alloc.c       2012-12-02 23:11:42 +0000
@@ -761,13 +761,17 @@
    infinity.
 
    If PA is null, then allocate a new array instead of reallocating
-   the old one.  Thus, to grow an array A without saving its old
-   contents, invoke xfree (A) immediately followed by xgrowalloc (0,
-   &NITEMS, ...).
+   the old one.
 
    Block interrupt input as needed.  If memory exhaustion occurs, set
    *NITEMS to zero if PA is null, and signal an error (i.e., do not
-   return).  */
+   return).
+
+   Thus, to grow an array A without saving its old contents, do
+   { xfree (A); A = NULL; A = xpalloc (NULL, &AITEMS, ...); }.
+   The A = NULL avoids a dangling pointer if xpalloc exhausts memory
+   and signals an error, and later this code is reexecuted and
+   attempts to free A.  */
 
 void *
 xpalloc (void *pa, ptrdiff_t *nitems, ptrdiff_t nitems_incr_min,

=== modified file 'src/charset.c'
--- a/src/charset.c     2012-11-14 04:55:41 +0000
+++ b/src/charset.c     2012-12-02 23:11:42 +0000
@@ -1142,12 +1142,14 @@
             example, the IDs are stuffed into struct
             coding_system.charbuf[i] entries, which are 'int'.  */
          int old_size = charset_table_size;
+         ptrdiff_t new_size = old_size;
          struct charset *new_table =
-           xpalloc (0, &charset_table_size, 1,
+           xpalloc (0, &new_size, 1,
                     min (INT_MAX, MOST_POSITIVE_FIXNUM),
                     sizeof *charset_table);
          memcpy (new_table, charset_table, old_size * sizeof *new_table);
          charset_table = new_table;
+         charset_table_size = new_size;
          /* FIXME: This leaks memory, as the old charset_table becomes
             unreachable.  If the old charset table is charset_table_init
             then this leak is intentional; otherwise, it's unclear.

=== modified file 'src/doprnt.c'
--- a/src/doprnt.c      2012-09-15 07:06:56 +0000
+++ b/src/doprnt.c      2012-12-02 23:11:42 +0000
@@ -521,7 +521,10 @@
       if (nbytes < *bufsize - 1)
        return nbytes;
       if (*buf != nonheapbuf)
-       xfree (*buf);
+       {
+         xfree (*buf);
+         *buf = NULL;
+       }
       *buf = xpalloc (NULL, bufsize, 1, bufsize_max, 1);
     }
 }


reply via email to

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