diff options
| author | Paul Eggert | 2011-04-06 20:34:05 -0700 |
|---|---|---|
| committer | Paul Eggert | 2011-04-06 20:34:05 -0700 |
| commit | 5fdb398c4b75b0c834aff7132f90b0ce5317a25a (patch) | |
| tree | 29ebb8fc5700fefdd867fa497eac27fb7d0bcfe0 /src/coding.c | |
| parent | b189fa667ed7ac7b17f9665cd8a0c26316b3c521 (diff) | |
| download | emacs-5fdb398c4b75b0c834aff7132f90b0ce5317a25a.tar.gz emacs-5fdb398c4b75b0c834aff7132f90b0ce5317a25a.zip | |
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.
Diffstat (limited to 'src/coding.c')
| -rw-r--r-- | src/coding.c | 53 |
1 files changed, 32 insertions, 21 deletions
diff --git a/src/coding.c b/src/coding.c index 798e5c533f6..f099605c774 100644 --- a/src/coding.c +++ b/src/coding.c | |||
| @@ -9023,14 +9023,15 @@ Return the corresponding character. */) | |||
| 9023 | { | 9023 | { |
| 9024 | Lisp_Object spec, attrs, val; | 9024 | Lisp_Object spec, attrs, val; |
| 9025 | struct charset *charset_roman, *charset_kanji, *charset_kana, *charset; | 9025 | struct charset *charset_roman, *charset_kanji, *charset_kana, *charset; |
| 9026 | EMACS_INT c; | 9026 | EMACS_INT ch; |
| 9027 | int c; | ||
| 9027 | 9028 | ||
| 9028 | CHECK_NATNUM (code); | 9029 | CHECK_NATNUM (code); |
| 9029 | c = XFASTINT (code); | 9030 | ch = XFASTINT (code); |
| 9030 | CHECK_CODING_SYSTEM_GET_SPEC (Vsjis_coding_system, spec); | 9031 | CHECK_CODING_SYSTEM_GET_SPEC (Vsjis_coding_system, spec); |
| 9031 | attrs = AREF (spec, 0); | 9032 | attrs = AREF (spec, 0); |
| 9032 | 9033 | ||
| 9033 | if (ASCII_BYTE_P (c) | 9034 | if (ASCII_BYTE_P (ch) |
| 9034 | && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs))) | 9035 | && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs))) |
| 9035 | return code; | 9036 | return code; |
| 9036 | 9037 | ||
| @@ -9039,27 +9040,31 @@ Return the corresponding character. */) | |||
| 9039 | charset_kana = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val); | 9040 | charset_kana = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val); |
| 9040 | charset_kanji = CHARSET_FROM_ID (XINT (XCAR (val))); | 9041 | charset_kanji = CHARSET_FROM_ID (XINT (XCAR (val))); |
| 9041 | 9042 | ||
| 9042 | if (c <= 0x7F) | 9043 | if (ch <= 0x7F) |
| 9043 | charset = charset_roman; | 9044 | { |
| 9044 | else if (c >= 0xA0 && c < 0xDF) | 9045 | c = ch; |
| 9046 | charset = charset_roman; | ||
| 9047 | } | ||
| 9048 | else if (ch >= 0xA0 && ch < 0xDF) | ||
| 9045 | { | 9049 | { |
| 9050 | c = ch - 0x80; | ||
| 9046 | charset = charset_kana; | 9051 | charset = charset_kana; |
| 9047 | c -= 0x80; | ||
| 9048 | } | 9052 | } |
| 9049 | else | 9053 | else |
| 9050 | { | 9054 | { |
| 9051 | EMACS_INT c1 = c >> 8; | 9055 | EMACS_INT c1 = ch >> 8; |
| 9052 | int c2 = c & 0xFF; | 9056 | int c2 = ch & 0xFF; |
| 9053 | 9057 | ||
| 9054 | if (c1 < 0x81 || (c1 > 0x9F && c1 < 0xE0) || c1 > 0xEF | 9058 | if (c1 < 0x81 || (c1 > 0x9F && c1 < 0xE0) || c1 > 0xEF |
| 9055 | || c2 < 0x40 || c2 == 0x7F || c2 > 0xFC) | 9059 | || c2 < 0x40 || c2 == 0x7F || c2 > 0xFC) |
| 9056 | error ("Invalid code: %d", code); | 9060 | error ("Invalid code: %"pEd, ch); |
| 9061 | c = ch; | ||
| 9057 | SJIS_TO_JIS (c); | 9062 | SJIS_TO_JIS (c); |
| 9058 | charset = charset_kanji; | 9063 | charset = charset_kanji; |
| 9059 | } | 9064 | } |
| 9060 | c = DECODE_CHAR (charset, c); | 9065 | c = DECODE_CHAR (charset, c); |
| 9061 | if (c < 0) | 9066 | if (c < 0) |
| 9062 | error ("Invalid code: %d", code); | 9067 | error ("Invalid code: %"pEd, ch); |
| 9063 | return make_number (c); | 9068 | return make_number (c); |
| 9064 | } | 9069 | } |
| 9065 | 9070 | ||
| @@ -9099,14 +9104,15 @@ Return the corresponding character. */) | |||
| 9099 | { | 9104 | { |
| 9100 | Lisp_Object spec, attrs, val; | 9105 | Lisp_Object spec, attrs, val; |
| 9101 | struct charset *charset_roman, *charset_big5, *charset; | 9106 | struct charset *charset_roman, *charset_big5, *charset; |
| 9107 | EMACS_INT ch; | ||
| 9102 | int c; | 9108 | int c; |
| 9103 | 9109 | ||
| 9104 | CHECK_NATNUM (code); | 9110 | CHECK_NATNUM (code); |
| 9105 | c = XFASTINT (code); | 9111 | ch = XFASTINT (code); |
| 9106 | CHECK_CODING_SYSTEM_GET_SPEC (Vbig5_coding_system, spec); | 9112 | CHECK_CODING_SYSTEM_GET_SPEC (Vbig5_coding_system, spec); |
| 9107 | attrs = AREF (spec, 0); | 9113 | attrs = AREF (spec, 0); |
| 9108 | 9114 | ||
| 9109 | if (ASCII_BYTE_P (c) | 9115 | if (ASCII_BYTE_P (ch) |
| 9110 | && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs))) | 9116 | && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs))) |
| 9111 | return code; | 9117 | return code; |
| 9112 | 9118 | ||
| @@ -9114,19 +9120,24 @@ Return the corresponding character. */) | |||
| 9114 | charset_roman = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val); | 9120 | charset_roman = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val); |
| 9115 | charset_big5 = CHARSET_FROM_ID (XINT (XCAR (val))); | 9121 | charset_big5 = CHARSET_FROM_ID (XINT (XCAR (val))); |
| 9116 | 9122 | ||
| 9117 | if (c <= 0x7F) | 9123 | if (ch <= 0x7F) |
| 9118 | charset = charset_roman; | 9124 | { |
| 9125 | c = ch; | ||
| 9126 | charset = charset_roman; | ||
| 9127 | } | ||
| 9119 | else | 9128 | else |
| 9120 | { | 9129 | { |
| 9121 | int b1 = c >> 8, b2 = c & 0x7F; | 9130 | EMACS_INT b1 = ch >> 8; |
| 9131 | int b2 = ch & 0x7F; | ||
| 9122 | if (b1 < 0xA1 || b1 > 0xFE | 9132 | if (b1 < 0xA1 || b1 > 0xFE |
| 9123 | || b2 < 0x40 || (b2 > 0x7E && b2 < 0xA1) || b2 > 0xFE) | 9133 | || b2 < 0x40 || (b2 > 0x7E && b2 < 0xA1) || b2 > 0xFE) |
| 9124 | error ("Invalid code: %d", code); | 9134 | error ("Invalid code: %"pEd, ch); |
| 9135 | c = ch; | ||
| 9125 | charset = charset_big5; | 9136 | charset = charset_big5; |
| 9126 | } | 9137 | } |
| 9127 | c = DECODE_CHAR (charset, (unsigned )c); | 9138 | c = DECODE_CHAR (charset, c); |
| 9128 | if (c < 0) | 9139 | if (c < 0) |
| 9129 | error ("Invalid code: %d", code); | 9140 | error ("Invalid code: %"pEd, ch); |
| 9130 | return make_number (c); | 9141 | return make_number (c); |
| 9131 | } | 9142 | } |
| 9132 | 9143 | ||
| @@ -9298,7 +9309,7 @@ usage: (find-operation-coding-system OPERATION ARGUMENTS...) */) | |||
| 9298 | || (EQ (operation, Qinsert_file_contents) && CONSP (target) | 9309 | || (EQ (operation, Qinsert_file_contents) && CONSP (target) |
| 9299 | && STRINGP (XCAR (target)) && BUFFERP (XCDR (target))) | 9310 | && STRINGP (XCAR (target)) && BUFFERP (XCDR (target))) |
| 9300 | || (EQ (operation, Qopen_network_stream) && INTEGERP (target)))) | 9311 | || (EQ (operation, Qopen_network_stream) && INTEGERP (target)))) |
| 9301 | error ("Invalid %dth argument", XFASTINT (target_idx) + 1); | 9312 | error ("Invalid %"pEd"th argument", XFASTINT (target_idx) + 1); |
| 9302 | if (CONSP (target)) | 9313 | if (CONSP (target)) |
| 9303 | target = XCAR (target); | 9314 | target = XCAR (target); |
| 9304 | 9315 | ||
| @@ -9774,7 +9785,7 @@ usage: (define-coding-system-internal ...) */) | |||
| 9774 | CHECK_CHARSET_GET_ID (tmp1, id); | 9785 | CHECK_CHARSET_GET_ID (tmp1, id); |
| 9775 | CHECK_NATNUM_CDR (val); | 9786 | CHECK_NATNUM_CDR (val); |
| 9776 | if (XINT (XCDR (val)) >= 4) | 9787 | if (XINT (XCDR (val)) >= 4) |
| 9777 | error ("Invalid graphic register number: %d", XINT (XCDR (val))); | 9788 | error ("Invalid graphic register number: %"pEd, XINT (XCDR (val))); |
| 9778 | XSETCAR (val, make_number (id)); | 9789 | XSETCAR (val, make_number (id)); |
| 9779 | } | 9790 | } |
| 9780 | 9791 | ||