don't use function-equal in nadvice
[bpt/emacs.git] / src / doprnt.c
index 707dd06..3d50724 100644 (file)
@@ -1,7 +1,7 @@
 /* Output like sprintf to a buffer of specified size.
    Also takes args differently: pass one pointer to the end
    of the format string in addition to the format string itself.
-   Copyright (C) 1985, 2001-201 Free Software Foundation, Inc.
+   Copyright (C) 1985, 2001-2014 Free Software Foundation, Inc.
 
 This file is part of GNU Emacs.
 
@@ -38,7 +38,7 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
      could include embedded null characters.
 
    . It signals an error if the length of the formatted string is about to
-     overflow MOST_POSITIVE_FIXNUM, to avoid producing strings longer than what
+     overflow ptrdiff_t or size_t, to avoid producing strings longer than what
      Emacs can handle.
 
    OTOH, this function supports only a small subset of the standard C formatted
@@ -102,8 +102,6 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 
 #include <config.h>
 #include <stdio.h>
-#include <ctype.h>
-#include <setjmp.h>
 #include <float.h>
 #include <unistd.h>
 #include <limits.h>
@@ -115,10 +113,6 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
    another macro.  */
 #include "character.h"
 
-#ifndef DBL_MAX_10_EXP
-#define DBL_MAX_10_EXP 308 /* IEEE double */
-#endif
-
 /* Generate output from a format-spec FORMAT,
    terminated at position FORMAT_END.
    (*FORMAT_END is not part of the format, but must exist and be readable.)
@@ -135,8 +129,8 @@ ptrdiff_t
 doprnt (char *buffer, ptrdiff_t bufsize, const char *format,
        const char *format_end, va_list ap)
 {
-  const char *fmt = format;    /* Pointer into format string */
-  register char *bufptr = buffer; /* Pointer into output buffer.. */
+  const char *fmt = format;    /* Pointer into format string */
+  char *bufptr = buffer;       /* Pointer into output buffer.  */
 
   /* Use this for sprintf unless we need something really big.  */
   char tembuf[DBL_MAX_10_EXP + 100];
@@ -150,7 +144,7 @@ doprnt (char *buffer, ptrdiff_t bufsize, const char *format,
   /* Buffer we have got with malloc.  */
   char *big_buffer = NULL;
 
-  register int tem = -1;
+  ptrdiff_t tem = -1;
   char *string;
   char fixed_buffer[20];       /* Default buffer for small formatting. */
   char *fmtcpy;
@@ -161,10 +155,9 @@ doprnt (char *buffer, ptrdiff_t bufsize, const char *format,
   if (format_end == 0)
     format_end = format + strlen (format);
 
-  if (format_end - format < sizeof (fixed_buffer) - 1)
-    fmtcpy = fixed_buffer;
-  else
-    SAFE_ALLOCA (fmtcpy, char *, format_end - format + 1);
+  fmtcpy = (format_end - format < sizeof (fixed_buffer) - 1
+           ? fixed_buffer
+           : SAFE_ALLOCA (format_end - format + 1));
 
   bufsize--;
 
@@ -257,7 +250,7 @@ doprnt (char *buffer, ptrdiff_t bufsize, const char *format,
            {
              if (big_buffer)
                xfree (big_buffer);
-             big_buffer = (char *) xmalloc (size_bound);
+             big_buffer = xmalloc_atomic (size_bound);
              sprintf_buffer = big_buffer;
              size_allocated = size_bound;
            }
@@ -368,7 +361,7 @@ doprnt (char *buffer, ptrdiff_t bufsize, const char *format,
 
              /* Copy string into final output, truncating if no room.  */
            doit:
-             eassert (tem != -1);
+             eassert (0 <= tem);
              /* Coming here means STRING contains ASCII only.  */
              if (STRING_BYTES_BOUND < tem)
                error ("Format width or precision too large");
@@ -528,7 +521,10 @@ evxprintf (char **buf, ptrdiff_t *bufsize,
       if (nbytes < *bufsize - 1)
        return nbytes;
       if (*buf != nonheapbuf)
-       xfree (*buf);
+       {
+         xfree (*buf);
+         *buf = NULL;
+       }
       *buf = xpalloc (NULL, bufsize, 1, bufsize_max, 1);
     }
 }