From e99a530f8cdca3ccd9e739cd092ed9865d12fe89 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Tue, 10 Jul 2012 14:48:34 -0700 Subject: [PATCH] Simplify by avoiding confusing use of strncpy etc. --- lib-src/ChangeLog | 19 +++++++++++ lib-src/etags.c | 56 ++++++++++++++------------------ lib-src/make-docfile.c | 2 +- lib-src/movemail.c | 45 ++++++++------------------ lib-src/pop.c | 73 +++++++++++++----------------------------- src/ChangeLog | 34 ++++++++++++++++++++ src/doc.c | 2 +- src/fileio.c | 10 +++--- src/frame.c | 4 +-- src/gtkutil.c | 2 +- src/keyboard.c | 10 +++--- src/lread.c | 28 +++++----------- src/nsmenu.m | 6 ++-- src/nsterm.m | 27 ++++++---------- src/process.c | 9 ++++-- src/regex.c | 2 +- src/s/gnu-linux.h | 3 +- src/s/sol2-6.h | 3 +- src/s/unixware.h | 3 +- src/sysdep.c | 18 +++-------- src/widget.c | 14 ++------ src/xdisp.c | 10 +++--- 22 files changed, 170 insertions(+), 210 deletions(-) diff --git a/lib-src/ChangeLog b/lib-src/ChangeLog index bc7c5d7114..449985966a 100644 --- a/lib-src/ChangeLog +++ b/lib-src/ChangeLog @@ -1,3 +1,22 @@ +2012-07-10 Paul Eggert + + Simplify by avoiding confusing use of strncpy etc. + * etags.c (write_classname, C_entries): + Use sprintf rather than strncpy or strncat. + * etags.c (consider_token, C_entries, HTML_labels, Prolog_functions) + (Erlang_functions, substitute, readline_internal, savenstr): + * movemail.c (mail_spool_name): + Use memcpy rather than strncpy or strncat when either will do. + * make-docfile.c (write_c_args): + Use memcmp rather than strncmp when either will do. + * movemail.c (pop_retr): + * pop.c (pop_stat, pop_list, pop_multi_first, pop_last) + (socket_connection, pop_getline, sendline, getok): + Use snprintf rather than strncpy or strncat. + * movemail.c (concat): Remove; no longer needed. + (xmalloc): Define only if needed, now that concat has gone away. + Return void *. All uses changed. + 2012-07-09 Paul Eggert Add GCC-style 'const' attribute to functions that can use it. diff --git a/lib-src/etags.c b/lib-src/etags.c index 7141811239..69200b790f 100644 --- a/lib-src/etags.c +++ b/lib-src/etags.c @@ -2642,17 +2642,11 @@ write_classname (linebuffer *cn, const char *qualifier) } for (i = 1; i < cstack.nl; i++) { - char *s; - int slen; - - s = cstack.cname[i]; + char *s = cstack.cname[i]; if (s == NULL) continue; - slen = strlen (s); - len += slen + qlen; - linebuffer_setlen (cn, len); - strncat (cn->buffer, qualifier, qlen); - strncat (cn->buffer, s, slen); + linebuffer_setlen (cn, len + qlen + strlen (s)); + len += sprintf (cn->buffer + len, "%s%s", qualifier, s); } } @@ -2867,7 +2861,7 @@ consider_token (register char *str, register int len, register int c, int *c_ext fvdef = fvnone; objdef = omethodtag; linebuffer_setlen (&token_name, len); - strncpy (token_name.buffer, str, len); + memcpy (token_name.buffer, str, len); token_name.buffer[len] = '\0'; return TRUE; } @@ -2879,10 +2873,11 @@ consider_token (register char *str, register int len, register int c, int *c_ext case omethodparm: if (parlev == 0) { + int oldlen = token_name.len; fvdef = fvnone; objdef = omethodtag; - linebuffer_setlen (&token_name, token_name.len + len); - strncat (token_name.buffer, str, len); + linebuffer_setlen (&token_name, oldlen + len); + memcpy (token_name.buffer + oldlen, str, len); return TRUE; } return FALSE; @@ -3311,12 +3306,12 @@ C_entries (int c_ext, FILE *inf) && nestlev > 0 && definedef == dnone) /* in struct body */ { + int len; write_classname (&token_name, qualifier); - linebuffer_setlen (&token_name, - token_name.len+qlen+toklen); - strcat (token_name.buffer, qualifier); - strncat (token_name.buffer, - newlb.buffer + tokoff, toklen); + len = token_name.len; + linebuffer_setlen (&token_name, len+qlen+toklen); + sprintf (token_name.buffer + len, "%s%.*s", + qualifier, toklen, newlb.buffer + tokoff); token.named = TRUE; } else if (objdef == ocatseen) @@ -3324,11 +3319,8 @@ C_entries (int c_ext, FILE *inf) { int len = strlen (objtag) + 2 + toklen; linebuffer_setlen (&token_name, len); - strcpy (token_name.buffer, objtag); - strcat (token_name.buffer, "("); - strncat (token_name.buffer, - newlb.buffer + tokoff, toklen); - strcat (token_name.buffer, ")"); + sprintf (token_name.buffer, "%s(%.*s)", + objtag, toklen, newlb.buffer + tokoff); token.named = TRUE; } else if (objdef == omethodtag @@ -3352,8 +3344,8 @@ C_entries (int c_ext, FILE *inf) len -= 1; } linebuffer_setlen (&token_name, len); - strncpy (token_name.buffer, - newlb.buffer + off, len); + memcpy (token_name.buffer, + newlb.buffer + off, len); token_name.buffer[len] = '\0'; if (defun) while (--len >= 0) @@ -3364,8 +3356,8 @@ C_entries (int c_ext, FILE *inf) else { linebuffer_setlen (&token_name, toklen); - strncpy (token_name.buffer, - newlb.buffer + tokoff, toklen); + memcpy (token_name.buffer, + newlb.buffer + tokoff, toklen); token_name.buffer[toklen] = '\0'; /* Name macros and members. */ token.named = (structdef == stagseen @@ -5161,7 +5153,7 @@ HTML_labels (FILE *inf) for (end = dbp; *end != '\0' && intoken (*end); end++) continue; linebuffer_setlen (&token_name, end - dbp); - strncpy (token_name.buffer, dbp, end - dbp); + memcpy (token_name.buffer, dbp, end - dbp); token_name.buffer[end - dbp] = '\0'; dbp = end; @@ -5261,7 +5253,7 @@ Prolog_functions (FILE *inf) else if (len + 1 > allocated) xrnew (last, len + 1, char); allocated = len + 1; - strncpy (last, cp, len); + memcpy (last, cp, len); last[len] = '\0'; } } @@ -5434,7 +5426,7 @@ Erlang_functions (FILE *inf) else if (len + 1 > allocated) xrnew (last, len + 1, char); allocated = len + 1; - strncpy (last, cp, len); + memcpy (last, cp, len); last[len] = '\0'; } } @@ -5817,7 +5809,7 @@ substitute (char *in, char *out, struct re_registers *regs) { dig = *out - '0'; diglen = regs->end[dig] - regs->start[dig]; - strncpy (t, in + regs->start[dig], diglen); + memcpy (t, in + regs->start[dig], diglen); t += diglen; } else @@ -6040,7 +6032,7 @@ readline_internal (linebuffer *lbp, register FILE *stream) filebuf.size *= 2; xrnew (filebuf.buffer, filebuf.size, char); } - strncpy (filebuf.buffer + filebuf.len, lbp->buffer, lbp->len); + memcpy (filebuf.buffer + filebuf.len, lbp->buffer, lbp->len); filebuf.len += lbp->len; filebuf.buffer[filebuf.len++] = '\n'; filebuf.buffer[filebuf.len] = '\0'; @@ -6263,7 +6255,7 @@ savenstr (const char *cp, int len) register char *dp; dp = xnew (len + 1, char); - strncpy (dp, cp, len); + memcpy (dp, cp, len); dp[len] = '\0'; return dp; } diff --git a/lib-src/make-docfile.c b/lib-src/make-docfile.c index 99c43da97f..bd87b5b652 100644 --- a/lib-src/make-docfile.c +++ b/lib-src/make-docfile.c @@ -541,7 +541,7 @@ write_c_args (FILE *out, char *func, char *buf, int minargs, int maxargs) /* In C code, `default' is a reserved word, so we spell it `defalt'; demangle that here. */ - if (ident_length == 6 && strncmp (ident_start, "defalt", 6) == 0) + if (ident_length == 6 && memcmp (ident_start, "defalt", 6) == 0) fprintf (out, "DEFAULT"); else while (ident_length-- > 0) diff --git a/lib-src/movemail.c b/lib-src/movemail.c index 3d994ec5a5..b9a1be8a7f 100644 --- a/lib-src/movemail.c +++ b/lib-src/movemail.c @@ -141,8 +141,9 @@ static _Noreturn void fatal (const char *s1, const char *s2, const char *s3); static void error (const char *s1, const char *s2, const char *s3); static _Noreturn void pfatal_with_name (char *name); static _Noreturn void pfatal_and_delete (char *name); -static char *concat (const char *s1, const char *s2, const char *s3); -static long *xmalloc (unsigned int size); +#ifdef MAIL_USE_MAILLOCK +static void *xmalloc (size_t size); +#endif #ifdef MAIL_USE_POP static int popmail (char *mailbox, char *outfile, int preserve, char *password, int reverse_order); static int pop_retr (popserver server, int msgno, FILE *arg); @@ -301,7 +302,7 @@ main (int argc, char **argv) inname_dirlen && !IS_DIRECTORY_SEP (inname[inname_dirlen - 1]); inname_dirlen--) continue; - tempname = (char *) xmalloc (inname_dirlen + sizeof "EXXXXXX"); + tempname = xmalloc (inname_dirlen + sizeof "EXXXXXX"); while (1) { @@ -583,8 +584,8 @@ mail_spool_name (char *inname) if (stat (MAILDIR, &stat1) < 0) return NULL; - indir = (char *) xmalloc (fname - inname + 1); - strncpy (indir, inname, fname - inname); + indir = xmalloc (fname - inname + 1); + memcpy (indir, inname, fname - inname); indir[fname-inname] = '\0'; @@ -644,32 +645,18 @@ pfatal_and_delete (char *name) fatal ("%s for %s", s, name); } -/* Return a newly-allocated string whose contents concatenate those of s1, s2, s3. */ - -static char * -concat (const char *s1, const char *s2, const char *s3) -{ - size_t len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3); - char *result = (char *) xmalloc (len1 + len2 + len3 + 1); - - strcpy (result, s1); - strcpy (result + len1, s2); - strcpy (result + len1 + len2, s3); - *(result + len1 + len2 + len3) = 0; - - return result; -} - +#ifdef MAIL_USE_MAILLOCK /* Like malloc but get fatal error if memory is exhausted. */ -static long * -xmalloc (unsigned int size) +static void * +xmalloc (size_t size) { - long *result = (long *) malloc (size); + void *result = malloc (size); if (!result) fatal ("virtual memory exhausted", 0, 0); return result; } +#endif /* This is the guts of the interface to the Post Office Protocol. */ @@ -851,10 +838,7 @@ pop_retr (popserver server, int msgno, FILE *arg) if (pop_retrieve_first (server, msgno, &line)) { - char *msg = concat ("Error from POP server: ", pop_error, ""); - strncpy (Errmsg, msg, sizeof (Errmsg)); - Errmsg[sizeof (Errmsg)-1] = '\0'; - free (msg); + snprintf (Errmsg, sizeof Errmsg, "Error from POP server: %s", pop_error); return (NOTOK); } @@ -873,10 +857,7 @@ pop_retr (popserver server, int msgno, FILE *arg) if (ret) { - char *msg = concat ("Error from POP server: ", pop_error, ""); - strncpy (Errmsg, msg, sizeof (Errmsg)); - Errmsg[sizeof (Errmsg)-1] = '\0'; - free (msg); + snprintf (Errmsg, sizeof Errmsg, "Error from POP server: %s", pop_error); return (NOTOK); } diff --git a/lib-src/pop.c b/lib-src/pop.c index b8ed65c3ea..74054e0e1b 100644 --- a/lib-src/pop.c +++ b/lib-src/pop.c @@ -340,10 +340,7 @@ pop_stat (popserver server, int *count, int *size) if (strncmp (fromserver, "+OK ", 4)) { if (0 == strncmp (fromserver, "-ERR", 4)) - { - strncpy (pop_error, fromserver, ERROR_MAX); - pop_error[ERROR_MAX-1] = '\0'; - } + snprintf (pop_error, ERROR_MAX, "%s", fromserver); else { strcpy (pop_error, @@ -444,10 +441,7 @@ pop_list (popserver server, int message, int **IDs, int **sizes) if (strncmp (fromserver, "+OK ", 4)) { if (! strncmp (fromserver, "-ERR", 4)) - { - strncpy (pop_error, fromserver, ERROR_MAX); - pop_error[ERROR_MAX-1] = '\0'; - } + snprintf (pop_error, ERROR_MAX, "%s", fromserver); else { strcpy (pop_error, @@ -686,8 +680,7 @@ pop_multi_first (popserver server, const char *command, char **response) if (0 == strncmp (*response, "-ERR", 4)) { - strncpy (pop_error, *response, ERROR_MAX); - pop_error[ERROR_MAX-1] = '\0'; + snprintf (pop_error, ERROR_MAX, "%s", *response); return (-1); } else if (0 == strncmp (*response, "+OK", 3)) @@ -860,8 +853,7 @@ pop_last (popserver server) if (! strncmp (fromserver, "-ERR", 4)) { - strncpy (pop_error, fromserver, ERROR_MAX); - pop_error[ERROR_MAX-1] = '\0'; + snprintf (pop_error, ERROR_MAX, "%s", fromserver); return (-1); } else if (strncmp (fromserver, "+OK ", 4)) @@ -1061,9 +1053,8 @@ socket_connection (char *host, int flags) sock = socket (PF_INET, SOCK_STREAM, 0); if (sock < 0) { - strcpy (pop_error, POP_SOCKET_ERROR); - strncat (pop_error, strerror (errno), - ERROR_MAX - sizeof (POP_SOCKET_ERROR)); + snprintf (pop_error, ERROR_MAX, "%s%s", + POP_SOCKET_ERROR, strerror (errno)); return (-1); } @@ -1139,9 +1130,7 @@ socket_connection (char *host, int flags) if (! connect_ok) { CLOSESOCKET (sock); - strcpy (pop_error, CONNECT_ERROR); - strncat (pop_error, strerror (errno), - ERROR_MAX - sizeof (CONNECT_ERROR)); + snprintf (pop_error, ERROR_MAX, "%s%s", CONNECT_ERROR, strerror (errno)); return (-1); } @@ -1159,9 +1148,8 @@ socket_connection (char *host, int flags) krb5_auth_con_free (kcontext, auth_context); if (kcontext) krb5_free_context (kcontext); - strcpy (pop_error, KRB_ERROR); - strncat (pop_error, error_message (rem), - ERROR_MAX - sizeof (KRB_ERROR)); + snprintf (pop_error, ERROR_MAX, "%s%s", + KRB_ERROR, error_message (rem)); CLOSESOCKET (sock); return (-1); } @@ -1199,30 +1187,19 @@ socket_connection (char *host, int flags) krb5_free_principal (kcontext, server); if (rem) { - strcpy (pop_error, KRB_ERROR); - strncat (pop_error, error_message (rem), - ERROR_MAX - sizeof (KRB_ERROR)); + int pop_error_len = snprintf (pop_error, ERROR_MAX, "%s%s", + KRB_ERROR, error_message (rem)); #if defined HAVE_KRB5_ERROR_TEXT if (err_ret && err_ret->text.length) { - strncat (pop_error, " [server says '", - ERROR_MAX - strlen (pop_error) - 1); - strncat (pop_error, err_ret->text.data, - min (ERROR_MAX - strlen (pop_error) - 1, - err_ret->text.length)); - strncat (pop_error, "']", - ERROR_MAX - strlen (pop_error) - 1); + int errlen = err_ret->text.length; + snprintf (pop_error + pop_error_len, ERROR_MAX - pop_error_len, + " [server says '.*%s']", errlen, err_ret->text.data); } #elif defined HAVE_KRB5_ERROR_E_TEXT - if (err_ret && err_ret->e_text && strlen (*err_ret->e_text)) - { - strncat (pop_error, " [server says '", - ERROR_MAX - strlen (pop_error) - 1); - strncat (pop_error, *err_ret->e_text, - ERROR_MAX - strlen (pop_error) - 1); - strncat (pop_error, "']", - ERROR_MAX - strlen (pop_error) - 1); - } + if (err_ret && err_ret->e_text && **err_ret->e_text) + snprintf (pop_error + pop_error_len, ERRMAX - pop_error_len, + " [server says '%s']", *err_ret->e_text); #endif if (err_ret) krb5_free_error (kcontext, err_ret); @@ -1243,9 +1220,7 @@ socket_connection (char *host, int flags) free ((char *) ticket); if (rem != KSUCCESS) { - strcpy (pop_error, KRB_ERROR); - strncat (pop_error, krb_err_txt[rem], - ERROR_MAX - sizeof (KRB_ERROR)); + snprintf (pop_error, ERROR_MAX, "%s%s", KRB_ERROR, krb_err_txt[rem]); CLOSESOCKET (sock); return (-1); } @@ -1350,9 +1325,8 @@ pop_getline (popserver server, char **line) server->buffer_size - server->data - 1, 0); if (ret < 0) { - strcpy (pop_error, GETLINE_ERROR); - strncat (pop_error, strerror (errno), - ERROR_MAX - sizeof (GETLINE_ERROR)); + snprintf (pop_error, ERROR_MAX, "%s%s", + GETLINE_ERROR, strerror (errno)); pop_trash (server); return (-1); } @@ -1436,9 +1410,7 @@ sendline (popserver server, const char *line) if (ret < 0) { pop_trash (server); - strcpy (pop_error, SENDLINE_ERROR); - strncat (pop_error, strerror (errno), - ERROR_MAX - sizeof (SENDLINE_ERROR)); + snprintf (pop_error, ERROR_MAX, "%s%s", SENDLINE_ERROR, strerror (errno)); return (ret); } @@ -1500,8 +1472,7 @@ getok (popserver server) return (0); else if (! strncmp (fromline, "-ERR", 4)) { - strncpy (pop_error, fromline, ERROR_MAX); - pop_error[ERROR_MAX-1] = '\0'; + snprintf (pop_error, ERROR_MAX, "%s", fromline); return (-1); } else 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 + + 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 . + (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 * 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 (arobert@cogsci.ucsd.edu) #include #include #include +#include #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 . */ 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 . */ { 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 . */ 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 */ -- 2.20.1