Source-Changes-HG archive

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

[src/trunk]: src/lib/libedit improvements in malloc/free handling.



details:   https://anonhg.NetBSD.org/src/rev/11b3166da91e
branches:  trunk
changeset: 372204:11b3166da91e
user:      christos <christos%NetBSD.org@localhost>
date:      Sun Oct 30 19:11:31 2022 +0000

description:
improvements in malloc/free handling.

diffstat:

 lib/libedit/chared.c       |  11 +++++++----
 lib/libedit/chartype.c     |   6 ++++--
 lib/libedit/el.c           |   6 +++---
 lib/libedit/filecomplete.c |   6 ++++--
 lib/libedit/map.c          |  13 ++++++++-----
 lib/libedit/read.c         |  25 ++++++++++++++-----------
 lib/libedit/read.h         |   4 ++--
 lib/libedit/readline.c     |   8 +++++---
 lib/libedit/terminal.c     |  29 +++++++++--------------------
 9 files changed, 56 insertions(+), 52 deletions(-)

diffs (truncated from 356 to 300 lines):

diff -r dabe1b282aac -r 11b3166da91e lib/libedit/chared.c
--- a/lib/libedit/chared.c      Sun Oct 30 15:08:50 2022 +0000
+++ b/lib/libedit/chared.c      Sun Oct 30 19:11:31 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: chared.c,v 1.62 2022/02/08 21:13:22 rillig Exp $       */
+/*     $NetBSD: chared.c,v 1.63 2022/10/30 19:11:31 christos Exp $     */
 
 /*-
  * Copyright (c) 1992, 1993
@@ -37,7 +37,7 @@
 #if 0
 static char sccsid[] = "@(#)chared.c   8.1 (Berkeley) 6/4/93";
 #else
-__RCSID("$NetBSD: chared.c,v 1.62 2022/02/08 21:13:22 rillig Exp $");
+__RCSID("$NetBSD: chared.c,v 1.63 2022/10/30 19:11:31 christos Exp $");
 #endif
 #endif /* not lint && not SCCSID */
 
@@ -414,7 +414,7 @@
        el->el_chared.c_redo.buf        = el_calloc(EL_BUFSIZ,
            sizeof(*el->el_chared.c_redo.buf));
        if (el->el_chared.c_redo.buf == NULL)
-               return -1;
+               goto out;
        el->el_chared.c_redo.pos        = el->el_chared.c_redo.buf;
        el->el_chared.c_redo.lim        = el->el_chared.c_redo.buf + EL_BUFSIZ;
        el->el_chared.c_redo.cmd        = ED_UNASSIGNED;
@@ -425,7 +425,7 @@
        el->el_chared.c_kill.buf        = el_calloc(EL_BUFSIZ,
            sizeof(*el->el_chared.c_kill.buf));
        if (el->el_chared.c_kill.buf == NULL)
-               return -1;
+               goto out;
        el->el_chared.c_kill.mark       = el->el_line.buffer;
        el->el_chared.c_kill.last       = el->el_chared.c_kill.buf;
        el->el_chared.c_resizefun       = NULL;
@@ -442,6 +442,9 @@
        el->el_state.lastcmd            = ED_UNASSIGNED;
 
        return 0;
+out:
+       ch_end(el);
+       return -1;
 }
 
 /* ch_reset():
diff -r dabe1b282aac -r 11b3166da91e lib/libedit/chartype.c
--- a/lib/libedit/chartype.c    Sun Oct 30 15:08:50 2022 +0000
+++ b/lib/libedit/chartype.c    Sun Oct 30 19:11:31 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: chartype.c,v 1.35 2019/07/23 10:18:52 christos Exp $   */
+/*     $NetBSD: chartype.c,v 1.36 2022/10/30 19:11:31 christos Exp $   */
 
 /*-
  * Copyright (c) 2009 The NetBSD Foundation, Inc.
@@ -31,7 +31,7 @@
  */
 #include "config.h"
 #if !defined(lint) && !defined(SCCSID)
-__RCSID("$NetBSD: chartype.c,v 1.35 2019/07/23 10:18:52 christos Exp $");
+__RCSID("$NetBSD: chartype.c,v 1.36 2022/10/30 19:11:31 christos Exp $");
 #endif /* not lint && not SCCSID */
 
 #include <ctype.h>
@@ -158,6 +158,8 @@
                        return NULL;
 
        wargv = el_calloc((size_t)(argc + 1), sizeof(*wargv));
