aboutsummaryrefslogtreecommitdiffstats
path: root/lib-src
diff options
context:
space:
mode:
authorPaul Eggert2019-06-25 14:53:39 -0700
committerPaul Eggert2019-06-25 14:55:08 -0700
commitd7c6836288c91bb639956cb8c748dd6597c55cd4 (patch)
tree687f6f257d3cb5f4bd54eb634aef2737eb9c0856 /lib-src
parent349b778dde7f583a92dd1531aef3ff5c336e9aee (diff)
downloademacs-d7c6836288c91bb639956cb8c748dd6597c55cd4.tar.gz
emacs-d7c6836288c91bb639956cb8c748dd6597c55cd4.zip
Avoid some strlen work, primarily via strnlen
* admin/merge-gnulib (GNULIB_MODULES): Add strnlen. * lib-src/etags.c (find_entries): * src/emacs.c (main): * src/nsmenu.m (parseKeyEquiv:): * src/nsterm.m (ns_xlfd_to_fontname): * src/term.c (vfatal): Prefer !*X to !strlen (X). * lib-src/etags.c (pfnote, add_regex): * lib-src/pop.c (pop_open): * lib-src/update-game-score.c (main): * lwlib/lwlib.c (lw_separator_p): * src/doprnt.c (doprnt): * src/emacs.c (main): * src/inotify.c (inotifyevent_to_event): * src/keyboard.c (menu_separator_name_p, parse_tool_bar_item): * src/sysdep.c (get_current_dir_name_or_unreachable): * src/xdisp.c (store_mode_line_string): Use strnlen to avoid unnecessary work with strlen. * lib-src/etags.c (Prolog_functions, prolog_pr) (Erlang_functions, erlang_func): Prefer ptrdiff_t to size_t when either will do. (prolog_pr, erlang_func): New arg LASTLEN, to avoid unnecessary strlen call. All callers changed. * lib/gnulib.mk.in, m4/gnulib-comp.m4: Regenerate. * lib/strnlen.c, m4/strnlen.m4: New files, copied from Gnulib. * lwlib/lwlib.c (lw_separator_p): * src/json.c (json_has_prefix): Use strncmp to avoid unecessary work with strlen + memcmp. * src/process.c (set_socket_option): Use SBYTES instead of strlen.
Diffstat (limited to 'lib-src')
-rw-r--r--lib-src/etags.c155
-rw-r--r--lib-src/pop.c4
-rw-r--r--lib-src/update-game-score.c2
3 files changed, 69 insertions, 92 deletions
diff --git a/lib-src/etags.c b/lib-src/etags.c
index 442b622cceb..036c485d0bb 100644
--- a/lib-src/etags.c
+++ b/lib-src/etags.c
@@ -1811,7 +1811,7 @@ find_entries (FILE *inf)
1811 } 1811 }
1812 *cp = '\0'; 1812 *cp = '\0';
1813 1813
1814 if (strlen (lp) > 0) 1814 if (*lp)
1815 { 1815 {
1816 lang = get_language_from_interpreter (lp); 1816 lang = get_language_from_interpreter (lp);
1817 if (lang != NULL && lang->function != NULL) 1817 if (lang != NULL && lang->function != NULL)
@@ -2012,7 +2012,7 @@ pfnote (char *name, bool is_func, char *linestart, int linelen, int lno,
2012 np->left = np->right = NULL; 2012 np->left = np->right = NULL;
2013 if (CTAGS && !cxref_style) 2013 if (CTAGS && !cxref_style)
2014 { 2014 {
2015 if (strlen (linestart) < 50) 2015 if (strnlen (linestart, 50) < 50)
2016 np->regex = concat (linestart, "$", ""); 2016 np->regex = concat (linestart, "$", "");
2017 else 2017 else
2018 np->regex = savenstr (linestart, 50); 2018 np->regex = savenstr (linestart, 50);
@@ -5883,20 +5883,15 @@ HTML_labels (FILE *inf)
5883 * Original code by Sunichirou Sugou (1989) 5883 * Original code by Sunichirou Sugou (1989)
5884 * Rewritten by Anders Lindgren (1996) 5884 * Rewritten by Anders Lindgren (1996)
5885 */ 5885 */
5886static size_t prolog_pr (char *, char *); 5886static ptrdiff_t prolog_pr (char *, char *, ptrdiff_t);
5887static void prolog_skip_comment (linebuffer *, FILE *); 5887static void prolog_skip_comment (linebuffer *, FILE *);
5888static size_t prolog_atom (char *, size_t); 5888static size_t prolog_atom (char *, size_t);
5889 5889
5890static void 5890static void
5891Prolog_functions (FILE *inf) 5891Prolog_functions (FILE *inf)
5892{ 5892{
5893 char *cp, *last; 5893 char *cp, *last = NULL;
5894 size_t len; 5894 ptrdiff_t lastlen = 0, allocated = 0;
5895 size_t allocated;
5896
5897 allocated = 0;
5898 len = 0;
5899 last = NULL;
5900 5895
5901 LOOP_ON_INPUT_LINES (inf, lb, cp) 5896 LOOP_ON_INPUT_LINES (inf, lb, cp)
5902 { 5897 {
@@ -5906,17 +5901,22 @@ Prolog_functions (FILE *inf)
5906 continue; 5901 continue;
5907 else if (cp[0] == '/' && cp[1] == '*') /* comment. */ 5902 else if (cp[0] == '/' && cp[1] == '*') /* comment. */
5908 prolog_skip_comment (&lb, inf); 5903 prolog_skip_comment (&lb, inf);
5909 else if ((len = prolog_pr (cp, last)) > 0) 5904 else
5910 { 5905 {
5911 /* Predicate or rule. Store the function name so that we 5906 ptrdiff_t len = prolog_pr (cp, last, lastlen);
5912 only generate a tag for the first clause. */ 5907 if (0 < len)
5913 if (last == NULL) 5908 {
5914 last = xnew (len + 1, char); 5909 /* Store the predicate name to avoid generating duplicate
5915 else if (len + 1 > allocated) 5910 tags later. */
5916 xrnew (last, len + 1, char); 5911 if (allocated <= len)
5917 allocated = len + 1; 5912 {
5918 memcpy (last, cp, len); 5913 xrnew (last, len + 1, char);
5919 last[len] = '\0'; 5914 allocated = len + 1;
5915 }
5916 memcpy (last, cp, len);
5917 last[len] = '\0';
5918 lastlen = len;
5919 }
5920 } 5920 }
5921 } 5921 }
5922 free (last); 5922 free (last);
@@ -5949,33 +5949,25 @@ prolog_skip_comment (linebuffer *plb, FILE *inf)
5949 * Return the size of the name of the predicate or rule, or 0 if no 5949 * Return the size of the name of the predicate or rule, or 0 if no
5950 * header was found. 5950 * header was found.
5951 */ 5951 */
5952static size_t 5952static ptrdiff_t
5953prolog_pr (char *s, char *last) 5953prolog_pr (char *s, char *last, ptrdiff_t lastlen)
5954
5955 /* Name of last clause. */
5956{ 5954{
5957 size_t pos; 5955 ptrdiff_t len = prolog_atom (s, 0);
5958 size_t len; 5956 if (len == 0)
5959
5960 pos = prolog_atom (s, 0);
5961 if (! pos)
5962 return 0; 5957 return 0;
5958 ptrdiff_t pos = skip_spaces (s + len) - s;
5963 5959
5964 len = pos; 5960 /* Save only the first clause. */
5965 pos = skip_spaces (s + pos) - s;
5966
5967 if ((s[pos] == '.' 5961 if ((s[pos] == '.'
5968 || (s[pos] == '(' && (pos += 1)) 5962 || (s[pos] == '(' && (pos += 1))
5969 || (s[pos] == ':' && s[pos + 1] == '-' && (pos += 2))) 5963 || (s[pos] == ':' && s[pos + 1] == '-' && (pos += 2)))
5970 && (last == NULL /* save only the first clause */ 5964 && ! (lastlen == len && memcmp (s, last, len) == 0))
5971 || len != strlen (last) 5965 {
5972 || !strneq (s, last, len))) 5966 make_tag (s, len, true, s, pos, lineno, linecharno);
5973 { 5967 return len;
5974 make_tag (s, len, true, s, pos, lineno, linecharno); 5968 }
5975 return len; 5969
5976 } 5970 return 0;
5977 else
5978 return 0;
5979} 5971}
5980 5972
5981/* 5973/*
@@ -6043,21 +6035,15 @@ prolog_atom (char *s, size_t pos)
6043 * Assumes that Erlang functions start at column 0. 6035 * Assumes that Erlang functions start at column 0.
6044 * Original code by Anders Lindgren (1996) 6036 * Original code by Anders Lindgren (1996)
6045 */ 6037 */
6046static int erlang_func (char *, char *, int *); 6038static int erlang_func (char *, char *, ptrdiff_t, ptrdiff_t *);
6047static void erlang_attribute (char *); 6039static void erlang_attribute (char *);
6048static int erlang_atom (char *); 6040static int erlang_atom (char *);
6049 6041
6050static void 6042static void
6051Erlang_functions (FILE *inf) 6043Erlang_functions (FILE *inf)
6052{ 6044{
6053 char *cp, *last; 6045 char *cp, *last = NULL;
6054 int len; 6046 ptrdiff_t lastlen = 0, allocated = 0;
6055 int allocated;
6056 int name_offset = 0;
6057
6058 allocated = 0;
6059 len = 0;
6060 last = NULL;
6061 6047
6062 LOOP_ON_INPUT_LINES (inf, lb, cp) 6048 LOOP_ON_INPUT_LINES (inf, lb, cp)
6063 { 6049 {
@@ -6078,19 +6064,23 @@ Erlang_functions (FILE *inf)
6078 last = NULL; 6064 last = NULL;
6079 } 6065 }
6080 } 6066 }
6081 else if ((len = erlang_func (cp, last, &name_offset)) > 0) 6067 else
6082 { 6068 {
6083 /* 6069 ptrdiff_t name_offset;
6084 * Function. Store the function name so that we only 6070 ptrdiff_t len = erlang_func (cp, last, lastlen, &name_offset);
6085 * generates a tag for the first clause. 6071 if (0 < len)
6086 */ 6072 {
6087 if (last == NULL) 6073 /* Store the function name to avoid generating duplicate
6088 last = xnew (len + 1, char); 6074 tags later. */
6089 else if (len + 1 > allocated) 6075 if (allocated <= len)
6090 xrnew (last, len + 1, char); 6076 {
6091 allocated = len + 1; 6077 xrnew (last, len + 1, char);
6092 memcpy (last, cp + name_offset, len); 6078 allocated = len + 1;
6093 last[len] = '\0'; 6079 }
6080 memcpy (last, cp + name_offset, len);
6081 last[len] = '\0';
6082 lastlen = len;
6083 }
6094 } 6084 }
6095 } 6085 }
6096 free (last); 6086 free (last);
@@ -6108,40 +6098,27 @@ Erlang_functions (FILE *inf)
6108 * was found. 6098 * was found.
6109 */ 6099 */
6110static int 6100static int
6111erlang_func (char *s, char *last, int *name_offset) 6101erlang_func (char *s, char *last, ptrdiff_t lastlen, ptrdiff_t *name_offset)
6112
6113 /* Name of last clause. */
6114{ 6102{
6115 int pos;
6116 int len;
6117 char *name = s; 6103 char *name = s;
6118 6104 ptrdiff_t len = erlang_atom (s);
6119 pos = erlang_atom (s); 6105 if (len == 0)
6120 if (pos < 1)
6121 return 0; 6106 return 0;
6122 6107 ptrdiff_t pos = skip_spaces (s + len) - s;
6123 len = pos;
6124 pos = skip_spaces (s + pos) - s;
6125 6108
6126 /* If the name is quoted, the quotes are not part of the name. */ 6109 /* If the name is quoted, the quotes are not part of the name. */
6127 if (len > 2 && name[0] == '\'' && name[len - 1] == '\'') 6110 bool quoted = 2 < len && name[0] == '\'' && name[len - 1] == '\'';
6128 { 6111 name += quoted;
6129 *name_offset = 1; 6112 len -= 2 * quoted;
6130 name++;
6131 len -= 2;
6132 }
6133 else
6134 *name_offset = 0;
6135 6113
6136 /* Save only the first clause. */ 6114 /* Save only the first clause. */
6137 if (s[pos++] == '(' 6115 if (s[pos++] == '('
6138 && (last == NULL 6116 && ! (lastlen == len && memcmp (name, last, len) == 0))
6139 || len != (int)strlen (last) 6117 {
6140 || !strneq (name, last, len))) 6118 make_tag (s, len, true, s, pos, lineno, linecharno);
6141 { 6119 *name_offset = quoted;
6142 make_tag (name, len, true, s, pos, lineno, linecharno); 6120 return len;
6143 return len; 6121 }
6144 }
6145 6122
6146 return 0; 6123 return 0;
6147} 6124}
@@ -6363,7 +6340,7 @@ add_regex (char *regexp_pattern, language *lang)
6363 single_line = false; /* dot does not match newline */ 6340 single_line = false; /* dot does not match newline */
6364 6341
6365 6342
6366 if (strlen (regexp_pattern) < 3) 6343 if (strnlen (regexp_pattern, 3) < 3)
6367 { 6344 {
6368 error ("null regexp"); 6345 error ("null regexp");
6369 return; 6346 return;
diff --git a/lib-src/pop.c b/lib-src/pop.c
index c14808d6d37..e4bd6c04965 100644
--- a/lib-src/pop.c
+++ b/lib-src/pop.c
@@ -285,7 +285,7 @@ pop_open (char *host, char *username, char *password, int flags)
285 /* 285 /*
286 * I really shouldn't use the pop_error variable like this, but.... 286 * I really shouldn't use the pop_error variable like this, but....
287 */ 287 */
288 if (strlen (username) > ERROR_MAX - 6) 288 if (strnlen (username, ERROR_MAX - 6 + 1) == ERROR_MAX - 6 + 1)
289 { 289 {
290 pop_close (server); 290 pop_close (server);
291 strcpy (pop_error, 291 strcpy (pop_error,
@@ -299,7 +299,7 @@ pop_open (char *host, char *username, char *password, int flags)
299 return (0); 299 return (0);
300 } 300 }
301 301
302 if (strlen (password) > ERROR_MAX - 6) 302 if (strnlen (password, ERROR_MAX - 6 + 1) == ERROR_MAX - 6 + 1)
303 { 303 {
304 pop_close (server); 304 pop_close (server);
305 strcpy (pop_error, 305 strcpy (pop_error,
diff --git a/lib-src/update-game-score.c b/lib-src/update-game-score.c
index 8a6f46b38e7..782425186e8 100644
--- a/lib-src/update-game-score.c
+++ b/lib-src/update-game-score.c
@@ -240,7 +240,7 @@ main (int argc, char **argv)
240 if (! user) 240 if (! user)
241 lose_syserr ("Couldn't determine user id"); 241 lose_syserr ("Couldn't determine user id");
242 data = argv[optind + 2]; 242 data = argv[optind + 2];
243 if (strlen (data) > MAX_DATA_LEN) 243 if (strnlen (data, MAX_DATA_LEN + 1) == MAX_DATA_LEN + 1)
244 data[MAX_DATA_LEN] = '\0'; 244 data[MAX_DATA_LEN] = '\0';
245 nl = strchr (data, '\n'); 245 nl = strchr (data, '\n');
246 if (nl) 246 if (nl)