Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/lib/libedit Change strncpy to either memcpy (when we know th...



details:   https://anonhg.NetBSD.org/src/rev/8d15287515b1
branches:  trunk
changeset: 455130:8d15287515b1
user:      christos <christos%NetBSD.org@localhost>
date:      Tue Oct 08 19:17:57 2019 +0000

description:
Change strncpy to either memcpy (when we know the len), or strlcpy (when
we used to NUL terminate explicitly.

diffstat:

 lib/libedit/filecomplete.c |  14 ++++++--------
 lib/libedit/history.c      |  14 ++++++++------
 lib/libedit/readline.c     |  18 +++++++-----------
 3 files changed, 21 insertions(+), 25 deletions(-)

diffs (170 lines):

diff -r 50e7c93a41ce -r 8d15287515b1 lib/libedit/filecomplete.c
--- a/lib/libedit/filecomplete.c        Tue Oct 08 18:50:44 2019 +0000
+++ b/lib/libedit/filecomplete.c        Tue Oct 08 19:17:57 2019 +0000
@@ -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 @@
                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 @@
                                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 @@
  */
 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 @@
                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 */
diff -r 50e7c93a41ce -r 8d15287515b1 lib/libedit/history.c
--- a/lib/libedit/history.c     Tue Oct 08 18:50:44 2019 +0000
+++ b/lib/libedit/history.c     Tue Oct 08 19:17:57 2019 +0000
@@ -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 @@
 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;
diff -r 50e7c93a41ce -r 8d15287515b1 lib/libedit/readline.c
--- a/lib/libedit/readline.c    Tue Oct 08 18:50:44 2019 +0000
+++ b/lib/libedit/readline.c    Tue Oct 08 19:17:57 2019 +0000
@@ -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 @@
        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 @@
        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 @@
                        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 @@
                        }                                               \
                        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 @@
                        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])



Home | Main Index | Thread Index | Old Index