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: Tue, 30 Aug 2011 23:03:07 -0700 [thread overview]
Message-ID: <4E5DCE9B.9060903@cs.ucla.edu> (raw)
In-Reply-To: <87vcteib8y.fsf@stupidchicken.com>
On 08/30/11 19:08, Chong Yidong wrote:
> I don't much like the idea of using custom functions like esprintf and
> esnprintf. They make the code much less clear.
In code that is formatting buffers or strings that might be longer
than 2 GiB, we cannot use sprintf etc. We need to fix these issues
somehow. If there's a better way than esprintf etc. please let me know.
Anyway, I don't quite follow why using esprintf etc. makes the calling
code much less clear. esprintf is like sprintf, except that on 64-bit
hosts esprintf doesn't have sprintf's 2 GiB limit. If the use of
esprintf is unclear, then surely the use of sprintf is just as unclear.
> I seem to recall that the reason we don't use snprintf is that
> it's not available on all the platforms that Emacs supports.
Although that used to be true, I expect that platforms lacking
snprintf (e.g., Solaris 2.5.1, IRIX 5.3, OSF/1 4.0) are no longer of
practical importance as Emacs porting targets.
That being said, it's easy to allay this concern, by using esnprintf
instead of snprintf in all areas of the code that might run on
ancient platforms. The following further patch does this.
====
Avoid the use of snprintf.
* font.c (APPEND_SNPRINTF): Remove.
(font_unparse_xlfd):
* xterm.c (x_io_error_quitter):
Use esnprintf, not snprintf. That way, we don't have to worry
about porting to ancient platforms that lack snprintf.
(x_term_init): Use sprintf, not snprintf.
=== modified file 'src/font.c'
--- src/font.c 2011-08-29 20:57:42 +0000
+++ src/font.c 2011-08-31 05:50:10 +0000
@@ -1285,14 +1285,14 @@
}
else
f[XLFD_AVGWIDTH_INDEX] = "*";
- len = snprintf (name, nbytes, "-%s-%s-%s-%s-%s-%s-%s-%s-%s-%s-%s",
- f[XLFD_FOUNDRY_INDEX], f[XLFD_FAMILY_INDEX],
- f[XLFD_WEIGHT_INDEX], f[XLFD_SLANT_INDEX],
- f[XLFD_SWIDTH_INDEX], f[XLFD_ADSTYLE_INDEX],
- f[XLFD_PIXEL_INDEX], f[XLFD_RESX_INDEX],
- f[XLFD_SPACING_INDEX], f[XLFD_AVGWIDTH_INDEX],
- f[XLFD_REGISTRY_INDEX]);
- return len < nbytes ? len : -1;
+ len = esnprintf (name, nbytes, "-%s-%s-%s-%s-%s-%s-%s-%s-%s-%s-%s",
+ f[XLFD_FOUNDRY_INDEX], f[XLFD_FAMILY_INDEX],
+ f[XLFD_WEIGHT_INDEX], f[XLFD_SLANT_INDEX],
+ f[XLFD_SWIDTH_INDEX], f[XLFD_ADSTYLE_INDEX],
+ f[XLFD_PIXEL_INDEX], f[XLFD_RESX_INDEX],
+ f[XLFD_SPACING_INDEX], f[XLFD_AVGWIDTH_INDEX],
+ f[XLFD_REGISTRY_INDEX]);
+ return len == nbytes - 1 ? -1 : len;
}
/* Parse NAME (null terminated) and store information in FONT
@@ -1592,39 +1592,32 @@
p = name;
lim = name + nbytes;
-# define APPEND_SNPRINTF(args) \
- do { \
- int len = snprintf args; \
- if (! (0 <= len && len < lim - p)) \
- return -1; \
- p += len; \
- } while (0)
if (! NILP (family))
- APPEND_SNPRINTF ((p, lim - p, "%s", SSDATA (family)));
+ p += esnprintf (p, lim - p, "%s", SSDATA (family));
if (point_size > 0)
- APPEND_SNPRINTF ((p, lim - p, "-%d" + (p == name), point_size));
+ p += esnprintf (p, lim - p, "-%d" + (p == name), point_size);
else if (pixel_size > 0)
- APPEND_SNPRINTF ((p, lim - p, ":pixelsize=%d", pixel_size));
+ p += esnprintf (p, lim - p, ":pixelsize=%d", pixel_size);
if (! NILP (AREF (font, FONT_FOUNDRY_INDEX)))
- APPEND_SNPRINTF ((p, lim - p, ":foundry=%s",
- SSDATA (SYMBOL_NAME (AREF (font,
- FONT_FOUNDRY_INDEX)))));
+ p += esnprintf (p, lim - p, ":foundry=%s",
+ SSDATA (SYMBOL_NAME (AREF (font,
+ FONT_FOUNDRY_INDEX))));
for (i = 0; i < 3; i++)
if (! NILP (styles[i]))
- APPEND_SNPRINTF ((p, lim - p, ":%s=%s", style_names[i],
- SSDATA (SYMBOL_NAME (styles[i]))));
+ p += esnprintf (p, lim - p, ":%s=%s", style_names[i],
+ SSDATA (SYMBOL_NAME (styles[i])));
if (INTEGERP (AREF (font, FONT_DPI_INDEX)))
- APPEND_SNPRINTF ((p, lim - p, ":dpi=%"pI"d",
- XINT (AREF (font, FONT_DPI_INDEX))));
+ p += esnprintf (p, lim - p, ":dpi=%"pI"d",
+ XINT (AREF (font, FONT_DPI_INDEX)));
if (INTEGERP (AREF (font, FONT_SPACING_INDEX)))
- APPEND_SNPRINTF ((p, lim - p, ":spacing=%"pI"d",
- XINT (AREF (font, FONT_SPACING_INDEX))));
+ p += esnprintf (p, lim - p, ":spacing=%"pI"d",
+ XINT (AREF (font, FONT_SPACING_INDEX)));
if (INTEGERP (AREF (font, FONT_AVGWIDTH_INDEX)))
- APPEND_SNPRINTF ((p, lim - p,
- (XINT (AREF (font, FONT_AVGWIDTH_INDEX)) == 0
- ? ":scalable=true"
- : ":scalable=false")));
- return (p - name);
+ p += esnprintf (p, lim - p,
+ (XINT (AREF (font, FONT_AVGWIDTH_INDEX)) == 0
+ ? ":scalable=true"
+ : ":scalable=false"));
+ return lim - p == 1 ? -1 : p - name;
}
/* Parse NAME (null terminated) and store information in FONT
=== modified file 'src/xterm.c'
--- src/xterm.c 2011-08-29 20:03:30 +0000
+++ src/xterm.c 2011-08-31 05:24:36 +0000
@@ -7900,8 +7900,8 @@
{
char buf[256];
- snprintf (buf, sizeof buf, "Connection lost to X server `%s'",
- DisplayString (display));
+ esnprintf (buf, sizeof buf, "Connection lost to X server `%s'",
+ DisplayString (display));
x_connection_closed (display, buf);
return 0;
}
@@ -10278,8 +10278,8 @@
atom_names[i] = (char *) atom_refs[i].name;
/* Build _XSETTINGS_SN atom name */
- snprintf (xsettings_atom_name, sizeof (xsettings_atom_name),
- "_XSETTINGS_S%d", XScreenNumberOfScreen (dpyinfo->screen));
+ sprintf (xsettings_atom_name,
+ "_XSETTINGS_S%d", XScreenNumberOfScreen (dpyinfo->screen));
atom_names[i] = xsettings_atom_name;
XInternAtoms (dpyinfo->display, atom_names, total_atom_count,
next prev parent reply other threads:[~2011-08-31 6:03 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 [this message]
2011-08-31 16:14 ` Chong Yidong
2011-08-31 22:21 ` Paul Eggert
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
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=4E5DCE9B.9060903@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 external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.