+       if (wargv == NULL)
+               return NULL;
 
        for (i = 0, p = conv->wbuff; i < argc; ++i) {
                if (!argv[i]) {   /* don't pass null pointers to mbstowcs */
diff -r dabe1b282aac -r 11b3166da91e lib/libedit/el.c
--- a/lib/libedit/el.c  Sun Oct 30 15:08:50 2022 +0000
+++ b/lib/libedit/el.c  Sun Oct 30 19:11:31 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: el.c,v 1.100 2021/08/15 10:08:41 christos Exp $        */
+/*     $NetBSD: el.c,v 1.101 2022/10/30 19:11:31 christos Exp $        */
 
 /*-
  * Copyright (c) 1992, 1993
@@ -37,7 +37,7 @@
 #if 0
 static char sccsid[] = "@(#)el.c       8.2 (Berkeley) 1/3/94";
 #else
-__RCSID("$NetBSD: el.c,v 1.100 2021/08/15 10:08:41 christos Exp $");
+__RCSID("$NetBSD: el.c,v 1.101 2022/10/30 19:11:31 christos Exp $");
 #endif
 #endif /* not lint && not SCCSID */
 
@@ -142,7 +142,7 @@
        if (!(el->el_flags & NO_TTY))
                tty_end(el, TCSAFLUSH);
        ch_end(el);
-       read_end(el->el_read);
+       read_end(el);
        search_end(el);
        hist_end(el);
        prompt_end(el);
diff -r dabe1b282aac -r 11b3166da91e lib/libedit/filecomplete.c
--- a/lib/libedit/filecomplete.c        Sun Oct 30 15:08:50 2022 +0000
+++ b/lib/libedit/filecomplete.c        Sun Oct 30 19:11:31 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: filecomplete.c,v 1.70 2022/03/12 15:29:17 christos Exp $       */
+/*     $NetBSD: filecomplete.c,v 1.71 2022/10/30 19:11:31 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.70 2022/03/12 15:29:17 christos Exp $");
+__RCSID("$NetBSD: filecomplete.c,v 1.71 2022/10/30 19:11:31 christos Exp $");
 #endif /* not lint && not SCCSID */
 
 #include <sys/types.h>
@@ -637,6 +637,8 @@
                return unescaped_word;
        }
        temp = el_malloc((len + 1) * sizeof(*temp));
+       if (temp == NULL)
+               return NULL;
        (void) wcsncpy(temp, ctemp, len);
        temp[len] = '\0';
        return temp;
diff -r dabe1b282aac -r 11b3166da91e lib/libedit/map.c
--- a/lib/libedit/map.c Sun Oct 30 15:08:50 2022 +0000
+++ b/lib/libedit/map.c Sun Oct 30 19:11:31 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: map.c,v 1.54 2021/08/29 09:41:59 christos Exp $        */
+/*     $NetBSD: map.c,v 1.55 2022/10/30 19:11:31 christos Exp $        */
 
 /*-
  * Copyright (c) 1992, 1993
@@ -37,7 +37,7 @@
 #if 0
 static char sccsid[] = "@(#)map.c      8.1 (Berkeley) 6/4/93";
 #else
-__RCSID("$NetBSD: map.c,v 1.54 2021/08/29 09:41:59 christos Exp $");
+__RCSID("$NetBSD: map.c,v 1.55 2022/10/30 19:11:31 christos Exp $");
 #endif
 #endif /* not lint && not SCCSID */
 
@@ -918,18 +918,18 @@
                return -1;
        el->el_map.key = el_calloc(N_KEYS, sizeof(*el->el_map.key));
        if (el->el_map.key == NULL)
-               return -1;
+               goto out;
        el->el_map.emacs = el_map_emacs;
        el->el_map.vic = el_map_vi_command;
        el->el_map.vii = el_map_vi_insert;
        el->el_map.help = el_calloc(EL_NUM_FCNS, sizeof(*el->el_map.help));
        if (el->el_map.help == NULL)
-               return -1;
+               goto out;
        (void) memcpy(el->el_map.help, el_func_help,
            sizeof(*el->el_map.help) * EL_NUM_FCNS);
        el->el_map.func = el_calloc(EL_NUM_FCNS, sizeof(*el->el_map.func));
        if (el->el_map.func == NULL)
-               return -1;
+               goto out;
        memcpy(el->el_map.func, el_func, sizeof(*el->el_map.func)
            * EL_NUM_FCNS);
        el->el_map.nfunc = EL_NUM_FCNS;
@@ -940,6 +940,9 @@
        map_init_emacs(el);
 #endif /* VIDEFAULT */
        return 0;
+out:
+       map_end(el);
+       return -1;
 }
 
 
