Fix crashes with lots of overlays.
[bpt/emacs.git] / src / ccl.c
index ffd412b..9bfd437 100644 (file)
--- a/src/ccl.c
+++ b/src/ccl.c
@@ -1,5 +1,5 @@
 /* CCL (Code Conversion Language) interpreter.
-   Copyright (C) 2001-2011 Free Software Foundation, Inc.
+   Copyright (C) 2001-2013 Free Software Foundation, Inc.
    Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
      2005, 2006, 2007, 2008, 2009, 2010, 2011
      National Institute of Advanced Industrial Science and Technology (AIST)
@@ -26,7 +26,6 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 #include <config.h>
 
 #include <stdio.h>
-#include <setjmp.h>
 #include <limits.h>
 
 #include "lisp.h"
@@ -47,7 +46,7 @@ static Lisp_Object Qcode_conversion_map;
 static Lisp_Object Qcode_conversion_map_id;
 
 /* Symbols of ccl program have this property, a value of the property
-   is an index for Vccl_protram_table. */
+   is an index for Vccl_program_table. */
 static Lisp_Object Qccl_program_idx;
 
 /* Table of registered CCL programs.  Each element is a vector of
@@ -61,7 +60,7 @@ static Lisp_Object Vccl_program_table;
 
 /* Return a hash table of id number ID.  */
 #define GET_HASH_TABLE(id) \
-  (XHASH_TABLE (XCDR (XVECTOR (Vtranslation_hash_table_vector)->contents[(id)])))
+  (XHASH_TABLE (XCDR (AREF (Vtranslation_hash_table_vector, (id)))))
 
 /* CCL (Code Conversion Language) is a simple language which has
    operations on one input buffer, one output buffer, and 7 registers.
@@ -210,8 +209,8 @@ static Lisp_Object Vccl_program_table;
 #define CCL_WriteArrayReadJump 0x0B /* Write an array element, read, and jump:
                                        1:A--D--D--R--E--S--S-rrrXXXXX
                                        2:LENGTH
-                                       3:ELEMENET[0]
-                                       4:ELEMENET[1]
+                                       3:ELEMENT[0]
+                                       4:ELEMENT[1]
                                        ...
                                        N:A--D--D--R--E--S--S-rrrYYYYY
                                        ------------------------------
@@ -429,7 +428,7 @@ static Lisp_Object Vccl_program_table;
 
 #define CCL_Extension          0x1F /* Extended CCL code
                                        1:ExtendedCOMMNDRrrRRRrrrXXXXX
-                                       2:ARGUEMENT
+                                       2:ARGUMENT
                                        3:...
                                        ------------------------------
                                        extended_command (rrr,RRR,Rrr,ARGS)
@@ -484,7 +483,7 @@ static Lisp_Object Vccl_program_table;
    If the element is t or lambda, finish without changing reg[rrr].
    If the element is a number, set reg[rrr] to the number and finish.
 
-   Detail of the map structure is descibed in the comment for
+   Detail of the map structure is described in the comment for
    CCL_MapMultiple below.  */
 
 #define CCL_IterateMultipleMap 0x10 /* Iterate multiple maps
@@ -552,7 +551,7 @@ static Lisp_Object Vccl_program_table;
    But, when VALm is mapped to VALn and VALn is not a number, the
    mapping proceed as below:
 
-   If VALn is nil, the lastest map is ignored and the mapping of VALm
+   If VALn is nil, the last map is ignored and the mapping of VALm
    proceed to the next map.
 
    In VALn is t, VALm is reverted to reg[rrr] and the mapping of VALm
@@ -561,7 +560,7 @@ static Lisp_Object Vccl_program_table;
    If VALn is lambda, move to the next map set like reaching to the
    end of the current map set.
 
-   If VALn is a symbol, call the CCL program refered by it.
+   If VALn is a symbol, call the CCL program referred by it.
    Then, use reg[rrr] as a mapped value except for -1, -2 and -3.
    Such special values are regarded as nil, t, and lambda respectively.
 
@@ -823,7 +822,7 @@ while (0)
    : (charset = CHARSET_FROM_ID ((id)), DECODE_CHAR (charset, (code))))
 
 /* Encode character C by some of charsets in CHARSET_LIST.  Set ID to
-   the id of the used charset, ENCODED to the resulf of encoding.
+   the id of the used charset, ENCODED to the result of encoding.
    Assume that we can use the variable `charset'.  */
 
 #define CCL_ENCODE_CHAR(c, charset_list, id, encoded)          \
