aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorPaul Eggert <[email protected]>2012-07-10 14:48:34 -0700
committerPaul Eggert <[email protected]>2012-07-10 14:48:34 -0700
commite99a530f8cdca3ccd9e739cd092ed9865d12fe89 (patch)
treec2bef9f80ff9910be17757a83f61caed02146d0a /src
parentc59592b32f5b5808c12720bfd37ea73b473fa1db (diff)
Simplify by avoiding confusing use of strncpy etc.
Diffstat (limited to 'src')
-rw-r--r--src/ChangeLog34
-rw-r--r--src/doc.c2
-rw-r--r--src/fileio.c10
-rw-r--r--src/frame.c4
-rw-r--r--src/gtkutil.c2
-rw-r--r--src/keyboard.c10
-rw-r--r--src/lread.c28
-rw-r--r--src/nsmenu.m6
-rw-r--r--src/nsterm.m27
-rw-r--r--src/process.c9
-rw-r--r--src/regex.c2
-rw-r--r--src/s/gnu-linux.h3
-rw-r--r--src/s/sol2-6.h3
-rw-r--r--src/s/unixware.h3
-rw-r--r--src/sysdep.c18
-rw-r--r--src/widget.c14
-rw-r--r--src/xdisp.c10
17 files changed, 91 insertions, 94 deletions
diff --git a/src/ChangeLog b/src/ChangeLog
index 84703fcdc2..af0fc3a8e3 100644
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,3 +1,37 @@
+2012-07-10 Paul Eggert <[email protected]>
+
+ Simplify by avoiding confusing use of strncpy etc.
+ * doc.c (Fsnarf_documentation):
+ * fileio.c (Ffile_name_directory, Fsubstitute_in_file_name):
+ * frame.c (Fmake_terminal_frame):
+ * gtkutil.c (get_utf8_string):
+ * lread.c (openp):
+ * nsmenu.m (ns_update_menubar):
+ * regex.c (regerror):
+ Prefer memcpy to strncpy and strncat when either will do.
+ * fileio.c (Fsubstitute_in_file_name):
+ * keyboard.c (MULTI_LETTER_MOD, parse_modifiers_uncached)
+ (menu_separator_name_p):
+ * nsmenu.m (ns_update_menubar):
+ Prefer memcmp to strncmp when either will do.
+ * nsterm.m: Include <ftoastr.h>.
+ (ns_get_color):
+ * s/gnu-linux.h, s/sol2-6.h, s/unixware.h (PTY_TTY_NAME_SPRINTF):
+ Prefer snprintf to strncpy.
+ * nsterm.m (ns_term_init):
+ * widget.c (set_frame_size) [0]: Prefer xstrdup to xmalloc + strncpy.
+ * nsterm.m (ns_term_init):
+ Avoid the need for strncpy, by using build_string or
+ make_unibyte_string directly. Use dtoastr, not snprintf.
+ * process.c (Fmake_network_process): Diagnose service names that
+ are too long, rather than silently truncating them or creating
+ non-null-terminated names.
+ (Fnetwork_interface_info): Likewise, for interface names.
+ * sysdep.c (system_process_attributes) [GNU_LINUX]:
+ Prefer sprintf to strncat.
+ * xdisp.c (debug_method_add) [GLYPH_DEBUG]:
+ Prefer vsnprintf to vsprintf + strncpy.
+
2012-07-10 Glenn Morris <[email protected]>
* dispnew.c (PENDING_OUTPUT_COUNT) [!__GNU_LIBRARY__]:
diff --git a/src/doc.c b/src/doc.c
index 7d82211fae..44363e30e8 100644
--- a/src/doc.c
+++ b/src/doc.c
@@ -645,7 +645,7 @@ the same file name is found in the `doc-directory'. */)
{
ptrdiff_t len = end - p - 2;
char *fromfile = alloca (len + 1);
- strncpy (fromfile, &p[2], len);
+ memcpy (fromfile, &p[2], len);
fromfile[len] = 0;
if (fromfile[len-1] == 'c')
fromfile[len-1] = 'o';
diff --git a/src/fileio.c b/src/fileio.c
index 5d4ee1bde0..eccb1fbb55 100644
--- a/src/fileio.c
+++ b/src/fileio.c
@@ -365,7 +365,7 @@ Given a Unix syntax file name, returns a string ending in slash. */)
if (p == beg + 4 && IS_DIRECTORY_SEP (*beg) && beg[1] == ':')
{
- strncpy (res, beg, 2);
+ memcpy (res, beg, 2);
beg += 2;
r += 2;
}
@@ -1648,7 +1648,7 @@ those `/' is discarded. */)
/* Copy out the variable name. */
target = alloca (s - o + 1);
- strncpy (target, o, s - o);
+ memcpy (target, o, s - o);
target[s - o] = 0;
#ifdef DOS_NT
strupr (target); /* $home == $HOME etc. */
@@ -1711,7 +1711,7 @@ those `/' is discarded. */)
/* Copy out the variable name. */
target = alloca (s - o + 1);
- strncpy (target, o, s - o);
+ memcpy (target, o, s - o);
target[s - o] = 0;
#ifdef DOS_NT
strupr (target); /* $home == $HOME etc. */
@@ -1732,13 +1732,13 @@ those `/' is discarded. */)
orig = make_unibyte_string (o, orig_length);
decoded = DECODE_FILE (orig);
decoded_length = SBYTES (decoded);
- strncpy (x, SSDATA (decoded), decoded_length);
+ memcpy (x, SDATA (decoded), decoded_length);
x += decoded_length;
/* If environment variable needed decoding, return value
needs to be multibyte. */
if (decoded_length != orig_length
- || strncmp (SSDATA (decoded), o, orig_length))
+ || memcmp (SDATA (decoded), o, orig_length))
multibyte = 1;
}
}
diff --git a/src/frame.c b/src/frame.c
index 3a3f552603..8d7981777b 100644
--- a/src/frame.c
+++ b/src/frame.c
@@ -646,7 +646,7 @@ affects all frames on the same terminal device. */)
if (!NILP (tty))
{
name = alloca (SBYTES (tty) + 1);
- strncpy (name, SSDATA (tty), SBYTES (tty));
+ memcpy (name, SSDATA (tty), SBYTES (tty));
name[SBYTES (tty)] = 0;
}
@@ -657,7 +657,7 @@ affects all frames on the same terminal device. */)
if (!NILP (tty_type))
{
type = alloca (SBYTES (tty_type) + 1);
- strncpy (type, SSDATA (tty_type), SBYTES (tty_type));
+ memcpy (type, SSDATA (tty_type), SBYTES (tty_type));
type[SBYTES (tty_type)] = 0;
}
diff --git a/src/gtkutil.c b/src/gtkutil.c
index f4551b4b9f..290ecef921 100644
--- a/src/gtkutil.c
+++ b/src/gtkutil.c
@@ -529,7 +529,7 @@ get_utf8_string (const char *str)
&bytes_written, &err))
&& err->code == G_CONVERT_ERROR_ILLEGAL_SEQUENCE)
{
- strncpy (up, (char *)p, bytes_written);
+ memcpy (up, p, bytes_written);
sprintf (up + bytes_written, "\\%03o", p[bytes_written]);
up += bytes_written+4;
p += bytes_written+1;
diff --git a/src/keyboard.c b/src/keyboard.c
index 71c8d869ec..b34d3c470a 100644
--- a/src/keyboard.c
+++ b/src/keyboard.c
@@ -6134,7 +6134,7 @@ parse_modifiers_uncached (Lisp_Object symbol, ptrdiff_t *modifier_end)
#define MULTI_LETTER_MOD(BIT, NAME, LEN) \
if (i + LEN + 1 <= SBYTES (name) \
- && ! strncmp (SSDATA (name) + i, NAME, LEN)) \
+ && ! memcmp (SDATA (name) + i, NAME, LEN)) \
{ \
this_mod_end = i + LEN; \
this_mod = BIT; \
@@ -6172,13 +6172,13 @@ parse_modifiers_uncached (Lisp_Object symbol, ptrdiff_t *modifier_end)
if (! (modifiers & (down_modifier | drag_modifier
| double_modifier | triple_modifier))
&& i + 7 == SBYTES (name)
- && strncmp (SSDATA (name) + i, "mouse-", 6) == 0
+ && memcmp (SDATA (name) + i, "mouse-", 6) == 0
&& ('0' <= SREF (name, i + 6) && SREF (name, i + 6) <= '9'))
modifiers |= click_modifier;
if (! (modifiers & (double_modifier | triple_modifier))
&& i + 6 < SBYTES (name)
- && strncmp (SSDATA (name) + i, "wheel-", 6) == 0)
+ && memcmp (SDATA (name) + i, "wheel-", 6) == 0)
modifiers |= click_modifier;
if (modifier_end)
@@ -6630,7 +6630,7 @@ parse_solitary_modifier (Lisp_Object symbol)
#define MULTI_LETTER_MOD(BIT, NAME, LEN) \
if (LEN == SBYTES (name) \
- && ! strncmp (SSDATA (name), NAME, LEN)) \
+ && ! memcmp (SDATA (name), NAME, LEN)) \
return BIT;
case 'A':
@@ -7418,7 +7418,7 @@ menu_separator_name_p (const char *label)
if (!label)
return 0;
else if (strlen (label) > 3
- && strncmp (label, "--", 2) == 0
+ && memcmp (label, "--", 2) == 0
&& label[2] != '-')
{
int i;
diff --git a/src/lread.c b/src/lread.c
index bd85e44093..640414b3e9 100644
--- a/src/lread.c
+++ b/src/lread.c
@@ -1495,26 +1495,14 @@ openp (Lisp_Object path, Lisp_Object str, Lisp_Object suffixes, Lisp_Object *sto
/* Concatenate path element/specified name with the suffix.
If the directory starts with /:, remove that. */
- if (SCHARS (filename) > 2
- && SREF (filename, 0) == '/'
- && SREF (filename, 1) == ':')
- {
- fnlen = SBYTES (filename) - 2;
- strncpy (fn, SSDATA (filename) + 2, fnlen);
- fn[fnlen] = '\0';
- }
- else
- {
- 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);
- fnlen += lsuffix;
- }
+ int prefixlen = ((SCHARS (filename) > 2
+ && SREF (filename, 0) == '/'
+ && SREF (filename, 1) == ':')
+ ? 2 : 0);
+ fnlen = SBYTES (filename) - prefixlen;
+ memcpy (fn, SDATA (filename) + prefixlen, fnlen);
+ memcpy (fn + fnlen, SDATA (XCAR (tail)), lsuffix + 1);
+ 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)
diff --git a/src/nsmenu.m b/src/nsmenu.m
index cccecffb42..2cd626e131 100644
--- a/src/nsmenu.m
+++ b/src/nsmenu.m
@@ -426,7 +426,8 @@ ns_update_menubar (struct frame *f, int deep_p, EmacsMenu *submenu)
break;
else
continue;
- if (strncmp (previous_strings[i], SDATA (string), 10))
+ if (memcmp (previous_strings[i], SDATA (string),
+ min (10, SBYTES (string) + 1)))
break;
}
@@ -447,7 +448,8 @@ ns_update_menubar (struct frame *f, int deep_p, EmacsMenu *submenu)
break;
if (n < 100)
- strncpy (previous_strings[i/4], SDATA (string), 10);
+ memcpy (previous_strings[i/4], min (10, SBYTES (string) + 1),
+ SDATA (string));
wv = xmalloc_widget_value ();
wv->name = SSDATA (string);
diff --git a/src/nsterm.m b/src/nsterm.m
index fde02f3ec9..0745efc35b 100644
--- a/src/nsterm.m
+++ b/src/nsterm.m
@@ -37,6 +37,7 @@ GNUstep port and post-20 update by Adrian Robert ([email protected])
#include <unistd.h>
#include <setjmp.h>
#include <c-strcase.h>
+#include <ftoastr.h>
#include "lisp.h"
#include "blockinput.h"
@@ -1442,21 +1443,16 @@ ns_get_color (const char *name, NSColor **col)
[scanner scanFloat: &b];
}
else if (!strncmp(name, "rgb:", 4)) /* A newer X11 format -- rgb:r/g/b */
- {
- strncpy (hex, name + 4, 19);
- hex[19] = '\0';
- scaling = (strlen(hex) - 2) / 3;
- }
+ scaling = (snprintf (hex, sizeof hex, "%s", name + 4) - 2) / 3;
else if (name[0] == '#') /* An old X11 format; convert to newer */
{
int len = (strlen(name) - 1);
int start = (len % 3 == 0) ? 1 : len / 4 + 1;
int i;
scaling = strlen(name+start) / 3;
- for (i=0; i<3; i++) {
- strncpy(hex + i * (scaling + 1), name + start + i * scaling, scaling);
- hex[(i+1) * (scaling + 1) - 1] = '/';
- }
+ for (i = 0; i < 3; i++)
+ snprintf (hex + i * (scaling + 1), "%.*s/", scaling,
+ name + start + i * scaling);
hex[3 * (scaling + 1) - 1] = '\0';
}
@@ -4107,10 +4103,7 @@ ns_term_init (Lisp_Object display_name)
ns_display_name_list);
dpyinfo->name_list_element = XCAR (ns_display_name_list);
- /* Set the name of the terminal. */
- terminal->name = xmalloc (SBYTES (display_name) + 1);
- strncpy (terminal->name, SDATA (display_name), SBYTES (display_name));
- terminal->name[SBYTES (display_name)] = 0;
+ terminal->name = xstrdup (SSDATA (display_name));
UNBLOCK_INPUT;
@@ -4167,14 +4160,14 @@ ns_term_init (Lisp_Object display_name)
}
{
- char c[128];
#ifdef NS_IMPL_GNUSTEP
- strncpy (c, gnustep_base_version, sizeof (c));
+ Vwindow_system_version = build_string (gnustep_base_version);
#else
/*PSnextrelease (128, c); */
- snprintf (c, sizeof (c), "%g", NSAppKitVersionNumber);
+ char c[DBL_BUFSIZE_BOUND];
+ int len = dtoastr (c, sizeof c, 0, 0, NSAppKitVersionNumber);
+ Vwindow_system_version = make_unibyte_string (c, len);
#endif
- Vwindow_system_version = build_string (c);
}
delete_keyboard_wait_descriptor (0);
diff --git a/src/process.c b/src/process.c
index b8c3a18b33..79100eb7a2 100644
--- a/src/process.c
+++ b/src/process.c
@@ -3013,7 +3013,9 @@ usage: (make-network-process &rest ARGS) */)
CHECK_STRING (service);
memset (&address_un, 0, sizeof address_un);
address_un.sun_family = AF_LOCAL;
- strncpy (address_un.sun_path, SSDATA (service), sizeof address_un.sun_path);
+ if (sizeof address_un.sun_path <= SBYTES (service))
+ error ("Service name too long");
+ strcpy (address_un.sun_path, SSDATA (service));
ai.ai_addr = (struct sockaddr *) &address_un;
ai.ai_addrlen = sizeof address_un;
goto open_socket;
@@ -3717,8 +3719,9 @@ FLAGS is the current flags of the interface. */)
CHECK_STRING (ifname);
- memset (rq.ifr_name, 0, sizeof rq.ifr_name);
- strncpy (rq.ifr_name, SSDATA (ifname), sizeof (rq.ifr_name));
+ if (sizeof rq.ifr_name <= SBYTES (ifname))
+ error ("interface name too long");
+ strcpy (rq.ifr_name, SSDATA (ifname));
s = socket (AF_INET, SOCK_STREAM, 0);
if (s < 0)
diff --git a/src/regex.c b/src/regex.c
index 751006d57b..4bf119402a 100644
--- a/src/regex.c
+++ b/src/regex.c
@@ -6644,7 +6644,7 @@ regerror (int err_code, const regex_t *preg, char *errbuf, size_t errbuf_size)
{
if (msg_size > errbuf_size)
{
- strncpy (errbuf, msg, errbuf_size - 1);
+ memcpy (errbuf, msg, errbuf_size - 1);
errbuf[errbuf_size - 1] = 0;
}
else
diff --git a/src/s/gnu-linux.h b/src/s/gnu-linux.h
index e3d43249d8..6f45ee00b7 100644
--- a/src/s/gnu-linux.h
+++ b/src/s/gnu-linux.h
@@ -63,8 +63,7 @@ along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
close (fd); \
return -1; \
} \
- strncpy (pty_name, ptyname, sizeof (pty_name)); \
- pty_name[sizeof (pty_name) - 1] = 0; \
+ snprintf (pty_name, sizeof pty_name, "%s", ptyname); \
sigunblock (sigmask (SIGCHLD)); \
}
diff --git a/src/s/sol2-6.h b/src/s/sol2-6.h
index bb7a9859b7..ec45748e03 100644
--- a/src/s/sol2-6.h
+++ b/src/s/sol2-6.h
@@ -54,8 +54,7 @@ along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
{ emacs_close (fd); return -1; } \
if (!(ptyname = ptsname (fd))) \
{ emacs_close (fd); return -1; } \
- strncpy (pty_name, ptyname, sizeof (pty_name)); \
- pty_name[sizeof (pty_name) - 1] = 0; \
+ snprintf (pty_name, sizeof pty_name, "%s", ptyname); \
}
#define GC_SETJMP_WORKS 1
diff --git a/src/s/unixware.h b/src/s/unixware.h
index e9ebb63f30..5bda987ffe 100644
--- a/src/s/unixware.h
+++ b/src/s/unixware.h
@@ -40,8 +40,7 @@ along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
fatal("could not unlock slave pty"); \
if (!(ptyname = ptsname(fd))) \
fatal ("could not enable slave pty"); \
- strncpy(pty_name, ptyname, sizeof(pty_name)); \
- pty_name[sizeof(pty_name) - 1] = 0; \
+ snprintf (pty_name, sizeof pty_name, "%s", ptyname); \
}
/* Conservative garbage collection has not been tested, so for now
diff --git a/src/sysdep.c b/src/sysdep.c
index 0639b72285..ed92676041 100644
--- a/src/sysdep.c
+++ b/src/sysdep.c
@@ -2744,9 +2744,11 @@ system_process_attributes (Lisp_Object pid)
char procbuf[1025], *p, *q;
int fd;
ssize_t nread;
- const char *cmd = NULL;
+ static char const default_cmd[] = "???";
+ const char *cmd = default_cmd;
+ int cmdsize = sizeof default_cmd - 1;
char *cmdline = NULL;
- ptrdiff_t cmdsize = 0, cmdline_size;
+ ptrdiff_t cmdline_size;
unsigned char c;
printmax_t proc_id;
int ppid, pgrp, sess, tty, tpgid, thcount;
@@ -2808,11 +2810,6 @@ system_process_attributes (Lisp_Object pid)
}
else
q = NULL;
- if (cmd == NULL)
- {
- cmd = "???";
- cmdsize = 3;
- }
/* Command name is encoded in locale-coding-system; decode it. */
cmd_str = make_unibyte_string (cmd, cmdsize);
decoded_cmd = code_convert_string_norecord (cmd_str,
@@ -2950,14 +2947,9 @@ system_process_attributes (Lisp_Object pid)
}
if (!cmdline_size)
{
- if (!cmd)
- cmd = "???";
- if (!cmdsize)
- cmdsize = strlen (cmd);
cmdline_size = cmdsize + 2;
cmdline = xmalloc (cmdline_size + 1);
- strcpy (cmdline, "[");
- strcat (strncat (cmdline, cmd, cmdsize), "]");
+ sprintf (cmdline, "[%.*s]", cmdsize, cmd);
}
emacs_close (fd);
/* Command line is encoded in locale-coding-system; decode it. */
diff --git a/src/widget.c b/src/widget.c
index e219c200e0..b94c30f4e9 100644
--- a/src/widget.c
+++ b/src/widget.c
@@ -429,25 +429,15 @@ set_frame_size (EmacsFrame ew)
{
/* the tricky things with the sign is to make sure that
-0 is printed -0. */
- int len;
- char *tem;
sprintf (shell_position, "=%c%d%c%d",
flags & XNegative ? '-' : '+', x < 0 ? -x : x,
flags & YNegative ? '-' : '+', y < 0 ? -y : y);
- len = strlen (shell_position) + 1;
- tem = xmalloc (len);
- strncpy (tem, shell_position, len);
- XtVaSetValues (wmshell, XtNgeometry, tem, NULL);
+ XtVaSetValues (wmshell, XtNgeometry, xstrdup (shell_position), NULL);
}
else if (flags & (WidthValue | HeightValue))
{
- int len;
- char *tem;
sprintf (shell_position, "=%dx%d", pixel_width, pixel_height);
- len = strlen (shell_position) + 1;
- tem = xmalloc (len);
- strncpy (tem, shell_position, len);
- XtVaSetValues (wmshell, XtNgeometry, tem, NULL);
+ XtVaSetValues (wmshell, XtNgeometry, xstrdup (shell_position), NULL);
}
/* If the geometry spec we're using has W/H components, mark the size
diff --git a/src/xdisp.c b/src/xdisp.c
index 1732e3dfe5..4c9f3fda0a 100644
--- a/src/xdisp.c
+++ b/src/xdisp.c
@@ -12492,23 +12492,21 @@ static void debug_method_add (struct window *, char const *, ...)
static void
debug_method_add (struct window *w, char const *fmt, ...)
{
- char buffer[512];
char *method = w->desired_matrix->method;
int len = strlen (method);
int size = sizeof w->desired_matrix->method;
int remaining = size - len - 1;
va_list ap;
- va_start (ap, fmt);
- vsprintf (buffer, fmt, ap);
- va_end (ap);
if (len && remaining)
{
method[len] = '|';
--remaining, ++len;
}
- strncpy (method + len, buffer, remaining);
+ va_start (ap, fmt);
+ vsnprintf (method + len, remaining + 1, fmt, ap);
+ va_end (ap);
if (trace_redisplay_p)
fprintf (stderr, "%p (%s): %s\n",
@@ -12517,7 +12515,7 @@ debug_method_add (struct window *w, char const *fmt, ...)
&& STRINGP (BVAR (XBUFFER (w->buffer), name)))
? SSDATA (BVAR (XBUFFER (w->buffer), name))
: "no buffer"),
- buffer);
+ method + len);
}
#endif /* GLYPH_DEBUG */