unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Paul Eggert <eggert@cs.ucla.edu>
To: Chong Yidong <cyd@stupidchicken.com>
Cc: 9412@debbugs.gnu.org
Subject: bug#9412: sprintf-related integer and memory overflow issues
Date: Wed, 31 Aug 2011 15:21:37 -0700	[thread overview]
Message-ID: <4E5EB3F1.5030906@cs.ucla.edu> (raw)
In-Reply-To: <871uw136ei.fsf@stupidchicken.com>

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  <eggert@cs.ucla.edu>
+
+	* configure.in (snprintf): New check.
+
 2011-08-30  Paul Eggert  <eggert@cs.ucla.edu>
 
 	* 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  <eggert@cs.ucla.edu>
+
+	* config.nt (HAVE_SNPRINTF): New macro.
+
 2011-07-28  Paul Eggert  <eggert@cs.ucla.edu>
 
 	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
 \f
 int
 emacs_open (const char *path, int oflag, int mode)







  reply	other threads:[~2011-08-31 22:21 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-08-30 22:42 bug#9412: sprintf-related integer and memory overflow issues Paul Eggert
2011-08-31  2:08 ` Chong Yidong
2011-08-31  6:03   ` Paul Eggert
2011-08-31 16:14     ` Chong Yidong
2011-08-31 22:21       ` Paul Eggert [this message]
2011-09-01  2:42         ` Chong Yidong
2011-09-01  5:44           ` Paul Eggert
2011-08-31 16:53     ` Dan Nicolaescu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4E5EB3F1.5030906@cs.ucla.edu \
    --to=eggert@cs.ucla.edu \
    --cc=9412@debbugs.gnu.org \
    --cc=cyd@stupidchicken.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).