summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPaul Eggert <eggert@cs.ucla.edu>2019-06-25 14:53:39 -0700
committerPaul Eggert <eggert@cs.ucla.edu>2019-06-25 14:55:08 -0700
commitd7c6836288c91bb639956cb8c748dd6597c55cd4 (patch)
tree687f6f257d3cb5f4bd54eb634aef2737eb9c0856 /src
parent349b778dde7f583a92dd1531aef3ff5c336e9aee (diff)
downloademacs-d7c6836288c91bb639956cb8c748dd6597c55cd4.tar.gz
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 'src')
-rw-r--r--src/doprnt.c4
-rw-r--r--src/emacs.c4
-rw-r--r--src/inotify.c3
-rw-r--r--src/json.c4
-rw-r--r--src/keyboard.c15
-rw-r--r--src/nsmenu.m2
-rw-r--r--src/nsterm.m2
-rw-r--r--src/process.c6
-rw-r--r--src/sysdep.c4
-rw-r--r--src/term.c2
-rw-r--r--src/xdisp.c4
11 files changed, 21 insertions, 29 deletions
diff --git a/src/doprnt.c b/src/doprnt.c
index 5fb70634048..64bb368ee3e 100644
--- a/src/doprnt.c
+++ b/src/doprnt.c
@@ -357,8 +357,8 @@ doprnt (char *buffer, ptrdiff_t bufsize, const char *format,
if (fmtcpy[1] != 's')
minlen = atoi (&fmtcpy[1]);
string = va_arg (ap, char *);
- tem = strlen (string);
- if (STRING_BYTES_BOUND < tem)
+ tem = strnlen (string, STRING_BYTES_BOUND + 1);
+ if (tem == STRING_BYTES_BOUND + 1)
error ("String for %%s or %%S format is too long");
width = strwidth (string, tem);
goto doit1;
diff --git a/src/emacs.c b/src/emacs.c
index 1ddd10b8051..231acc0ef32 100644
--- a/src/emacs.c
+++ b/src/emacs.c
@@ -1520,8 +1520,8 @@ Using an Emacs configured with --with-x-toolkit=lucid does not have this problem
}
/* In exec'd: parse special dname into pipe and name info. */
- if (!dname_arg || !strchr (dname_arg, '\n')
- || strlen (dname_arg) < 1 || strlen (dname_arg) > 70)
+ if (!dname_arg || !*dname_arg || strnlen (dname_arg, 71) == 71
+ || !strchr (dname_arg, '\n'))
{
fprintf (stderr, "emacs daemon: daemon name absent or too long\n");
exit (EXIT_CANNOT_INVOKE);
diff --git a/src/inotify.c b/src/inotify.c
index 9a7dbb8f413..e8891aefc7a 100644
--- a/src/inotify.c
+++ b/src/inotify.c
@@ -181,8 +181,7 @@ inotifyevent_to_event (Lisp_Object watch, struct inotify_event const *ev)
if (ev->len > 0)
{
- size_t const len = strlen (ev->name);
- name = make_unibyte_string (ev->name, min (len, ev->len));
+ name = make_unibyte_string (ev->name, strnlen (ev->name, ev->len));
name = DECODE_FILE (name);
}
else
diff --git a/src/json.c b/src/json.c
index e2a4424463b..23234c767d8 100644
--- a/src/json.c
+++ b/src/json.c
@@ -197,9 +197,7 @@ init_json (void)
static bool
json_has_prefix (const char *string, const char *prefix)
{
- size_t string_len = strlen (string);
- size_t prefix_len = strlen (prefix);
- return string_len >= prefix_len && memcmp (string, prefix, prefix_len) == 0;
+ return strncmp (string, prefix, strlen (prefix)) == 0;
}
/* Return whether STRING ends with SUFFIX. */
diff --git a/src/keyboard.c b/src/keyboard.c
index 0d7f124f5f3..56916e0cb4e 100644
--- a/src/keyboard.c
+++ b/src/keyboard.c
@@ -7331,7 +7331,7 @@ menu_separator_name_p (const char *label)
{
if (!label)
return 0;
- else if (strlen (label) > 3
+ else if (strnlen (label, 4) == 4
&& memcmp (label, "--", 2) == 0
&& label[2] != '-')
{
@@ -8248,13 +8248,13 @@ parse_tool_bar_item (Lisp_Object key, Lisp_Object item)
Lisp_Object tcapt = PROP (TOOL_BAR_ITEM_CAPTION);
const char *label = SYMBOLP (tkey) ? SSDATA (SYMBOL_NAME (tkey)) : "";
const char *capt = STRINGP (tcapt) ? SSDATA (tcapt) : "";
- ptrdiff_t max_lbl =
- 2 * max (0, min (tool_bar_max_label_size, STRING_BYTES_BOUND / 2));
- char *buf = xmalloc (max_lbl + 1);
+ ptrdiff_t max_lbl_size =
+ 2 * max (0, min (tool_bar_max_label_size, STRING_BYTES_BOUND / 2)) + 1;
+ char *buf = xmalloc (max_lbl_size);
Lisp_Object new_lbl;
- ptrdiff_t caption_len = strlen (capt);
+ ptrdiff_t caption_len = strnlen (capt, max_lbl_size);
- if (caption_len <= max_lbl && capt[0] != '\0')
+ if (0 < caption_len && caption_len < max_lbl_size)
{
strcpy (buf, capt);
while (caption_len > 0 && buf[caption_len - 1] == '.')
@@ -8263,7 +8263,8 @@ parse_tool_bar_item (Lisp_Object key, Lisp_Object item)
label = capt = buf;
}
- if (strlen (label) <= max_lbl && label[0] != '\0')
+ ptrdiff_t label_len = strnlen (label, max_lbl_size);
+ if (0 < label_len && label_len < max_lbl_size)
{
ptrdiff_t j;
if (label != buf)
diff --git a/src/nsmenu.m b/src/nsmenu.m
index 3fe06cda02a..817f8cff184 100644
--- a/src/nsmenu.m
+++ b/src/nsmenu.m
@@ -611,7 +611,7 @@ ns_activate_menubar (struct frame *f)
const char *tpos = key;
keyEquivModMask = NSEventModifierFlagCommand;
- if (!key || !strlen (key))
+ if (!key || !*key)
return @"";
while (*tpos == ' ' || *tpos == '(')
diff --git a/src/nsterm.m b/src/nsterm.m
index 0ab03b46df1..8d46a49cc5e 100644
--- a/src/nsterm.m
+++ b/src/nsterm.m
@@ -9298,7 +9298,7 @@ ns_xlfd_to_fontname (const char *xlfd)
sscanf (xlfd, "-%*[^-]-%179[^-]-", name);
/* stopgap for malformed XLFD input */
- if (strlen (name) == 0)
+ if (!*name)
strcpy (name, "Monaco");
/* undo hack in ns_fontname_to_xlfd, converting '$' to '-', '_' to ' '
diff --git a/src/process.c b/src/process.c
index 6717ccb4187..15d87cf6015 100644
--- a/src/process.c
+++ b/src/process.c
@@ -2857,11 +2857,7 @@ set_socket_option (int s, Lisp_Object opt, Lisp_Object val)
This should work on all systems. KFS. 2003-09-23. */
memset (devname, 0, sizeof devname);
if (STRINGP (val))
- {
- char *arg = SSDATA (val);
- int len = min (strlen (arg), IFNAMSIZ);
- memcpy (devname, arg, len);
- }
+ memcpy (devname, SDATA (val), min (SBYTES (val), IFNAMSIZ));
else if (!NILP (val))
error ("Bad option value for %s", name);
ret = setsockopt (s, sopt->optlevel, sopt->optnum,
diff --git a/src/sysdep.c b/src/sysdep.c
index 0910b69f8bc..b702bae5818 100644
--- a/src/sysdep.c
+++ b/src/sysdep.c
@@ -281,7 +281,7 @@ get_current_dir_name_or_unreachable (void)
pwd = get_current_dir_name ();
if (pwd)
{
- if (strlen (pwd) < dirsize_max)
+ if (strnlen (pwd, dirsize_max) < dirsize_max)
return pwd;
free (pwd);
errno = ERANGE;
@@ -298,7 +298,7 @@ get_current_dir_name_or_unreachable (void)
sometimes a nicer name, and using it may avoid a fatal error if a
parent directory is searchable but not readable. */
if (pwd
- && (pwdlen = strlen (pwd)) < bufsize_max
+ && (pwdlen = strnlen (pwd, bufsize_max)) < bufsize_max
&& IS_DIRECTORY_SEP (pwd[pwdlen && IS_DEVICE_SEP (pwd[1]) ? 2 : 0])
&& stat (pwd, &pwdstat) == 0
&& stat (".", &dotstat) == 0
diff --git a/src/term.c b/src/term.c
index ce06e1cd4e5..8b5a710d80a 100644
--- a/src/term.c
+++ b/src/term.c
@@ -4400,7 +4400,7 @@ vfatal (const char *str, va_list ap)
{
fprintf (stderr, "emacs: ");
vfprintf (stderr, str, ap);
- if (!(strlen (str) > 0 && str[strlen (str) - 1] == '\n'))
+ if (! (str[0] && str[strlen (str) - 1] == '\n'))
fprintf (stderr, "\n");
exit (1);
}
diff --git a/src/xdisp.c b/src/xdisp.c
index 25e8932945e..9f63ef4b180 100644
--- a/src/xdisp.c
+++ b/src/xdisp.c
@@ -24341,9 +24341,7 @@ store_mode_line_string (const char *string, Lisp_Object lisp_string,
if (string != NULL)
{
- len = strlen (string);
- if (precision > 0 && len > precision)
- len = precision;
+ len = strnlen (string, precision <= 0 ? SIZE_MAX : precision);
lisp_string = make_string (string, len);
if (NILP (props))
props = mode_line_string_face_prop;