Subject: Re: bug#9412: sprintf-related integer and memory overflow issues

From: Paul Eggert <[email-masked]>
Date: Wed, 31 Aug 2011 15:21:37 -0700

On 08/31/11 09:14, Chong Yidong wrote:

> I'm not clear on this issue of sprintf etc being restricted to 2GB.
> Could you explain further?

sprintf returns an 'int' counting the number of bytes it outputs.
If this count would exceed INT_MAX, sprintf returns -1 and sets errno.
sprintf therefore cannot be used to create a string that might be
longer than INT_MAX bytes.  On typical 64-bit hosts, this is an
arbitrary 2 GiB limit, which Emacs shouldn't have.

> Surely such a limitation is a bug in the C library, not Emacs?
> If so, it should be fixed there, not in Emacs.

I agree, but unfortunately the problem is inherent to sprintf's type
signature, which has been stable for many years and is not slated to
be improved or extended even in C1X.  It would take decades before we
could assume any such fix would be present in the C library.

> sprintf (and snprintf) is a well-known function; when someone comes
> across it in the code, it's not necessary to look it up to know what
> it's doing.

True, but surely it's not too much to ask Emacs developers to remember
that esprintf is like sprintf, except without the 2 GiB limit.  Emacs
developers already deal with similar printf-like functions ('message',
'error', etc.) and this sort of thing is nothing new.

I wouldn't be suggesting esprintf if it weren't for the 2 GiB limit.

> I think we should add a stub for snprintf in sysdep.c for the
> !HAVE_SNPRINTF case (which will need configure to set up HAVE_SNPRINTF).

Sure, that's easily done, with the following additional patch.
I'll CC: this to Eli since it adds a #define to nt/config.nt.

=== modified file 'ChangeLog'
--- ChangeLog	2011-08-30 20:46:59 +0000
+++ ChangeLog	2011-08-31 22:18:16 +0000
@@ -1,3 +1,7 @@
+2011-08-31  Paul Eggert  <[email-masked]>
+
+	* configure.in (snprintf): New check.
+
 2011-08-30  Paul Eggert  <[email-masked]>
 
 	* configure.in (opsys): Change pattern to *-*-linux*

=== modified file 'configure.in'
--- configure.in	2011-08-30 20:46:59 +0000
+++ configure.in	2011-08-31 22:18:16 +0000
@@ -3071,6 +3071,8 @@
 
 AC_FUNC_FORK
 
+AC_CHECK_FUNCS(snprintf)
+
 dnl Adapted from Haible's version.
 AC_CACHE_CHECK([for nl_langinfo and CODESET], emacs_cv_langinfo_codeset,
   [AC_LINK_IFELSE([AC_LANG_PROGRAM([[#include <langinfo.h>]],

=== modified file 'nt/ChangeLog'
--- nt/ChangeLog	2011-07-28 00:48:01 +0000
+++ nt/ChangeLog	2011-08-31 22:18:16 +0000
@@ -1,3 +1,7 @@
+2011-08-31  Paul Eggert  <[email-masked]>
+
+	* config.nt (HAVE_SNPRINTF): New macro.
+
 2011-07-28  Paul Eggert  <[email-masked]>
 
 	Assume freestanding C89 headers, string.h, stdlib.h.

=== modified file 'nt/config.nt'
--- nt/config.nt	2011-07-07 01:32:56 +0000
+++ nt/config.nt	2011-08-31 22:18:16 +0000
@@ -240,6 +240,7 @@
 #define HAVE_SETSOCKOPT 1
 #define HAVE_GETSOCKNAME 1
 #define HAVE_GETPEERNAME 1
+#define HAVE_SNPRINTF 1
 #define HAVE_LANGINFO_CODESET 1
 /* Local (unix) sockets are not supported.  */
 #undef HAVE_SYS_UN_H

=== modified file 'src/ChangeLog'
--- src/ChangeLog	2011-08-31 20:02:51 +0000
+++ src/ChangeLog	2011-08-31 22:18:16 +0000
@@ -90,6 +90,8 @@
 	* process.c (make_process): Use printmax_t, not int, to format
 	process-name gensyms.
 
+	* sysdep.c (snprintf) [! HAVE_SNPRINTF]: New function.
+
 	* term.c (produce_glyphless_glyph): Make sprintf buffer a bit bigger
 	to avoid potential buffer overrun.

=== modified file 'src/sysdep.c'
--- src/sysdep.c	2011-07-29 01:16:54 +0000
+++ src/sysdep.c	2011-08-31 22:18:16 +0000
@@ -1811,6 +1811,45 @@
 }
 #endif /* not WINDOWSNT */
 #endif /* ! HAVE_STRERROR */
+
+#ifndef HAVE_SNPRINTF
+/* Approximate snprintf as best we can on ancient hosts that lack it.  */
+int
+snprintf (char *buf, size_t bufsize, char const *format, ...)
+{
+  ptrdiff_t size = min (bufsize, PTRDIFF_MAX);
+  ptrdiff_t nbytes = size - 1;
+  va_list ap;
+
+  if (size)
+    {
+      va_start (ap, format);
+      nbytes = doprnt (buf, size, format, 0, ap);
+      va_end (ap);
+    }
+
+  if (nbytes == size - 1)
+    {
+      /* Calculate the length of the string that would have been created
+	 had the buffer been large enough.  */
+      char stackbuf[4000];
+      char *b = stackbuf;
+      ptrdiff_t bsize = sizeof stackbuf;
+      va_start (ap, format);
+      nbytes = evxprintf (&b, &bsize, stackbuf, -1, format, ap);
+      va_end (ap);
+      if (b != stackbuf)
+	xfree (b);
+    }
+
+  if (INT_MAX < nbytes)
+    {
+      errno = EOVERFLOW;
+      return -1;
+    }
+  return nbytes;
+}
+#endif
 
 int
 emacs_open (const char *path, int oflag, int mode)