2011-04-07 Paul Eggert <eggert@cs.ucla.edu>
+ error: Print 32- and 64-bit integers portably (Bug#8435).
+ Without this change, on typical 64-bit hosts error ("...%d...", N)
+ was used to print both 32- and 64-bit integers N, which relied on
+ undefined behavior.
+ * lisp.h, src/m/amdx86-64.h, src/m/ia64.h, src/m/ibms390x.h (pEd):
+ New macro.
+ * lisp.h (error, verror): Mark as printf-like functions.
+ * eval.c (verror): Use vsnprintf, not doprnt, to do the real work.
+ Report overflow in size calculations when allocating printf buffer.
+ Do not truncate output string at its first null byte.
+ * xdisp.c (vmessage): Use vsnprintf, not doprnt, to do the real work.
+ Truncate the output at a character boundary, since vsnprintf does not
+ do that.
+ * charset.c (check_iso_charset_parameter): Convert internal
+ character to string before calling 'error', since %c now has the
+ printf meaning.
+ * coding.c (Fdecode_sjis_char, Fdecode_big5_char): Avoid int
+ overflow when computing char to be passed to 'error'. Do not
+ pass Lisp_Object to 'error'; pass the integer instead.
+ * nsfns.m (Fns_do_applescript): Use int, not long, since it's
+ formatted with plain %d.
+
* eval.c (internal_lisp_condition_case): Don't pass spurious arg.
* keyboard.c (access_keymap_keyremap): Print func name, not garbage.
table = check_category_table (table);
if (!NILP (CATEGORY_DOCSTRING (table, XFASTINT (category))))
- error ("Category `%c' is already defined", XFASTINT (category));
+ error ("Category `%c' is already defined", (int) XFASTINT (category));
if (!NILP (Vpurify_flag))
docstring = Fpurecopy (docstring);
CATEGORY_DOCSTRING (table, XFASTINT (category)) = docstring;
table = check_category_table (table);
if (NILP (CATEGORY_DOCSTRING (table, XFASTINT (category))))
- error ("Undefined category: %c", XFASTINT (category));
+ error ("Undefined category: %c", (int) XFASTINT (category));
set_value = NILP (reset) ? Qt : Qnil;
unbind_to (count, Qnil);
if (fd < 0
|| ! (fp = fdopen (fd, "r")))
- error ("Failure in loading charset map: %S", SDATA (mapfile));
+ error ("Failure in loading charset map: %s", SDATA (mapfile));
/* Use SAFE_ALLOCA instead of alloca, as `charset_map_entries' is
large (larger than MAX_ALLOCA). */
{
CHECK_NUMBER (val);
if (XINT (val) < '0' || XINT (val) > 127)
- error ("Invalid iso-final-char: %d", XINT (val));
+ error ("Invalid iso-final-char: %"pEd, XINT (val));
charset.iso_final = XINT (val);
}
{
CHECK_NATNUM (val);
if ((XINT (val) > 0 && XINT (val) <= 128) || XINT (val) >= 256)
- error ("Invalid emacs-mule-id: %d", XINT (val));
+ error ("Invalid emacs-mule-id: %"pEd, XINT (val));
charset.emacs_mule_id = XINT (val);
}
CHECK_NATNUM (final_char);
if (XINT (dimension) > 3)
- error ("Invalid DIMENSION %d, it should be 1, 2, or 3", XINT (dimension));
+ error ("Invalid DIMENSION %"pEd", it should be 1, 2, or 3",
+ XINT (dimension));
if (XINT (chars) != 94 && XINT (chars) != 96)
- error ("Invalid CHARS %d, it should be 94 or 96", XINT (chars));
+ error ("Invalid CHARS %"pEd", it should be 94 or 96", XINT (chars));
if (XINT (final_char) < '0' || XINT (final_char) > '~')
- error ("Invalid FINAL-CHAR %c, it should be `0'..`~'", XINT (chars));
+ {
+ unsigned char str[MAX_MULTIBYTE_LENGTH + 1];
+ int len = CHAR_STRING (XINT (chars), str);
+ str[len] = '\0';
+ error ("Invalid FINAL-CHAR %s, it should be `0'..`~'", str);
+ }
}
{
Lisp_Object spec, attrs, val;
struct charset *charset_roman, *charset_kanji, *charset_kana, *charset;
- EMACS_INT c;
+ EMACS_INT ch;
+ int c;
CHECK_NATNUM (code);
- c = XFASTINT (code);
+ ch = XFASTINT (code);
CHECK_CODING_SYSTEM_GET_SPEC (Vsjis_coding_system, spec);
attrs = AREF (spec, 0);
- if (ASCII_BYTE_P (c)
+ if (ASCII_BYTE_P (ch)
&& ! NILP (CODING_ATTR_ASCII_COMPAT (attrs)))
return code;
charset_kana = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val);
charset_kanji = CHARSET_FROM_ID (XINT (XCAR (val)));
- if (c <= 0x7F)
- charset = charset_roman;
- else if (c >= 0xA0 && c < 0xDF)
+ if (ch <= 0x7F)
+ {
+ c = ch;
+ charset = charset_roman;
+ }
+ else if (ch >= 0xA0 && ch < 0xDF)
{
+ c = ch - 0x80;
charset = charset_kana;
- c -= 0x80;
}
else
{
- EMACS_INT c1 = c >> 8;
- int c2 = c & 0xFF;
+ EMACS_INT c1 = ch >> 8;
+ int c2 = ch & 0xFF;
if (c1 < 0x81 || (c1 > 0x9F && c1 < 0xE0) || c1 > 0xEF
|| c2 < 0x40 || c2 == 0x7F || c2 > 0xFC)
- error ("Invalid code: %d", code);
+ error ("Invalid code: %"pEd, ch);
+ c = ch;
SJIS_TO_JIS (c);
charset = charset_kanji;
}
c = DECODE_CHAR (charset, c);
if (c < 0)
- error ("Invalid code: %d", code);
+ error ("Invalid code: %"pEd, ch);
return make_number (c);
}
{
Lisp_Object spec, attrs, val;
struct charset *charset_roman, *charset_big5, *charset;
+ EMACS_INT ch;
int c;
CHECK_NATNUM (code);
- c = XFASTINT (code);
+ ch = XFASTINT (code);
CHECK_CODING_SYSTEM_GET_SPEC (Vbig5_coding_system, spec);
attrs = AREF (spec, 0);
- if (ASCII_BYTE_P (c)
+ if (ASCII_BYTE_P (ch)
&& ! NILP (CODING_ATTR_ASCII_COMPAT (attrs)))
return code;
charset_roman = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val);
charset_big5 = CHARSET_FROM_ID (XINT (XCAR (val)));
- if (c <= 0x7F)
- charset = charset_roman;
+ if (ch <= 0x7F)
+ {
+ c = ch;
+ charset = charset_roman;
+ }
else
{
- int b1 = c >> 8, b2 = c & 0x7F;
+ EMACS_INT b1 = ch >> 8;
+ int b2 = ch & 0x7F;
if (b1 < 0xA1 || b1 > 0xFE
|| b2 < 0x40 || (b2 > 0x7E && b2 < 0xA1) || b2 > 0xFE)
- error ("Invalid code: %d", code);
+ error ("Invalid code: %"pEd, ch);
+ c = ch;
charset = charset_big5;
}
- c = DECODE_CHAR (charset, (unsigned )c);
+ c = DECODE_CHAR (charset, c);
if (c < 0)
- error ("Invalid code: %d", code);
+ error ("Invalid code: %"pEd, ch);
return make_number (c);
}
|| (EQ (operation, Qinsert_file_contents) && CONSP (target)
&& STRINGP (XCAR (target)) && BUFFERP (XCDR (target)))
|| (EQ (operation, Qopen_network_stream) && INTEGERP (target))))
- error ("Invalid %dth argument", XFASTINT (target_idx) + 1);
+ error ("Invalid %"pEd"th argument", XFASTINT (target_idx) + 1);
if (CONSP (target))
target = XCAR (target);
CHECK_CHARSET_GET_ID (tmp1, id);
CHECK_NATNUM_CDR (val);
if (XINT (XCDR (val)) >= 4)
- error ("Invalid graphic register number: %d", XINT (XCDR (val)));
+ error ("Invalid graphic register number: %"pEd, XINT (XCDR (val)));
XSETCAR (val, make_number (id));
}
if (0 > lseek (fd, position - offset, 0))
{
emacs_close (fd);
- error ("Position %ld out of range in doc string file \"%s\"",
+ error ("Position %"pEd" out of range in doc string file \"%s\"",
position, name);
}
; /* Just a source file name boundary marker. Ignore it. */
else
- error ("DOC file invalid at position %d", pos);
+ error ("DOC file invalid at position %"pEd, pos);
}
}
pos += end - buf;
verror (const char *m, va_list ap)
{
char buf[200];
- EMACS_INT size = 200;
- int mlen;
+ size_t size = sizeof buf;
+ size_t size_max = (size_t) -1;
char *buffer = buf;
int allocated = 0;
+ int used;
Lisp_Object string;
- mlen = strlen (m);
-
while (1)
{
- EMACS_INT used;
- used = doprnt (buffer, size, m, m + mlen, ap);
+ used = vsnprintf (buffer, size, m, ap);
+ if (used < 0)
+ used = 0;
if (used < size)
break;
- size *= 2;
+ if (size <= size_max / 2)
+ size *= 2;
+ else if (size < size_max)
+ size = size_max;
+ else
+ memory_full ();
if (allocated)
buffer = (char *) xrealloc (buffer, size);
else
}
}
- string = build_string (buffer);
+ string = make_string (buffer, used);
if (allocated)
xfree (buffer);
EMACS_INT converted = str_to_unibyte (SDATA (string), str, chars, 0);
if (converted < chars)
- error ("Can't convert the %dth character to unibyte", converted);
+ error ("Can't convert the %"pEd"th character to unibyte", converted);
string = make_unibyte_string ((char *) str, chars);
xfree (str);
}
i = i->right; /* Move to the right child */
}
else if (NULL_PARENT (i))
- error ("Point %d after end of properties", pos);
+ error ("Point %"pEd" after end of properties", pos);
else
i = INTERVAL_PARENT (i);
continue;
#ifndef EMACS_INT
#define EMACS_INT long
#define BITS_PER_EMACS_INT BITS_PER_LONG
+#define pEd "ld"
#endif
#ifndef EMACS_UINT
#define EMACS_UINT unsigned long
#ifndef EMACS_INT
#define EMACS_INT int
#define BITS_PER_EMACS_INT BITS_PER_INT
+#define pEd "d"
#endif
#ifndef EMACS_UINT
#define EMACS_UINT unsigned int
extern void specbind (Lisp_Object, Lisp_Object);
extern void record_unwind_protect (Lisp_Object (*) (Lisp_Object), Lisp_Object);
extern Lisp_Object unbind_to (int, Lisp_Object);
-extern void error (const char *, ...) NO_RETURN;
-extern void verror (const char *, va_list) NO_RETURN;
+extern void error (const char *, ...) NO_RETURN ATTRIBUTE_FORMAT_PRINTF (1, 2);
+extern void verror (const char *, va_list)
+ NO_RETURN ATTRIBUTE_FORMAT_PRINTF (1, 0);
extern void do_autoload (Lisp_Object, Lisp_Object);
extern Lisp_Object un_autoload (Lisp_Object);
EXFUN (Ffetch_bytecode, 1);
/* Define the type to use. */
#define EMACS_INT long
+#define pEd "ld"
#define EMACS_UINT unsigned long
/* Define XPNTR to avoid or'ing with DATA_SEG_BITS */
/* Define the type to use. */
#define EMACS_INT long
+#define pEd "ld"
#define EMACS_UINT unsigned long
#ifdef REL_ALLOC
/* Define the type to use. */
#define EMACS_INT long
+#define pEd "ld"
#define EMACS_UINT unsigned long
/* On the 64 bit architecture, we can use 60 bits for addresses */
/* Define XPNTR to avoid or'ing with DATA_SEG_BITS */
#define XPNTR(a) XUINT (a)
-
if (!STRINGP (f->icon_name))
encoded_icon_name = encoded_name;
else
- encoded_icon_name = ENCODE_UTF_8 (f->icon_name);
+ encoded_icon_name = ENCODE_UTF_8 (f->icon_name);
str = [NSString stringWithUTF8String: SDATA (encoded_icon_name)];
if (FRAME_ICONIFIED_P (f))
[[view window] setMiniwindowTitle: str];
- else
+ else
{
NSString *fstr;
0, /* x_set_fullscreen will ignore */
x_set_font_backend, /* generic OK */
x_set_alpha,
- 0, /* x_set_sticky */
- 0, /* x_set_tool_bar_position */
+ 0, /* x_set_sticky */
+ 0, /* x_set_tool_bar_position */
};
(Lisp_Object script)
{
Lisp_Object result;
- long status;
+ int status;
CHECK_STRING (script);
check_ns ();
{
struct ns_display_info *dpyinfo;
check_ns ();
-
+
dpyinfo = check_ns_display_info (display);
/* We force 24+ bit depths to 24-bit to prevent an overflow. */
return make_number (1 << min (dpyinfo->n_planes, 24));
pt.y = x_display_pixel_height (FRAME_NS_DISPLAY_INFO (f)) - XINT (top)
- height;
}
-
+
/* Ensure in bounds. (Note, screen origin = lower left.) */
if (INTEGERP (left))
*root_x = pt.x;
check_window_system_func = check_ns;
}
-
CHECK_NUMBER (tem);
err = cfsetspeed (&attr, XINT (tem));
if (err != 0)
- error ("cfsetspeed(%d) failed: %s", XINT (tem), emacs_strerror (errno));
+ error ("cfsetspeed(%"pEd") failed: %s", XINT (tem),
+ emacs_strerror (errno));
childp2 = Fplist_put (childp2, QCspeed, tem);
/* Configure bytesize. */
error ("Window height %d too small (after splitting)", size_int);
if (size_int + window_safe_height > XFASTINT (o->total_lines))
error ("Window height %d too small (after splitting)",
- XFASTINT (o->total_lines) - size_int);
+ (int) (XFASTINT (o->total_lines) - size_int));
if (NILP (o->parent)
|| NILP (XWINDOW (o->parent)->vchild))
{
error ("Window width %d too small (after splitting)", size_int);
if (size_int + window_safe_width > XFASTINT (o->total_cols))
error ("Window width %d too small (after splitting)",
- XFASTINT (o->total_cols) - size_int);
+ (int) (XFASTINT (o->total_cols) - size_int));
if (NILP (o->parent)
|| NILP (XWINDOW (o->parent)->hchild))
{
{
if (m)
{
- EMACS_INT len;
-
- len = doprnt (FRAME_MESSAGE_BUF (f),
- FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, ap);
+ char *buf = FRAME_MESSAGE_BUF (f);
+ size_t bufsize = FRAME_MESSAGE_BUF_SIZE (f);
+ int len = vsnprintf (buf, bufsize, m, ap);
+ if (len < 0)
+ len = 0;
+
+ /* Do any truncation at a character boundary. */
+ if (0 < bufsize && bufsize <= len)
+ for (len = bufsize - 1;
+ len && ! CHAR_HEAD_P (buf[len - 1]);
+ len--)
+ continue;
message2 (FRAME_MESSAGE_BUF (f), len, 0);
}
struct terminal *t = get_terminal (object, 1);
if (t->type != output_x_window)
- error ("Terminal %d is not an X display", XINT (object));
+ error ("Terminal %"pEd" is not an X display", XINT (object));
dpyinfo = t->display_info.x;
}