Module Name:    src
Committed By:   christos
Date:           Sun Oct 30 19:11:31 UTC 2022

Modified Files:
        src/lib/libedit: chared.c chartype.c el.c filecomplete.c map.c read.c
            read.h readline.c terminal.c

Log Message:
improvements in malloc/free handling.


To generate a diff of this commit:
cvs rdiff -u -r1.62 -r1.63 src/lib/libedit/chared.c
cvs rdiff -u -r1.35 -r1.36 src/lib/libedit/chartype.c
cvs rdiff -u -r1.100 -r1.101 src/lib/libedit/el.c
cvs rdiff -u -r1.70 -r1.71 src/lib/libedit/filecomplete.c
cvs rdiff -u -r1.54 -r1.55 src/lib/libedit/map.c
cvs rdiff -u -r1.107 -r1.108 src/lib/libedit/read.c
cvs rdiff -u -r1.12 -r1.13 src/lib/libedit/read.h
cvs rdiff -u -r1.176 -r1.177 src/lib/libedit/readline.c
cvs rdiff -u -r1.44 -r1.45 src/lib/libedit/terminal.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/chared.c
diff -u src/lib/libedit/chared.c:1.62 src/lib/libedit/chared.c:1.63
--- src/lib/libedit/chared.c:1.62	Tue Feb  8 16:13:22 2022
+++ src/lib/libedit/chared.c	Sun Oct 30 15:11:31 2022
@@ -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 @@ ch_init(EditLine *el)
 	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 @@ ch_init(EditLine *el)
 	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 @@ ch_init(EditLine *el)
 	el->el_state.lastcmd		= ED_UNASSIGNED;
 
 	return 0;
+out:
+	ch_end(el);
+	return -1;
 }
 
 /* ch_reset():

Index: src/lib/libedit/chartype.c
diff -u src/lib/libedit/chartype.c:1.35 src/lib/libedit/chartype.c:1.36
--- src/lib/libedit/chartype.c:1.35	Tue Jul 23 06:18:52 2019
+++ src/lib/libedit/chartype.c	Sun Oct 30 15:11:31 2022
@@ -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 @@ ct_decode_argv(int argc, const char *arg
 			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 */

Index: src/lib/libedit/el.c
diff -u src/lib/libedit/el.c:1.100 src/lib/libedit/el.c:1.101
--- src/lib/libedit/el.c:1.100	Sun Aug 15 06:08:41 2021
+++ src/lib/libedit/el.c	Sun Oct 30 15:11:31 2022
@@ -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 @@ el_end(EditLine *el)
 	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);

Index: src/lib/libedit/filecomplete.c
diff -u src/lib/libedit/filecomplete.c:1.70 src/lib/libedit/filecomplete.c:1.71
--- src/lib/libedit/filecomplete.c:1.70	Sat Mar 12 10:29:17 2022
+++ src/lib/libedit/filecomplete.c	Sun Oct 30 15:11:31 2022
@@ -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 @@ find_word_to_complete(const wchar_t * cu
 		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;

Index: src/lib/libedit/map.c
diff -u src/lib/libedit/map.c:1.54 src/lib/libedit/map.c:1.55
--- src/lib/libedit/map.c:1.54	Sun Aug 29 05:41:59 2021
+++ src/lib/libedit/map.c	Sun Oct 30 15:11:31 2022
@@ -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 @@ map_init(EditLine *el)
 		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(EditLine *el)
 	map_init_emacs(el);
 #endif /* VIDEFAULT */
 	return 0;
+out:
+	map_end(el);
+	return -1;
 }
 
 

Index: src/lib/libedit/read.c
diff -u src/lib/libedit/read.c:1.107 src/lib/libedit/read.c:1.108
--- src/lib/libedit/read.c:1.107	Sun Aug 15 06:08:41 2021
+++ src/lib/libedit/read.c	Sun Oct 30 15:11:31 2022
@@ -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 @@ read_init(EditLine *el)
 		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():

Index: src/lib/libedit/read.h
diff -u src/lib/libedit/read.h:1.12 src/lib/libedit/read.h:1.13
--- src/lib/libedit/read.h:1.12	Sun May 22 15:44:26 2016
+++ src/lib/libedit/read.h	Sun Oct 30 15:11:31 2022
@@ -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);

Index: src/lib/libedit/readline.c
diff -u src/lib/libedit/readline.c:1.176 src/lib/libedit/readline.c:1.177
--- src/lib/libedit/readline.c:1.176	Tue Sep 20 21:33:53 2022
+++ src/lib/libedit/readline.c	Sun Oct 30 15:11:31 2022
@@ -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 @@ _default_history_file(void)
 		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 @@ rl_copy_text(int from, int to)
 
 	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;

Index: src/lib/libedit/terminal.c
diff -u src/lib/libedit/terminal.c:1.44 src/lib/libedit/terminal.c:1.45
--- src/lib/libedit/terminal.c:1.44	Thu Sep  9 16:24:07 2021
+++ src/lib/libedit/terminal.c	Sun Oct 30 15:11:31 2022
@@ -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 @@
 #if 0
 static char sccsid[] = "@(#)term.c	8.2 (Berkeley) 4/30/95";
 #else
-__RCSID("$NetBSD: terminal.c,v 1.44 2021/09/09 20:24:07 christos Exp $");
+__RCSID("$NetBSD: terminal.c,v 1.45 2022/10/30 19:11:31 christos Exp $");
 #endif
 #endif /* not lint && not SCCSID */
 
@@ -272,40 +272,29 @@ terminal_init(EditLine *el)
 	el->el_terminal.t_buf = el_calloc(TC_BUFSIZE,
 	    sizeof(*el->el_terminal.t_buf));
 	if (el->el_terminal.t_buf == NULL)
-		goto fail1;
+		return -1;
 	el->el_terminal.t_cap = el_calloc(TC_BUFSIZE,
 	    sizeof(*el->el_terminal.t_cap));
 	if (el->el_terminal.t_cap == NULL)
-		goto fail2;
+		goto out;
 	el->el_terminal.t_fkey = el_calloc(A_K_NKEYS,
 	    sizeof(*el->el_terminal.t_fkey));
 	if (el->el_terminal.t_fkey == NULL)
-		goto fail3;
+		goto out;
 	el->el_terminal.t_loc = 0;
 	el->el_terminal.t_str = el_calloc(T_str,
 	    sizeof(*el->el_terminal.t_str));
 	if (el->el_terminal.t_str == NULL)
-		goto fail4;
+		goto out;
 	el->el_terminal.t_val = el_calloc(T_val,
 	    sizeof(*el->el_terminal.t_val));
 	if (el->el_terminal.t_val == NULL)
-		goto fail5;
+		goto out;
 	(void) terminal_set(el, NULL);
 	terminal_init_arrow(el);
 	return 0;
-fail5:
-	free(el->el_terminal.t_str);
-	el->el_terminal.t_str = NULL;
-fail4:
-	free(el->el_terminal.t_fkey);
-	el->el_terminal.t_fkey = NULL;
-fail3:
-	free(el->el_terminal.t_cap);
-	el->el_terminal.t_cap = NULL;
-fail2:
-	free(el->el_terminal.t_buf);
-	el->el_terminal.t_buf = NULL;
-fail1:
+out:
+	terminal_end(el);
 	return -1;
 }
 

Reply via email to