aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorPaul Eggert2011-09-01 07:44:49 -0700
committerPaul Eggert2011-09-01 07:44:49 -0700
commit8666506ecd6b1a90c7c66fb4b6051e90fba20c30 (patch)
tree5f853c76e609cf303965e7cd7eb361e011e98641 /src
parent726cfaae869af4f678e26ace56d7dc42676bc299 (diff)
downloademacs-8666506ecd6b1a90c7c66fb4b6051e90fba20c30.tar.gz
emacs-8666506ecd6b1a90c7c66fb4b6051e90fba20c30.zip
* src/doprnt.c (esnprintf): Remove. All uses removed.
Suggested by Chong Yidong in <http://debbugs.gnu.org/cgi/bugreport.cgi?bug=9412#23>.
Diffstat (limited to 'src')
-rw-r--r--src/ChangeLog11
-rw-r--r--src/dbusbind.c42
-rw-r--r--src/dispnew.c20
-rw-r--r--src/doprnt.c21
-rw-r--r--src/font.c94
-rw-r--r--src/lisp.h2
-rw-r--r--src/xterm.c8
7 files changed, 115 insertions, 83 deletions
diff --git a/src/ChangeLog b/src/ChangeLog
index 0ba2df42186..94ba3a040ca 100644
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,9 +1,9 @@
12011-08-31 Paul Eggert <eggert@cs.ucla.edu> 12011-09-01 Paul Eggert <eggert@cs.ucla.edu>
2 2
3 sprintf-related integer and memory overflow issues (Bug#9412). 3 sprintf-related integer and memory overflow issues (Bug#9412).
4 4
5 * doprnt.c (doprnt): Support printing ptrdiff_t and intmax_t values. 5 * doprnt.c (doprnt): Support printing ptrdiff_t and intmax_t values.
6 (esprintf, esnprintf, exprintf, evxprintf): New functions. 6 (esprintf, exprintf, evxprintf): New functions.
7 * keyboard.c (command_loop_level): Now EMACS_INT, not int. 7 * keyboard.c (command_loop_level): Now EMACS_INT, not int.
8 (cmd_error): kbd macro iterations count is now EMACS_INT, not int. 8 (cmd_error): kbd macro iterations count is now EMACS_INT, not int.
9 (modify_event_symbol): Do not assume that the length of 9 (modify_event_symbol): Do not assume that the length of
@@ -17,7 +17,7 @@
17 * minibuf.c (minibuf_level): Now EMACS_INT, not int. 17 * minibuf.c (minibuf_level): Now EMACS_INT, not int.
18 (get_minibuffer): Arg is now EMACS_INT, not int. 18 (get_minibuffer): Arg is now EMACS_INT, not int.
19 * lisp.h (get_minibuffer, push_key_description): Reflect API changes. 19 * lisp.h (get_minibuffer, push_key_description): Reflect API changes.
20 (esprintf, esnprintf, exprintf, evxprintf): New decls. 20 (esprintf, exprintf, evxprintf): New decls.
21 * window.h (command_loop_level, minibuf_level): Reflect API changes. 21 * window.h (command_loop_level, minibuf_level): Reflect API changes.
22 22
23 * dbusbind.c (signature_cat): New function. 23 * dbusbind.c (signature_cat): New function.
@@ -43,7 +43,7 @@
43 * font.c: Include <float.h>, for DBL_MAX_10_EXP. 43 * font.c: Include <float.h>, for DBL_MAX_10_EXP.
44 (font_unparse_xlfd): Don't blindly alloca long strings. 44 (font_unparse_xlfd): Don't blindly alloca long strings.
45 Don't assume XINT result fits in int, or that XFLOAT_DATA * 10 45 Don't assume XINT result fits in int, or that XFLOAT_DATA * 10
46 fits in int, when using sprintf. Use single esnprintf to count 46 fits in int, when using sprintf. Use single snprintf to count
47 length of string rather than counting it via multiple sprintfs; 47 length of string rather than counting it via multiple sprintfs;
48 that's simpler and more reliable. 48 that's simpler and more reliable.
49 (font_unparse_fcname): Use it to avoid sprintf buffer overrun. 49 (font_unparse_fcname): Use it to avoid sprintf buffer overrun.
@@ -102,9 +102,6 @@
102 to avoid potential buffer overrun. 102 to avoid potential buffer overrun.
103 103
104 * xterm.c (x_io_error_quitter): Don't overrun sprintf buffer. 104 * xterm.c (x_io_error_quitter): Don't overrun sprintf buffer.
105 (x_term_init): Use sprintf, not snprintf, so that we need not
106 worry about ancient hosts that lack snprintf. The buffer cannot
107 possibly be overrun, so this is safe.
108 105
109 * xterm.h (x_check_errors): Add ATTRIBUTE_FORMAT_PRINTF. 106 * xterm.h (x_check_errors): Add ATTRIBUTE_FORMAT_PRINTF.
110 107
diff --git a/src/dbusbind.c b/src/dbusbind.c
index fd9a43aaf86..8dac2a6249f 100644
--- a/src/dbusbind.c
+++ b/src/dbusbind.c
@@ -284,6 +284,7 @@ xd_signature (char *signature, unsigned int dtype, unsigned int parent_type, Lis
284 unsigned int subtype; 284 unsigned int subtype;
285 Lisp_Object elt; 285 Lisp_Object elt;
286 char const *subsig; 286 char const *subsig;
287 int subsiglen;
287 char x[DBUS_MAXIMUM_SIGNATURE_LENGTH]; 288 char x[DBUS_MAXIMUM_SIGNATURE_LENGTH];
288 289
289 elt = object; 290 elt = object;
@@ -365,9 +366,9 @@ xd_signature (char *signature, unsigned int dtype, unsigned int parent_type, Lis
365 elt = CDR_SAFE (XD_NEXT_VALUE (elt)); 366 elt = CDR_SAFE (XD_NEXT_VALUE (elt));
366 } 367 }
367 368
368 if (esnprintf (signature, DBUS_MAXIMUM_SIGNATURE_LENGTH, 369 subsiglen = snprintf (signature, DBUS_MAXIMUM_SIGNATURE_LENGTH,
369 "%c%s", dtype, subsig) 370 "%c%s", dtype, subsig);
370 == DBUS_MAXIMUM_SIGNATURE_LENGTH - 1) 371 if (! (0 <= subsiglen && subsiglen < DBUS_MAXIMUM_SIGNATURE_LENGTH))
371 string_overflow (); 372 string_overflow ();
372 break; 373 break;
373 374
@@ -2088,32 +2089,45 @@ usage: (dbus-register-signal BUS SERVICE PATH INTERFACE SIGNAL HANDLER &rest ARG
2088 connection = xd_initialize (bus, TRUE); 2089 connection = xd_initialize (bus, TRUE);
2089 2090
2090 /* Create a rule to receive related signals. */ 2091 /* Create a rule to receive related signals. */
2091 rulelen = esnprintf (rule, sizeof rule, 2092 rulelen = snprintf (rule, sizeof rule,
2092 "type='signal',interface='%s',member='%s'", 2093 "type='signal',interface='%s',member='%s'",
2093 SDATA (interface), 2094 SDATA (interface),
2094 SDATA (signal)); 2095 SDATA (signal));
2096 if (! (0 <= rulelen && rulelen < sizeof rule))
2097 string_overflow ();
2095 2098
2096 /* Add unique name and path to the rule if they are non-nil. */ 2099 /* Add unique name and path to the rule if they are non-nil. */
2097 if (!NILP (uname)) 2100 if (!NILP (uname))
2098 rulelen += esnprintf (rule + rulelen, sizeof rule - rulelen, 2101 {
2102 int len = snprintf (rule + rulelen, sizeof rule - rulelen,
2099 ",sender='%s'", SDATA (uname)); 2103 ",sender='%s'", SDATA (uname));
2104 if (! (0 <= len && len < sizeof rule - rulelen))
2105 string_overflow ();
2106 rulelen += len;
2107 }
2100 2108
2101 if (!NILP (path)) 2109 if (!NILP (path))
2102 rulelen += esnprintf (rule + rulelen, sizeof rule - rulelen, 2110 {
2111 int len = snprintf (rule + rulelen, sizeof rule - rulelen,
2103 ",path='%s'", SDATA (path)); 2112 ",path='%s'", SDATA (path));
2113 if (! (0 <= len && len < sizeof rule - rulelen))
2114 string_overflow ();
2115 rulelen += len;
2116 }
2104 2117
2105 /* Add arguments to the rule if they are non-nil. */ 2118 /* Add arguments to the rule if they are non-nil. */
2106 for (i = 6; i < nargs; ++i) 2119 for (i = 6; i < nargs; ++i)
2107 if (!NILP (args[i])) 2120 if (!NILP (args[i]))
2108 { 2121 {
2122 int len;
2109 CHECK_STRING (args[i]); 2123 CHECK_STRING (args[i]);
2110 rulelen += esnprintf (rule + rulelen, sizeof rule - rulelen, 2124 len = snprintf (rule + rulelen, sizeof rule - rulelen,
2111 ",arg%"pD"d='%s'", i - 6, SDATA (args[i])); 2125 ",arg%"pD"d='%s'", i - 6, SDATA (args[i]));
2126 if (! (0 <= len && len < sizeof rule - rulelen))
2127 string_overflow ();
2128 rulelen += len;
2112 } 2129 }
2113 2130
2114 if (rulelen == sizeof rule - 1)
2115 string_overflow ();
2116
2117 /* Add the rule to the bus. */ 2131 /* Add the rule to the bus. */
2118 dbus_error_init (&derror); 2132 dbus_error_init (&derror);
2119 dbus_bus_add_match (connection, rule, &derror); 2133 dbus_bus_add_match (connection, rule, &derror);
diff --git a/src/dispnew.c b/src/dispnew.c
index 0cc888b4b7a..5c28d014819 100644
--- a/src/dispnew.c
+++ b/src/dispnew.c
@@ -272,16 +272,16 @@ add_window_display_history (struct window *w, const char *msg, int paused_p)
272 buf = redisplay_history[history_idx].trace; 272 buf = redisplay_history[history_idx].trace;
273 ++history_idx; 273 ++history_idx;
274 274
275 esnprintf (buf, sizeof redisplay_history[0].trace, 275 snprintf (buf, sizeof redisplay_history[0].trace,
276 "%"pMu": window %p (`%s')%s\n%s", 276 "%"pMu": window %p (`%s')%s\n%s",
277 history_tick++, 277 history_tick++,
278 w, 278 w,
279 ((BUFFERP (w->buffer) 279 ((BUFFERP (w->buffer)
280 && STRINGP (BVAR (XBUFFER (w->buffer), name))) 280 && STRINGP (BVAR (XBUFFER (w->buffer), name)))
281 ? SSDATA (BVAR (XBUFFER (w->buffer), name)) 281 ? SSDATA (BVAR (XBUFFER (w->buffer), name))
282 : "???"), 282 : "???"),
283 paused_p ? " ***paused***" : "", 283 paused_p ? " ***paused***" : "",
284 msg); 284 msg);
285} 285}
286 286
287 287
diff --git a/src/doprnt.c b/src/doprnt.c
index dae1dab04d7..638fa4d6312 100644
--- a/src/doprnt.c
+++ b/src/doprnt.c
@@ -486,27 +486,6 @@ esprintf (char *buf, char const *format, ...)
486 return nbytes; 486 return nbytes;
487} 487}
488 488
489/* Format to a buffer BUF of positive size BUFSIZE. This is like
490 snprintf, except it is not limited to returning an 'int' so it
491 doesn't have a silly 2 GiB limit on typical 64-bit hosts. However,
492 it is limited to the Emacs-style formats that doprnt supports, and
493 BUFSIZE must be positive.
494
495 Return the number of bytes put into BUF, excluding the terminating
496 '\0'. Unlike snprintf, always return a nonnegative value less than
497 BUFSIZE; if the output is truncated, return BUFSIZE - 1, which is
498 the length of the truncated output. */
499ptrdiff_t
500esnprintf (char *buf, ptrdiff_t bufsize, char const *format, ...)
501{
502 ptrdiff_t nbytes;
503 va_list ap;
504 va_start (ap, format);
505 nbytes = doprnt (buf, bufsize, format, 0, ap);
506 va_end (ap);
507 return nbytes;
508}
509
510/* Format to buffer *BUF of positive size *BUFSIZE, reallocating *BUF 489/* Format to buffer *BUF of positive size *BUFSIZE, reallocating *BUF
511 and updating *BUFSIZE if the buffer is too small, and otherwise 490 and updating *BUFSIZE if the buffer is too small, and otherwise
512 behaving line esprintf. When reallocating, free *BUF unless it is 491 behaving line esprintf. When reallocating, free *BUF unless it is
diff --git a/src/font.c b/src/font.c
index a5b873aea51..34cacb37ce4 100644
--- a/src/font.c
+++ b/src/font.c
@@ -1285,14 +1285,14 @@ font_unparse_xlfd (Lisp_Object font, int pixel_size, char *name, int nbytes)
1285 } 1285 }
1286 else 1286 else
1287 f[XLFD_AVGWIDTH_INDEX] = "*"; 1287 f[XLFD_AVGWIDTH_INDEX] = "*";
1288 len = esnprintf (name, nbytes, "-%s-%s-%s-%s-%s-%s-%s-%s-%s-%s-%s", 1288 len = snprintf (name, nbytes, "-%s-%s-%s-%s-%s-%s-%s-%s-%s-%s-%s",
1289 f[XLFD_FOUNDRY_INDEX], f[XLFD_FAMILY_INDEX], 1289 f[XLFD_FOUNDRY_INDEX], f[XLFD_FAMILY_INDEX],
1290 f[XLFD_WEIGHT_INDEX], f[XLFD_SLANT_INDEX], 1290 f[XLFD_WEIGHT_INDEX], f[XLFD_SLANT_INDEX],
1291 f[XLFD_SWIDTH_INDEX], f[XLFD_ADSTYLE_INDEX], 1291 f[XLFD_SWIDTH_INDEX], f[XLFD_ADSTYLE_INDEX],
1292 f[XLFD_PIXEL_INDEX], f[XLFD_RESX_INDEX], 1292 f[XLFD_PIXEL_INDEX], f[XLFD_RESX_INDEX],
1293 f[XLFD_SPACING_INDEX], f[XLFD_AVGWIDTH_INDEX], 1293 f[XLFD_SPACING_INDEX], f[XLFD_AVGWIDTH_INDEX],
1294 f[XLFD_REGISTRY_INDEX]); 1294 f[XLFD_REGISTRY_INDEX]);
1295 return len == nbytes - 1 ? -1 : len; 1295 return len < nbytes ? len : -1;
1296} 1296}
1297 1297
1298/* Parse NAME (null terminated) and store information in FONT 1298/* Parse NAME (null terminated) and store information in FONT
@@ -1593,31 +1593,75 @@ font_unparse_fcname (Lisp_Object font, int pixel_size, char *name, int nbytes)
1593 p = name; 1593 p = name;
1594 lim = name + nbytes; 1594 lim = name + nbytes;
1595 if (! NILP (family)) 1595 if (! NILP (family))
1596 p += esnprintf (p, lim - p, "%s", SSDATA (family)); 1596 {
1597 int len = snprintf (p, lim - p, "%s", SSDATA (family));
1598 if (! (0 <= len && len < lim - p))
1599 return -1;
1600 p += len;
1601 }
1597 if (point_size > 0) 1602 if (point_size > 0)
1598 p += esnprintf (p, lim - p, "-%d" + (p == name), point_size); 1603 {
1604 int len = snprintf (p, lim - p, "-%d" + (p == name), point_size);
1605 if (! (0 <= len && len < lim - p))
1606 return -1;
1607 p += len;
1608 }
1599 else if (pixel_size > 0) 1609 else if (pixel_size > 0)
1600 p += esnprintf (p, lim - p, ":pixelsize=%d", pixel_size); 1610 {
1611 int len = snprintf (p, lim - p, ":pixelsize=%d", pixel_size);
1612 if (! (0 <= len && len < lim - p))
1613 return -1;
1614 p += len;
1615 }
1601 if (! NILP (AREF (font, FONT_FOUNDRY_INDEX))) 1616 if (! NILP (AREF (font, FONT_FOUNDRY_INDEX)))
1602 p += esnprintf (p, lim - p, ":foundry=%s", 1617 {
1603 SSDATA (SYMBOL_NAME (AREF (font, 1618 int len = snprintf (p, lim - p, ":foundry=%s",
1604 FONT_FOUNDRY_INDEX)))); 1619 SSDATA (SYMBOL_NAME (AREF (font,
1620 FONT_FOUNDRY_INDEX))));
1621 if (! (0 <= len && len < lim - p))
1622 return -1;
1623 p += len;
1624 }
1605 for (i = 0; i < 3; i++) 1625 for (i = 0; i < 3; i++)
1606 if (! NILP (styles[i])) 1626 if (! NILP (styles[i]))
1607 p += esnprintf (p, lim - p, ":%s=%s", style_names[i], 1627 {
1608 SSDATA (SYMBOL_NAME (styles[i]))); 1628 int len = snprintf (p, lim - p, ":%s=%s", style_names[i],
1629 SSDATA (SYMBOL_NAME (styles[i])));
1630 if (! (0 <= len && len < lim - p))
1631 return -1;
1632 p += len;
1633 }
1634
1609 if (INTEGERP (AREF (font, FONT_DPI_INDEX))) 1635 if (INTEGERP (AREF (font, FONT_DPI_INDEX)))
1610 p += esnprintf (p, lim - p, ":dpi=%"pI"d", 1636 {
1611 XINT (AREF (font, FONT_DPI_INDEX))); 1637 int len = snprintf (p, lim - p, ":dpi=%"pI"d",
1638 XINT (AREF (font, FONT_DPI_INDEX)));
1639 if (! (0 <= len && len < lim - p))
1640 return -1;
1641 p += len;
1642 }
1643
1612 if (INTEGERP (AREF (font, FONT_SPACING_INDEX))) 1644 if (INTEGERP (AREF (font, FONT_SPACING_INDEX)))
1613 p += esnprintf (p, lim - p, ":spacing=%"pI"d", 1645 {
1614 XINT (AREF (font, FONT_SPACING_INDEX))); 1646 int len = snprintf (p, lim - p, ":spacing=%"pI"d",
1647 XINT (AREF (font, FONT_SPACING_INDEX)));
1648 if (! (0 <= len && len < lim - p))
1649 return -1;
1650 p += len;
1651 }
1652
1615 if (INTEGERP (AREF (font, FONT_AVGWIDTH_INDEX))) 1653 if (INTEGERP (AREF (font, FONT_AVGWIDTH_INDEX)))
1616 p += esnprintf (p, lim - p, 1654 {
1617 (XINT (AREF (font, FONT_AVGWIDTH_INDEX)) == 0 1655 int len = snprintf (p, lim - p,
1618 ? ":scalable=true" 1656 (XINT (AREF (font, FONT_AVGWIDTH_INDEX)) == 0
1619 : ":scalable=false")); 1657 ? ":scalable=true"
1620 return lim - p == 1 ? -1 : p - name; 1658 : ":scalable=false"));
1659 if (! (0 <= len && len < lim - p))
1660 return -1;
1661 p += len;
1662 }
1663
1664 return (p - name);
1621} 1665}
1622 1666
1623/* Parse NAME (null terminated) and store information in FONT 1667/* Parse NAME (null terminated) and store information in FONT
diff --git a/src/lisp.h b/src/lisp.h
index 2f6ec38f228..5c84bb8e06e 100644
--- a/src/lisp.h
+++ b/src/lisp.h
@@ -2897,8 +2897,6 @@ extern ptrdiff_t doprnt (char *, ptrdiff_t, const char *, const char *,
2897 va_list); 2897 va_list);
2898extern ptrdiff_t esprintf (char *, char const *, ...) 2898extern ptrdiff_t esprintf (char *, char const *, ...)
2899 ATTRIBUTE_FORMAT_PRINTF (2, 3); 2899 ATTRIBUTE_FORMAT_PRINTF (2, 3);
2900extern ptrdiff_t esnprintf (char *, ptrdiff_t, char const *, ...)
2901 ATTRIBUTE_FORMAT_PRINTF (3, 4);
2902extern ptrdiff_t exprintf (char **, ptrdiff_t *, char const *, ptrdiff_t, 2900extern ptrdiff_t exprintf (char **, ptrdiff_t *, char const *, ptrdiff_t,
2903 char const *, ...) 2901 char const *, ...)
2904 ATTRIBUTE_FORMAT_PRINTF (5, 6); 2902 ATTRIBUTE_FORMAT_PRINTF (5, 6);
diff --git a/src/xterm.c b/src/xterm.c
index 72e9f2b2236..86393cf411f 100644
--- a/src/xterm.c
+++ b/src/xterm.c
@@ -7900,8 +7900,8 @@ x_io_error_quitter (Display *display)
7900{ 7900{
7901 char buf[256]; 7901 char buf[256];
7902 7902
7903 esnprintf (buf, sizeof buf, "Connection lost to X server `%s'", 7903 snprintf (buf, sizeof buf, "Connection lost to X server `%s'",
7904 DisplayString (display)); 7904 DisplayString (display));
7905 x_connection_closed (display, buf); 7905 x_connection_closed (display, buf);
7906 return 0; 7906 return 0;
7907} 7907}
@@ -10278,8 +10278,8 @@ x_term_init (Lisp_Object display_name, char *xrm_option, char *resource_name)
10278 atom_names[i] = (char *) atom_refs[i].name; 10278 atom_names[i] = (char *) atom_refs[i].name;
10279 10279
10280 /* Build _XSETTINGS_SN atom name */ 10280 /* Build _XSETTINGS_SN atom name */
10281 sprintf (xsettings_atom_name, 10281 snprintf (xsettings_atom_name, sizeof (xsettings_atom_name),
10282 "_XSETTINGS_S%d", XScreenNumberOfScreen (dpyinfo->screen)); 10282 "_XSETTINGS_S%d", XScreenNumberOfScreen (dpyinfo->screen));
10283 atom_names[i] = xsettings_atom_name; 10283 atom_names[i] = xsettings_atom_name;
10284 10284
10285 XInternAtoms (dpyinfo->display, atom_names, total_atom_count, 10285 XInternAtoms (dpyinfo->display, atom_names, total_atom_count,