Avoid calls to strlen in miscellaneous functions.
authorDmitry Antipov <dmantipov@yandex.ru>
Tue, 10 Jul 2012 07:59:31 +0000 (11:59 +0400)
committerDmitry Antipov <dmantipov@yandex.ru>
Tue, 10 Jul 2012 07:59:31 +0000 (11:59 +0400)
* buffer.c (init_buffer): Use precalculated len, adjust if needed.
* font.c (Ffont_xlfd_name): Likewise.  Change to call make_string.
* lread.c (openp): Likewise.

src/ChangeLog
src/buffer.c
src/font.c
src/lread.c

index de0f729..5815c83 100644 (file)
@@ -1,3 +1,10 @@
+2012-07-10  Dmitry Antipov  <dmantipov@yandex.ru>
+
+       Avoid calls to strlen in miscellaneous functions.
+       * buffer.c (init_buffer): Use precalculated len, adjust if needed.
+       * font.c (Ffont_xlfd_name): Likewise.  Change to call make_string.
+       * lread.c (openp): Likewise.
+
 2012-07-10  Dmitry Antipov  <dmantipov@yandex.ru>
 
        Avoid calls to strlen in path processing functions.
@@ -5,7 +12,7 @@
        srclen argument and return the length of result.  Adjust users
        accordingly.
        (directory_file_name): Fix comment.  Change to add srclen argument,
-       swap 1nd and 2st arguments to obey the common convention.  Adjust
+       swap 1st and 2nd arguments to obey the common convention.  Adjust
        users accordingly.
        * filelock.c (fill_in_lock_file_name): Avoid calls to strlen.
 
index 28cede3..4999639 100644 (file)
@@ -5091,9 +5091,10 @@ init_buffer (void)
        fatal ("`get_current_dir_name' failed: %s\n", strerror (errno));
       pwd[len] = DIRECTORY_SEP;
       pwd[len + 1] = '\0';
+      len++;
     }
 
-  BVAR (current_buffer, directory) = make_unibyte_string (pwd, strlen (pwd));
+  BVAR (current_buffer, directory) = make_unibyte_string (pwd, len);
   if (! NILP (BVAR (&buffer_defaults, enable_multibyte_characters)))
     /* At this moment, we still don't know how to decode the
        directory name.  So, we keep the bytes in multibyte form so
index 4f0f814..1eca1c2 100644 (file)
@@ -4218,7 +4218,7 @@ the consecutive wildcards are folded into one.  */)
   (Lisp_Object font, Lisp_Object fold_wildcards)
 {
   char name[256];
-  int pixel_size = 0;
+  int namelen, pixel_size = 0;
 
   CHECK_FONT (font);
 
@@ -4232,11 +4232,13 @@ the consecutive wildcards are folded into one.  */)
          if (NILP (fold_wildcards))
            return font_name;
          strcpy (name, SSDATA (font_name));
+         namelen = SBYTES (font_name);
          goto done;
        }
       pixel_size = XFONT_OBJECT (font)->pixel_size;
     }
-  if (font_unparse_xlfd (font, pixel_size, name, 256) < 0)
+  namelen = font_unparse_xlfd (font, pixel_size, name, 256);
+  if (namelen < 0)
     return Qnil;
  done:
   if (! NILP (fold_wildcards))
@@ -4246,11 +4248,12 @@ the consecutive wildcards are folded into one.  */)
       while ((p1 = strstr (p0, "-*-*")))
        {
          strcpy (p1, p1 + 2);
+         namelen -= 2;
          p0 = p1;
        }
     }
 
-  return build_string (name);
+  return make_string (name, namelen);
 }
 
 DEFUN ("clear-font-cache", Fclear_font_cache, Sclear_font_cache, 0, 0, 0,
index c69190c..900a253 100644 (file)
@@ -1489,7 +1489,7 @@ openp (Lisp_Object path, Lisp_Object str, Lisp_Object suffixes, Lisp_Object *sto
       for (tail = NILP (suffixes) ? Fcons (empty_unibyte_string, Qnil) : suffixes;
           CONSP (tail); tail = XCDR (tail))
        {
-         ptrdiff_t lsuffix = SBYTES (XCAR (tail));
+         ptrdiff_t fnlen, lsuffix = SBYTES (XCAR (tail));
          Lisp_Object handler;
          int exists;
 
@@ -1499,20 +1499,22 @@ openp (Lisp_Object path, Lisp_Object str, Lisp_Object suffixes, Lisp_Object *sto
              && SREF (filename, 0) == '/'
              && SREF (filename, 1) == ':')
            {
-             strncpy (fn, SSDATA (filename) + 2,
-                      SBYTES (filename) - 2);
-             fn[SBYTES (filename) - 2] = 0;
+             fnlen = SBYTES (filename) - 2;
+             strncpy (fn, SSDATA (filename) + 2, fnlen);
+             fn[fnlen] = '\0';
            }
          else
            {
-             strncpy (fn, SSDATA (filename),
-                      SBYTES (filename));
-             fn[SBYTES (filename)] = 0;
+             fnlen = SBYTES (filename);
+             strncpy (fn, SSDATA (filename), fnlen);
+             fn[fnlen] = '\0';
            }
 
          if (lsuffix != 0)  /* Bug happens on CCI if lsuffix is 0.  */
-           strncat (fn, SSDATA (XCAR (tail)), lsuffix);
-
+           {
+             strncat (fn, SSDATA (XCAR (tail)), lsuffix);
+             fnlen += lsuffix;
+           }
          /* Check that the file exists and is not a directory.  */
          /* We used to only check for handlers on non-absolute file names:
                if (absolute)
@@ -1521,7 +1523,7 @@ openp (Lisp_Object path, Lisp_Object str, Lisp_Object suffixes, Lisp_Object *sto
                  handler = Ffind_file_name_handler (filename, Qfile_exists_p);
             It's not clear why that was the case and it breaks things like
             (load "/bar.el") where the file is actually "/bar.el.gz".  */
-         string = build_string (fn);
+         string = make_string (fn, fnlen);
          handler = Ffind_file_name_handler (string, Qfile_exists_p);
          if ((!NILP (handler) || !NILP (predicate)) && !NATNUMP (predicate))
             {