diff -r dabe1b282aac -r 11b3166da91e lib/libedit/read.c
--- a/lib/libedit/read.c        Sun Oct 30 15:08:50 2022 +0000
+++ b/lib/libedit/read.c        Sun Oct 30 19:11:31 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: read.c,v 1.107 2021/08/15 10:08:41 christos Exp $      */
+/*     $NetBSD: read.c,v 1.108 2022/10/30 19:11:31 christos Exp $      */
 
 /*-
  * Copyright (c) 1992, 1993
@@ -37,7 +37,7 @@
 #if 0
 static char sccsid[] = "@(#)read.c     8.1 (Berkeley) 6/4/93";
 #else
-__RCSID("$NetBSD: read.c,v 1.107 2021/08/15 10:08:41 christos Exp $");
+__RCSID("$NetBSD: read.c,v 1.108 2022/10/30 19:11:31 christos Exp $");
 #endif
 #endif /* not lint && not SCCSID */
 
@@ -89,28 +89,31 @@
                return -1;
 
        ma = &el->el_read->macros;
-       if ((ma->macro = el_calloc(EL_MAXMACRO, sizeof(*ma->macro))) == NULL) {
-               free(el->el_read);
-               return -1;
-       }
+       if ((ma->macro = el_calloc(EL_MAXMACRO, sizeof(*ma->macro))) == NULL)
+               goto out;
        ma->level = -1;
        ma->offset = 0;
 
        /* builtin read_char */
        el->el_read->read_char = read_char;
        return 0;
+out:
+       read_end(el);
+       return -1;
 }
 
 /* el_read_end():
  *     Free the data structures used by the read stuff.
  */
 libedit_private void
-read_end(struct el_read_t *el_read)
+read_end(EditLine *el)
 {
-       read_clearmacros(&el_read->macros);
-       el_free(el_read->macros.macro);
-       el_read->macros.macro = NULL;
-       el_free(el_read);
+
+       read_clearmacros(&el->el_read->macros);
+       el_free(el->el_read->macros.macro);
+       el->el_read->macros.macro = NULL;
+       el_free(el->el_read);
+       el->el_read = NULL;
 }
 
 /* el_read_setfn():
diff -r dabe1b282aac -r 11b3166da91e lib/libedit/read.h
--- a/lib/libedit/read.h        Sun Oct 30 15:08:50 2022 +0000
+++ b/lib/libedit/read.h        Sun Oct 30 19:11:31 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: read.h,v 1.12 2016/05/22 19:44:26 christos Exp $       */
+/*     $NetBSD: read.h,v 1.13 2022/10/30 19:11:31 christos Exp $       */
 
 /*-
  * Copyright (c) 2001 The NetBSD Foundation, Inc.
@@ -36,7 +36,7 @@
 #define        _h_el_read
 
 libedit_private int            read_init(EditLine *);
-libedit_private void           read_end(struct el_read_t *);
+libedit_private void           read_end(EditLine *);
 libedit_private void           read_prepare(EditLine *);
 libedit_private void           read_finish(EditLine *);
 libedit_private int            el_read_setfn(struct el_read_t *, el_rfunc_t);
diff -r dabe1b282aac -r 11b3166da91e lib/libedit/readline.c
--- a/lib/libedit/readline.c    Sun Oct 30 15:08:50 2022 +0000
+++ b/lib/libedit/readline.c    Sun Oct 30 19:11:31 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: readline.c,v 1.176 2022/09/21 01:33:53 christos Exp $  */
+/*     $NetBSD: readline.c,v 1.177 2022/10/30 19:11:31 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.176 2022/09/21 01:33:53 christos Exp $");
+__RCSID("$NetBSD: readline.c,v 1.177 2022/10/30 19:11:31 christos Exp $");
 #endif /* not lint && not SCCSID */
 
 #include <sys/types.h>
@@ -240,7 +240,7 @@
                return NULL;
 
        len = strlen(p->pw_dir) + sizeof("/.history");
-       if ((path = malloc(len)) == NULL)
+       if ((path = el_malloc(len)) == NULL)
                return NULL;
 
        (void)snprintf(path, len, "%s/.history", p->pw_dir);
@@ -2331,6 +2331,8 @@
 
        len = (size_t)(to - from);
        out = el_malloc((size_t)len + 1);
+       if (out == NULL)
+               return NULL;
        (void)strlcpy(out, li->buffer + from , len);
 
        return out;
diff -r dabe1b282aac -r 11b3166da91e lib/libedit/terminal.c
--- a/lib/libedit/terminal.c    Sun Oct 30 15:08:50 2022 +0000
+++ b/lib/libedit/terminal.c    Sun Oct 30 19:11:31 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: terminal.c,v 1.44 2021/09/09 20:24:07 christos Exp $   */
+/*     $NetBSD: terminal.c,v 1.45 2022/10/30 19:11:31 christos Exp $   */
 
 /*-
  * Copyright (c) 1992, 1993
@@ -37,7 +37,7 @@



Home | Main Index | Thread Index | Old Index