From cb1caeaf2ba26df05e8f9bcd4aa63203cef781fb Mon Sep 17 00:00:00 2001 From: Dmitry Antipov Date: Tue, 10 Jul 2012 11:59:31 +0400 Subject: [PATCH] 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. --- src/ChangeLog | 9 ++++++++- src/buffer.c | 3 ++- src/font.c | 9 ++++++--- src/lread.c | 22 ++++++++++++---------- 4 files changed, 28 insertions(+), 15 deletions(-) diff --git a/src/ChangeLog b/src/ChangeLog index de0f729f915..5815c83ae1e 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,11 +1,18 @@ 2012-07-10 Dmitry Antipov + 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 + Avoid calls to strlen in path processing functions. * fileio.c (file_name_as_directory): Add comment. Change to add 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. diff --git a/src/buffer.c b/src/buffer.c index 28cede3916c..4999639128d 100644 --- a/src/buffer.c +++ b/src/buffer.c @@ -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 diff --git a/src/font.c b/src/font.c index 4f0f814a583..1eca1c2eb29 100644 --- a/src/font.c +++ b/src/font.c @@ -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, diff --git a/src/lread.c b/src/lread.c index c69190c37b6..900a25372d8 100644 --- a/src/lread.c +++ b/src/lread.c @@ -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)) { -- 2.11.4.GIT