From 7b6df1e236be9d0d76ed74eabe303425a43c91fc Mon Sep 17 00:00:00 2001 From: tb Date: Wed, 12 Apr 2017 18:24:37 +0000 Subject: [PATCH] Use calloc(3) and recallocarray(3) instead of *alloc* + memset(0). ok deraadt --- lib/libedit/chared.c | 34 +++++++++------------------------- lib/libedit/el.c | 6 ++---- lib/libedit/hist.c | 7 +++---- lib/libedit/terminal.c | 8 +++----- 4 files changed, 17 insertions(+), 38 deletions(-) diff --git a/lib/libedit/chared.c b/lib/libedit/chared.c index 18e3d0295f2..38327b4a1b4 100644 --- a/lib/libedit/chared.c +++ b/lib/libedit/chared.c @@ -1,4 +1,4 @@ -/* $OpenBSD: chared.c,v 1.27 2016/05/22 23:09:56 schwarze Exp $ */ +/* $OpenBSD: chared.c,v 1.28 2017/04/12 18:24:37 tb Exp $ */ /* $NetBSD: chared.c,v 1.28 2009/12/30 22:37:40 christos Exp $ */ /*- @@ -390,25 +390,20 @@ cv__endword(wchar_t *p, wchar_t *high, int n, int (*wtest)(wint_t)) protected int ch_init(EditLine *el) { - el->el_line.buffer = reallocarray(NULL, EL_BUFSIZ, - sizeof(*el->el_line.buffer)); + el->el_line.buffer = calloc(EL_BUFSIZ, sizeof(*el->el_line.buffer)); if (el->el_line.buffer == NULL) return -1; - - (void) memset(el->el_line.buffer, 0, EL_BUFSIZ * - sizeof(*el->el_line.buffer)); el->el_line.cursor = el->el_line.buffer; el->el_line.lastchar = el->el_line.buffer; el->el_line.limit = &el->el_line.buffer[EL_BUFSIZ - EL_LEAVE]; - el->el_chared.c_undo.buf = reallocarray(NULL, EL_BUFSIZ, + el->el_chared.c_undo.buf = calloc(EL_BUFSIZ, sizeof(*el->el_chared.c_undo.buf)); if (el->el_chared.c_undo.buf == NULL) return -1; - (void) memset(el->el_chared.c_undo.buf, 0, EL_BUFSIZ * - sizeof(*el->el_chared.c_undo.buf)); el->el_chared.c_undo.len = -1; el->el_chared.c_undo.cursor = 0; + el->el_chared.c_redo.buf = reallocarray(NULL, EL_BUFSIZ, sizeof(*el->el_chared.c_redo.buf)); if (el->el_chared.c_redo.buf == NULL) @@ -420,12 +415,10 @@ ch_init(EditLine *el) el->el_chared.c_vcmd.action = NOP; el->el_chared.c_vcmd.pos = el->el_line.buffer; - el->el_chared.c_kill.buf = reallocarray(NULL, EL_BUFSIZ, + el->el_chared.c_kill.buf = calloc(EL_BUFSIZ, sizeof(*el->el_chared.c_kill.buf)); if (el->el_chared.c_kill.buf == NULL) return -1; - (void) memset(el->el_chared.c_kill.buf, 0, EL_BUFSIZ * - sizeof(*el->el_chared.c_kill.buf)); 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; @@ -494,14 +487,11 @@ ch_enlargebufs(EditLine *el, size_t addlen) /* * Reallocate line buffer. */ - newbuffer = reallocarray(el->el_line.buffer, newsz, + newbuffer = recallocarray(el->el_line.buffer, sz, newsz, sizeof(*newbuffer)); if (!newbuffer) return 0; - /* zero the newly added memory, leave old data in */ - (void) memset(&newbuffer[sz], 0, (newsz - sz) * sizeof(*newbuffer)); - oldbuf = el->el_line.buffer; el->el_line.buffer = newbuffer; @@ -513,14 +503,11 @@ ch_enlargebufs(EditLine *el, size_t addlen) /* * Reallocate kill buffer. */ - newbuffer = reallocarray(el->el_chared.c_kill.buf, newsz, + newbuffer = recallocarray(el->el_chared.c_kill.buf, sz, newsz, sizeof(*newbuffer)); if (!newbuffer) return 0; - /* zero the newly added memory, leave old data in */ - (void) memset(&newbuffer[sz], 0, (newsz - sz) * sizeof(*newbuffer)); - oldkbuf = el->el_chared.c_kill.buf; el->el_chared.c_kill.buf = newbuffer; @@ -532,13 +519,10 @@ ch_enlargebufs(EditLine *el, size_t addlen) /* * Reallocate undo buffer. */ - newbuffer = reallocarray(el->el_chared.c_undo.buf, - newsz, sizeof(*newbuffer)); + newbuffer = recallocarray(el->el_chared.c_undo.buf, sz, newsz, + sizeof(*newbuffer)); if (!newbuffer) return 0; - - /* zero the newly added memory, leave old data in */ - (void) memset(&newbuffer[sz], 0, (newsz - sz) * sizeof(*newbuffer)); el->el_chared.c_undo.buf = newbuffer; newbuffer = reallocarray(el->el_chared.c_redo.buf, diff --git a/lib/libedit/el.c b/lib/libedit/el.c index 3999744baa9..2b57de13e6e 100644 --- a/lib/libedit/el.c +++ b/lib/libedit/el.c @@ -1,4 +1,4 @@ -/* $OpenBSD: el.c,v 1.36 2016/05/22 23:09:56 schwarze Exp $ */ +/* $OpenBSD: el.c,v 1.37 2017/04/12 18:24:37 tb Exp $ */ /* $NetBSD: el.c,v 1.61 2011/01/27 23:11:40 christos Exp $ */ /*- @@ -57,13 +57,11 @@ EditLine * el_init(const char *prog, FILE *fin, FILE *fout, FILE *ferr) { - EditLine *el = (EditLine *) malloc(sizeof(EditLine)); + EditLine *el = (EditLine *) calloc(1, sizeof(EditLine)); if (el == NULL) return NULL; - memset(el, 0, sizeof(EditLine)); - el->el_infile = fin; el->el_outfile = fout; el->el_errfile = ferr; diff --git a/lib/libedit/hist.c b/lib/libedit/hist.c index 8358e33cffc..c26ff93c12f 100644 --- a/lib/libedit/hist.c +++ b/lib/libedit/hist.c @@ -1,4 +1,4 @@ -/* $OpenBSD: hist.c,v 1.17 2016/04/11 20:43:33 schwarze Exp $ */ +/* $OpenBSD: hist.c,v 1.18 2017/04/12 18:24:37 tb Exp $ */ /* $NetBSD: hist.c,v 1.28 2016/04/11 00:50:13 christos Exp $ */ /*- @@ -193,12 +193,11 @@ hist_enlargebuf(EditLine *el, size_t oldsz, size_t newsz) { wchar_t *newbuf; - newbuf = reallocarray(el->el_history.buf, newsz, sizeof(*newbuf)); + newbuf = recallocarray(el->el_history.buf, oldsz, newsz, + sizeof(*newbuf)); if (!newbuf) return 0; - (void) memset(&newbuf[oldsz], '\0', (newsz - oldsz) * sizeof(*newbuf)); - el->el_history.last = newbuf + (el->el_history.last - el->el_history.buf); el->el_history.buf = newbuf; diff --git a/lib/libedit/terminal.c b/lib/libedit/terminal.c index 3c1c70f41d5..638997b1083 100644 --- a/lib/libedit/terminal.c +++ b/lib/libedit/terminal.c @@ -1,4 +1,4 @@ -/* $OpenBSD: terminal.c,v 1.17 2016/05/06 13:12:52 schwarze Exp $ */ +/* $OpenBSD: terminal.c,v 1.18 2017/04/12 18:24:37 tb Exp $ */ /* $NetBSD: terminal.c,v 1.17 2016/02/15 15:35:03 christos Exp $ */ /*- @@ -272,14 +272,12 @@ terminal_init(EditLine *el) if (el->el_terminal.t_fkey == NULL) goto fail3; el->el_terminal.t_loc = 0; - el->el_terminal.t_str = reallocarray(NULL, T_str, sizeof(char *)); + el->el_terminal.t_str = calloc(T_str, sizeof(char *)); if (el->el_terminal.t_str == NULL) goto fail4; - (void) memset(el->el_terminal.t_str, 0, T_str * sizeof(char *)); - el->el_terminal.t_val = reallocarray(NULL, T_val, sizeof(int)); + el->el_terminal.t_val = calloc(T_val, sizeof(int)); if (el->el_terminal.t_val == NULL) goto fail5; - (void) memset(el->el_terminal.t_val, 0, T_val * sizeof(int)); (void) terminal_set(el, NULL); terminal_init_arrow(el); return 0; -- 2.20.1