* alloc.c (pure_bytes_used_lisp, pure_bytes_used_non_lisp):
[bpt/emacs.git] / src / doc.c
index 354aff8..9fbeb0b 100644 (file)
--- a/src/doc.c
+++ b/src/doc.c
@@ -39,7 +39,7 @@ Lisp_Object Qfunction_documentation;
 extern Lisp_Object Qclosure;
 /* Buffer used for reading from documentation file.  */
 static char *get_doc_string_buffer;
-static int get_doc_string_buffer_size;
+static ptrdiff_t get_doc_string_buffer_size;
 
 static unsigned char *read_bytecode_pointer;
 static Lisp_Object Fdocumentation_property (Lisp_Object, Lisp_Object,
@@ -86,9 +86,11 @@ get_doc_string (Lisp_Object filepos, int unibyte, int definition)
   register int fd;
   register char *name;
   register char *p, *p1;
-  EMACS_INT minsize;
-  EMACS_INT offset, position;
+  ptrdiff_t minsize;
+  int offset;
+  EMACS_INT position;
   Lisp_Object file, tem;
+  USE_SAFE_ALLOCA;
 
   if (INTEGERP (filepos))
     {
@@ -122,7 +124,7 @@ get_doc_string (Lisp_Object filepos, int unibyte, int definition)
       /* sizeof ("../etc/") == 8 */
       if (minsize < 8)
        minsize = 8;
-      name = (char *) alloca (minsize + SCHARS (file) + 8);
+      SAFE_ALLOCA (name, char *, minsize + SCHARS (file) + 8);
       strcpy (name, SSDATA (Vdoc_directory));
       strcat (name, SSDATA (file));
     }
@@ -153,33 +155,35 @@ get_doc_string (Lisp_Object filepos, int unibyte, int definition)
   /* Make sure we read at least 1024 bytes before `position'
      so we can check the leading text for consistency.  */
   offset = min (position, max (1024, position % (8 * 1024)));
-  if (0 > lseek (fd, position - offset, 0))
+  if (TYPE_MAXIMUM (off_t) < position
+      || lseek (fd, position - offset, 0) < 0)
     {
       emacs_close (fd);
-      error ("Position %"pEd" out of range in doc string file \"%s\"",
+      error ("Position %"pI"d out of range in doc string file \"%s\"",
             position, name);
     }
 
+  SAFE_FREE ();
+
   /* Read the doc string into get_doc_string_buffer.
      P points beyond the data just read.  */
 
   p = get_doc_string_buffer;
   while (1)
     {
-      EMACS_INT space_left = (get_doc_string_buffer_size
+      ptrdiff_t space_left = (get_doc_string_buffer_size - 1
                              - (p - get_doc_string_buffer));
       int nread;
 
       /* Allocate or grow the buffer if we need to.  */
-      if (space_left == 0)
+      if (space_left <= 0)
        {
-         EMACS_INT in_buffer = p - get_doc_string_buffer;
-         get_doc_string_buffer_size += 16 * 1024;
-         get_doc_string_buffer
-           = (char *) xrealloc (get_doc_string_buffer,
-                                get_doc_string_buffer_size + 1);
+         ptrdiff_t in_buffer = p - get_doc_string_buffer;
+         get_doc_string_buffer =
+           xpalloc (get_doc_string_buffer, &get_doc_string_buffer_size,
+                    16 * 1024, -1, 1);
          p = get_doc_string_buffer + in_buffer;
-         space_left = (get_doc_string_buffer_size
+         space_left = (get_doc_string_buffer_size - 1
                        - (p - get_doc_string_buffer));
        }
 
@@ -253,7 +257,12 @@ get_doc_string (Lisp_Object filepos, int unibyte, int definition)
          else if (c == '_')
            *to++ = 037;
          else
-           error ("Invalid data in documentation file -- ^A followed by code 0%o", c);
+           {
+             unsigned char uc = c;
+             error ("\
+Invalid data in documentation file -- %c followed by code %03o",
+                    1, uc);
+           }
        }
       else
        *to++ = *from++;
@@ -273,7 +282,7 @@ get_doc_string (Lisp_Object filepos, int unibyte, int definition)
   else
     {
       /* The data determines whether the string is multibyte.  */
-      EMACS_INT nchars =
+      ptrdiff_t nchars =
        multibyte_chars_in_text (((unsigned char *) get_doc_string_buffer
                                  + offset),
                                 to - (get_doc_string_buffer + offset));
@@ -345,10 +354,12 @@ string is passed through `substitute-command-keys'.  */)
     {
       if (XSUBR (fun)->doc == 0)
        return Qnil;
-      else if ((EMACS_INT) XSUBR (fun)->doc >= 0)
+      /* FIXME: This is not portable, as it assumes that string
+        pointers have the top bit clear.  */
+      else if ((intptr_t) XSUBR (fun)->doc >= 0)
        doc = build_string (XSUBR (fun)->doc);
       else
-       doc = make_number ((EMACS_INT) XSUBR (fun)->doc);
+       doc = make_number ((intptr_t) XSUBR (fun)->doc);
     }
   else if (COMPILEDP (fun))
     {
@@ -494,8 +505,7 @@ aren't strings.  */)
 /* Scanning the DOC files and placing docstring offsets into functions.  */
 
 static void
-store_function_docstring (Lisp_Object fun, EMACS_INT offset)
-/* Use EMACS_INT because we get offset from pointer subtraction.  */
+store_function_docstring (Lisp_Object fun, ptrdiff_t offset)
 {
   fun = indirect_function (fun);
 
@@ -503,7 +513,10 @@ store_function_docstring (Lisp_Object fun, EMACS_INT offset)
 
   /* Lisp_Subrs have a slot for it.  */
   if (SUBRP (fun))
-    XSUBR (fun)->doc = (char *) - offset;
+    {
+      intptr_t negative_offset = - offset;
+      XSUBR (fun)->doc = (char *) negative_offset;
+    }
 
   /* If it's a lisp form, stick it in the form.  */
   else if (CONSP (fun))
@@ -547,7 +560,7 @@ the same file name is found in the `doc-directory'.  */)
 {
   int fd;
   char buf[1024 + 1];
-  register EMACS_INT filled;
+  register int filled;
   register EMACS_INT pos;
   register char *p;
   Lisp_Object sym;
@@ -582,7 +595,7 @@ the same file name is found in the `doc-directory'.  */)
 
     for (beg = buildobj; *beg; beg = end)
       {
-        EMACS_INT len;
+        ptrdiff_t len;
 
         while (*beg && isspace (*beg)) ++beg;
 
@@ -630,7 +643,7 @@ the same file name is found in the `doc-directory'.  */)
               if (end - p > 4 && end[-2] == '.'
                   && (end[-1] == 'o' || end[-1] == 'c'))
                 {
-                  EMACS_INT len = end - p - 2;
+                  ptrdiff_t len = end - p - 2;
                   char *fromfile = alloca (len + 1);
                   strncpy (fromfile, &p[2], len);
                   fromfile[len] = 0;
@@ -671,7 +684,7 @@ the same file name is found in the `doc-directory'.  */)
                ; /* Just a source file name boundary marker.  Ignore it.  */
 
              else
-               error ("DOC file invalid at position %"pEd, pos);
+               error ("DOC file invalid at position %"pI"d", pos);
            }
        }
       pos += end - buf;
@@ -703,16 +716,16 @@ a new string, without any text properties, is returned.  */)
   int changed = 0;
   register unsigned char *strp;
   register char *bufp;
-  EMACS_INT idx;
-  EMACS_INT bsize;
+  ptrdiff_t idx;
+  ptrdiff_t bsize;
   Lisp_Object tem;
   Lisp_Object keymap;
   unsigned char *start;
-  EMACS_INT length, length_byte;
+  ptrdiff_t length, length_byte;
   Lisp_Object name;
   struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
   int multibyte;
-  EMACS_INT nchars;
+  ptrdiff_t nchars;
 
   if (NILP (string))
     return Qnil;
@@ -764,7 +777,7 @@ a new string, without any text properties, is returned.  */)
        }
       else if (strp[0] == '\\' && strp[1] == '[')
        {
-         EMACS_INT start_idx;
+         ptrdiff_t start_idx;
          int follow_remap = 1;
 
          changed = 1;
@@ -787,7 +800,7 @@ a new string, without any text properties, is returned.  */)
        do_remap:
          tem = Fwhere_is_internal (name, keymap, Qt, Qnil, Qnil);
 
-         if (VECTORP (tem) && XVECTOR (tem)->size > 1
+         if (VECTORP (tem) && ASIZE (tem) > 1
              && EQ (AREF (tem, 0), Qremap) && SYMBOLP (AREF (tem, 1))
              && follow_remap)
            {
@@ -803,7 +816,9 @@ a new string, without any text properties, is returned.  */)
 
          if (NILP (tem))       /* but not on any keys */
            {
-             EMACS_INT offset = bufp - buf;
+             ptrdiff_t offset = bufp - buf;
+             if (STRING_BYTES_BOUND - 4 < bsize)
+               string_overflow ();
              buf = (char *) xrealloc (buf, bsize += 4);
              bufp = buf + offset;
              memcpy (bufp, "M-x ", 4);
@@ -826,7 +841,7 @@ a new string, without any text properties, is returned.  */)
       else if (strp[0] == '\\' && (strp[1] == '{' || strp[1] == '<'))
        {
          struct buffer *oldbuf;
-         EMACS_INT start_idx;
+         ptrdiff_t start_idx;
          /* This is for computing the SHADOWS arg for describe_map_tree.  */
          Lisp_Object active_maps = Fcurrent_active_maps (Qnil, Qnil);
          Lisp_Object earlier_maps;
@@ -897,7 +912,9 @@ a new string, without any text properties, is returned.  */)
          length_byte = SBYTES (tem);
        subst:
          {
-           EMACS_INT offset = bufp - buf;
+           ptrdiff_t offset = bufp - buf;
+           if (STRING_BYTES_BOUND - length_byte < bsize)
+             string_overflow ();
            buf = (char *) xrealloc (buf, bsize += length_byte);
            bufp = buf + offset;
            memcpy (bufp, start, length_byte);
@@ -935,8 +952,7 @@ a new string, without any text properties, is returned.  */)
 void
 syms_of_doc (void)
 {
-  Qfunction_documentation = intern_c_string ("function-documentation");
-  staticpro (&Qfunction_documentation);
+  DEFSYM (Qfunction_documentation, "function-documentation");
 
   DEFVAR_LISP ("internal-doc-file-name", Vdoc_file_name,
               doc: /* Name of file containing documentation strings of built-in symbols.  */);