Module Name: src Committed By: christos Date: Tue Oct 8 19:17:57 UTC 2019
Modified Files: src/lib/libedit: filecomplete.c history.c readline.c Log Message: Change strncpy to either memcpy (when we know the len), or strlcpy (when we used to NUL terminate explicitly. To generate a diff of this commit: cvs rdiff -u -r1.58 -r1.59 src/lib/libedit/filecomplete.c cvs rdiff -u -r1.62 -r1.63 src/lib/libedit/history.c cvs rdiff -u -r1.157 -r1.158 src/lib/libedit/readline.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/lib/libedit/filecomplete.c diff -u src/lib/libedit/filecomplete.c:1.58 src/lib/libedit/filecomplete.c:1.59 --- src/lib/libedit/filecomplete.c:1.58 Sun Sep 8 01:50:58 2019 +++ src/lib/libedit/filecomplete.c Tue Oct 8 15:17:57 2019 @@ -1,4 +1,4 @@ -/* $NetBSD: filecomplete.c,v 1.58 2019/09/08 05:50:58 abhinav Exp $ */ +/* $NetBSD: filecomplete.c,v 1.59 2019/10/08 19:17:57 christos Exp $ */ /*- * Copyright (c) 1997 The NetBSD Foundation, Inc. @@ -31,7 +31,7 @@ #include "config.h" #if !defined(lint) && !defined(SCCSID) -__RCSID("$NetBSD: filecomplete.c,v 1.58 2019/09/08 05:50:58 abhinav Exp $"); +__RCSID("$NetBSD: filecomplete.c,v 1.59 2019/10/08 19:17:57 christos Exp $"); #endif /* not lint && not SCCSID */ #include <sys/types.h> @@ -86,8 +86,7 @@ fn_tilde_expand(const char *txt) temp = el_calloc(len, sizeof(*temp)); if (temp == NULL) return NULL; - (void)strncpy(temp, txt + 1, len - 2); - temp[len - 2] = '\0'; + (void)strlcpy(temp, txt + 1, len - 2); } if (temp[0] == 0) { #ifdef HAVE_GETPW_R_POSIX @@ -354,8 +353,7 @@ fn_filename_completion_function(const ch return NULL; } dirname = nptr; - (void)strncpy(dirname, text, len); - dirname[len] = '\0'; + (void)strlcpy(dirname, text, len); } else { el_free(filename); if (*text == 0) @@ -464,6 +462,7 @@ out: */ char ** completion_matches(const char *, char *(*)(const char *, int)); char ** +/*###467 [lint] completion_matches arg 1 declared inconsistently (pointer to const char != pointer to char) filecomplete.c(467) :: readline.c?(53)%%%*/ completion_matches(const char *text, char *(*genfunc)(const char *, int)) { char **match_list = NULL, *retstr, *prevstr; @@ -509,8 +508,7 @@ completion_matches(const char *text, cha el_free(match_list); return NULL; } - (void)strncpy(retstr, match_list[1], max_equal); - retstr[max_equal] = '\0'; + (void)strlcpy(retstr, match_list[1], max_equal); match_list[0] = retstr; /* add NULL as last pointer to the array */ Index: src/lib/libedit/history.c diff -u src/lib/libedit/history.c:1.62 src/lib/libedit/history.c:1.63 --- src/lib/libedit/history.c:1.62 Thu Sep 13 05:03:40 2018 +++ src/lib/libedit/history.c Tue Oct 8 15:17:57 2019 @@ -1,4 +1,4 @@ -/* $NetBSD: history.c,v 1.62 2018/09/13 09:03:40 kre Exp $ */ +/* $NetBSD: history.c,v 1.63 2019/10/08 19:17:57 christos Exp $ */ /*- * Copyright (c) 1992, 1993 @@ -37,7 +37,7 @@ #if 0 static char sccsid[] = "@(#)history.c 8.1 (Berkeley) 6/4/93"; #else -__RCSID("$NetBSD: history.c,v 1.62 2018/09/13 09:03:40 kre Exp $"); +__RCSID("$NetBSD: history.c,v 1.63 2019/10/08 19:17:57 christos Exp $"); #endif #endif /* not lint && not SCCSID */ @@ -411,21 +411,23 @@ static int history_def_add(void *p, TYPE(HistEvent) *ev, const Char *str) { history_t *h = (history_t *) p; - size_t len; + size_t len, elen, slen; Char *s; HistEventPrivate *evp = (void *)&h->cursor->ev; if (h->cursor == &h->list) return history_def_enter(p, ev, str); - len = Strlen(evp->str) + Strlen(str) + 1; + elen = Strlen(evp->str); + slen = Strlen(str); + len = elen + slen + 1; s = h_malloc(len * sizeof(*s)); if (s == NULL) { he_seterrev(ev, _HE_MALLOC_FAILED); return -1; } - (void) Strncpy(s, h->cursor->ev.str, len); + memcpy(s, evp->str, elen * sizeof(*s)); + memcpy(s + elen, str, slen * sizeof(*s)); s[len - 1] = '\0'; - (void) Strncat(s, str, len - Strlen(s) - 1); h_free(evp->str); evp->str = s; *ev = h->cursor->ev; Index: src/lib/libedit/readline.c diff -u src/lib/libedit/readline.c:1.157 src/lib/libedit/readline.c:1.158 --- src/lib/libedit/readline.c:1.157 Wed Aug 21 07:11:48 2019 +++ src/lib/libedit/readline.c Tue Oct 8 15:17:57 2019 @@ -1,4 +1,4 @@ -/* $NetBSD: readline.c,v 1.157 2019/08/21 11:11:48 christos Exp $ */ +/* $NetBSD: readline.c,v 1.158 2019/10/08 19:17:57 christos Exp $ */ /*- * Copyright (c) 1997 The NetBSD Foundation, Inc. @@ -31,7 +31,7 @@ #include "config.h" #if !defined(lint) && !defined(SCCSID) -__RCSID("$NetBSD: readline.c,v 1.157 2019/08/21 11:11:48 christos Exp $"); +__RCSID("$NetBSD: readline.c,v 1.158 2019/10/08 19:17:57 christos Exp $"); #endif /* not lint && not SCCSID */ #include <sys/types.h> @@ -521,7 +521,7 @@ _rl_compat_sub(const char *str, const ch s = str; while (*s) { if (*s == *what && !strncmp(s, what, what_len)) { - (void)strncpy(r, with, with_len); + memcpy(r, with, with_len); r += with_len; s += what_len; if (!globally) { @@ -607,8 +607,7 @@ get_history_event(const char *cmd, int * else { if ((pat = el_calloc(len + 1, sizeof(*pat))) == NULL) return NULL; - (void)strncpy(pat, cmd + begin, len); - pat[len] = '\0'; + (void)strlcpy(pat, cmd + begin, len); } if (history(h, &ev, H_CURR) != 0) { @@ -702,8 +701,7 @@ _history_expand_command(const char *comm if ((aptr = el_calloc(offs + 1, sizeof(*aptr))) == NULL) return -1; - (void)strncpy(aptr, command, offs); - aptr[offs] = '\0'; + strlcpy(aptr, command, offs); idx = 1; } else { int qchar; @@ -960,9 +958,8 @@ history_expand(char *str, char **output) } \ result = nresult; \ } \ - (void)strncpy(&result[idx], what, len); \ + (void)strlcpy(&result[idx], what, len); \ idx += len; \ - result[idx] = '\0'; \ } result = NULL; @@ -1150,8 +1147,7 @@ history_tokenize(const char *str) el_free(result); return NULL; } - (void)strncpy(temp, &str[start], len); - temp[len] = '\0'; + (void)strlcpy(temp, &str[start], len); result[idx++] = temp; result[idx] = NULL; if (str[i])