@@ -1371,7 +1370,7 @@ ccl_driver (struct ccl_program *ccl, int *source, int *destination, int src_size
 
                for (;i < j;i++)
                  {
-
+                   if (!VECTORP (Vcode_conversion_map_vector)) continue;
                    size = ASIZE (Vcode_conversion_map_vector);
                    point = XINT (ccl_prog[ic++]);
                    if (! (0 <= point && point < size)) continue;
@@ -1447,7 +1446,8 @@ ccl_driver (struct ccl_program *ccl, int *source, int *destination, int src_size
            case CCL_MapMultiple:
              {
                Lisp_Object map, content, attrib, value;
-               int point, size, map_vector_size;
+               EMACS_INT point;
+               ptrdiff_t size, map_vector_size;
                int map_set_rest_length, fin_ic;
                int current_ic = this_ic;
 
@@ -1530,6 +1530,8 @@ ccl_driver (struct ccl_program *ccl, int *source, int *destination, int src_size
                        break;
                      }
                  }
+               if (!VECTORP (Vcode_conversion_map_vector))
+                 CCL_INVALID_CMD;
                map_vector_size = ASIZE (Vcode_conversion_map_vector);
 
                do {
@@ -1652,7 +1654,8 @@ ccl_driver (struct ccl_program *ccl, int *source, int *destination, int src_size
                int point;
                j = XINT (ccl_prog[ic++]); /* map_id */
                op = reg[rrr];
-               if (j >= ASIZE (Vcode_conversion_map_vector))
+               if (! (VECTORP (Vcode_conversion_map_vector)
+                      && j < ASIZE (Vcode_conversion_map_vector)))
                  {
                    reg[RRR] = -1;
                    break;
@@ -1665,6 +1668,7 @@ ccl_driver (struct ccl_program *ccl, int *source, int *destination, int src_size
                  }
                map = XCDR (map);
                if (! (VECTORP (map)
+                      && 0 < ASIZE (map)
                       && INTEGERP (AREF (map, 0))
                       && XINT (AREF (map, 0)) <= op
                       && op - XINT (AREF (map, 0)) + 1 < ASIZE (map)))
@@ -1724,14 +1728,14 @@ ccl_driver (struct ccl_program *ccl, int *source, int *destination, int src_size
       switch (ccl->status)
        {
        case CCL_STAT_INVALID_CMD:
-         sprintf (msg, "\nCCL: Invalid command %x (ccl_code = %x) at %d.",
-                   code & 0x1F, code, this_ic);
+         msglen = sprintf (msg,
+                           "\nCCL: Invalid command %x (ccl_code = %x) at %d.",
+                           code & 0x1F, code, this_ic);
 #ifdef CCL_DEBUG
          {
            int i = ccl_backtrace_idx - 1;
            int j;
 
-           msglen = strlen (msg);
            if (dst + msglen <= (dst_bytes ? dst_end : src))
              {
                memcpy (dst, msg, msglen);
@@ -1743,8 +1747,7 @@ ccl_driver (struct ccl_program *ccl, int *source, int *destination, int src_size
                if (i < 0) i = CCL_DEBUG_BACKTRACE_LEN - 1;
                if (ccl_backtrace_table[i] == 0)
                  break;
-               sprintf (msg, " %d", ccl_backtrace_table[i]);
-               msglen = strlen (msg);
+               msglen = sprintf (msg, " %d", ccl_backtrace_table[i]);
                if (dst + msglen > (dst_bytes ? dst_end : src))
                  break;
                memcpy (dst, msg, msglen);
@@ -1756,15 +1759,13 @@ ccl_driver (struct ccl_program *ccl, int *source, int *destination, int src_size
          break;
 
        case CCL_STAT_QUIT:
-         if (! ccl->quit_silently)
-           sprintf (msg, "\nCCL: Quited.");
+         msglen = ccl->quit_silently ? 0 : sprintf (msg, "\nCCL: Quitted.");
          break;
 
        default:
-         sprintf (msg, "\nCCL: Unknown error type (%d)", ccl->status);
+         msglen = sprintf (msg, "\nCCL: Unknown error type (%d)", ccl->status);
        }
 
-      msglen = strlen (msg);
       if (msglen <= dst_end - dst)
        {
          for (i = 0; i < msglen; i++)
@@ -1803,7 +1804,7 @@ ccl_driver (struct ccl_program *ccl, int *source, int *destination, int src_size
 
 /* Resolve symbols in the specified CCL code (Lisp vector).  This
    function converts symbols of code conversion maps and character
-   translation tables embeded in the CCL code into their ID numbers.
+   translation tables embedded in the CCL code into their ID numbers.
 
    The return value is a new vector in which all symbols are resolved,
    Qt if resolving of some symbol failed,
@@ -2096,7 +2097,7 @@ usage: (ccl-execute-on-string CCL-PROGRAM STATUS STRING &optional CONTINUE UNIBY
   outbufsize = (ccl.buf_magnification
                ? str_bytes * ccl.buf_magnification + 256
                : str_bytes + 256);
-  outp = outbuf = (unsigned char *) xmalloc (outbufsize);
+  outp = outbuf = xmalloc (outbufsize);
 
   consumed_chars = consumed_bytes = 0;
   produced_chars = 0;
@@ -2257,12 +2258,16 @@ DEFUN ("register-code-conversion-map", Fregister_code_conversion_map,
 Return index number of the registered map.  */)
   (Lisp_Object symbol, Lisp_Object map)
 {
-  ptrdiff_t len = ASIZE (Vcode_conversion_map_vector);
+  ptrdiff_t len;
   ptrdiff_t i;
   Lisp_Object idx;
 
   CHECK_SYMBOL (symbol);
   CHECK_VECTOR (map);
+  if (! VECTORP (Vcode_conversion_map_vector))
+    error ("Invalid code-conversion-map-vector");
+
+  len = ASIZE (Vcode_conversion_map_vector);
 
   for (i = 0; i < len; i++)
     {