Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/lib/libform - fix memory leak
details: https://anonhg.NetBSD.org/src/rev/501b9e2b6816
branches: trunk
changeset: 378457:501b9e2b6816
user: christos <christos%NetBSD.org@localhost>
date: Tue Apr 13 13:13:03 2021 +0000
description:
- fix memory leak
- xxx questionable allocation
- remove casts
- use sizeof(*var)
- bcopy -> memcpy/memmove
diffstat:
lib/libform/field.c | 46 +++++++++++++++---------------
lib/libform/form.c | 8 ++--
lib/libform/internals.c | 68 ++++++++++++++++++---------------------------
lib/libform/type_alnum.c | 12 ++++----
lib/libform/type_alpha.c | 10 +++---
lib/libform/type_enum.c | 8 ++--
lib/libform/type_integer.c | 10 +++---
lib/libform/type_numeric.c | 10 +++---
lib/libform/type_regex.c | 6 ++--
9 files changed, 83 insertions(+), 95 deletions(-)
diffs (truncated from 611 to 300 lines):
diff -r a0895d2ccf27 -r 501b9e2b6816 lib/libform/field.c
--- a/lib/libform/field.c Tue Apr 13 10:10:25 2021 +0000
+++ b/lib/libform/field.c Tue Apr 13 13:13:03 2021 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: field.c,v 1.31 2016/03/09 19:47:13 christos Exp $ */
+/* $NetBSD: field.c,v 1.32 2021/04/13 13:13:03 christos Exp $ */
/*-
* Copyright (c) 1998-1999 Brett Lymn
* (blymn%baea.com.au@localhost, brett_lymn%yahoo.com.au@localhost)
@@ -29,7 +29,7 @@
*/
#include <sys/cdefs.h>
-__RCSID("$NetBSD: field.c,v 1.31 2016/03/09 19:47:13 christos Exp $");
+__RCSID("$NetBSD: field.c,v 1.32 2021/04/13 13:13:03 christos Exp $");
#include <sys/param.h>
#include <stdlib.h>
@@ -751,12 +751,12 @@ static FIELD *
(nrows < 0) || (nbuf < 0))
return NULL;
- if ((new = (FIELD *)malloc(sizeof(FIELD))) == NULL) {
+ if ((new = malloc(sizeof(*new))) == NULL) {
return NULL;
}
/* copy in the default field info */
- bcopy(prototype, new, sizeof(FIELD));
+ memcpy(new, prototype, sizeof(*new));
new->nbuf = nbuf + 1;
new->rows = rows;
@@ -775,7 +775,6 @@ FIELD *
new_field(int rows, int cols, int frow, int fcol, int nrows, int nbuf)
{
FIELD *new;
- size_t buf_len;
int i;
@@ -783,31 +782,24 @@ new_field(int rows, int cols, int frow,
frow, fcol, nrows, nbuf)) == NULL)
return NULL;
- buf_len = (nbuf + 1) * sizeof(FORM_STR);
-
- if ((new->buffers = (FORM_STR *)malloc(buf_len)) == NULL) {
+ if ((new->buffers = calloc(nbuf + 1, sizeof(*new->buffers))) == NULL) {
free(new);
return NULL;
}
- /* Initialise the strings to a zero length string */
+ /* Initialise the strings to a zero length string */
for (i = 0; i < nbuf + 1; i++) {
if ((new->buffers[i].string =
- (char *) malloc(sizeof(char))) == NULL) {
- free(new->buffers);
- free(new);
- return NULL;
+ malloc(sizeof(*new->buffers[i].string))) == NULL) {
+ goto out;
}
new->buffers[i].string[0] = '\0';
new->buffers[i].length = 0;
new->buffers[i].allocated = 1;
}
- if ((new->alines = (_FORMI_FIELD_LINES *)
- malloc(sizeof(struct _formi_field_lines))) == NULL) {
- free(new->buffers);
- free(new);
- return NULL;
+ if ((new->alines = malloc(sizeof(*new->alines))) == NULL) {
+ goto out;
}
new->alines->prev = NULL;
@@ -822,6 +814,13 @@ new_field(int rows, int cols, int frow,
new->cur_line = new->alines;
return new;
+out:
+ while (--i >= 0) {
+ free(new->buffers[i].string);
+ }
+ free(new->buffers);
+ free(new);
+ return NULL;
}
/*
@@ -836,23 +835,24 @@ dup_field(FIELD *field, int frow, int fc
if (field == NULL)
return NULL;
- /* XXXX this right???? */
+ /* XXX: this right???? */
if ((new = _formi_create_field(field, (int) field->rows,
- (int ) field->cols,
+ (int) field->cols,
frow, fcol, (int) field->nrows,
field->nbuf - 1)) == NULL)
return NULL;
row_len = (field->rows + field->nrows + 1) * field->cols;
- buf_len = (field->nbuf + 1) * row_len * sizeof(FORM_STR);
+ buf_len = (field->nbuf + 1) * row_len * sizeof(*new->buffers);
- if ((new->buffers = (FORM_STR *)malloc(buf_len)) == NULL) {
+ /* XXX: dups buffers but not their strings? */
+ if ((new->buffers = malloc(buf_len)) == NULL) {
free(new);
return NULL;
}
/* copy the buffers from the source field into the new copy */
- bcopy(field->buffers, new->buffers, buf_len);
+ memcpy(new->buffers, field->buffers, buf_len);
return new;
}
diff -r a0895d2ccf27 -r 501b9e2b6816 lib/libform/form.c
--- a/lib/libform/form.c Tue Apr 13 10:10:25 2021 +0000
+++ b/lib/libform/form.c Tue Apr 13 13:13:03 2021 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: form.c,v 1.16 2016/03/09 19:47:13 christos Exp $ */
+/* $NetBSD: form.c,v 1.17 2021/04/13 13:13:04 christos Exp $ */
/*-
* Copyright (c) 1998-1999 Brett Lymn
@@ -30,7 +30,7 @@
*/
#include <sys/cdefs.h>
-__RCSID("$NetBSD: form.c,v 1.16 2016/03/09 19:47:13 christos Exp $");
+__RCSID("$NetBSD: form.c,v 1.17 2021/04/13 13:13:04 christos Exp $");
#include <stdlib.h>
#include <strings.h>
@@ -487,12 +487,12 @@ new_form(FIELD **fields)
{
FORM *new;
- if ((new = (FORM *) malloc(sizeof(FORM))) == NULL)
+ if ((new = malloc(sizeof(*new))) == NULL)
return NULL;
/* copy in the defaults... */
- bcopy(&_formi_default_form, new, sizeof(FORM));
+ memcpy(new, &_formi_default_form, sizeof(*new));
if (new->win == NULL)
new->scrwin = stdscr; /* something for curses to write to */
diff -r a0895d2ccf27 -r 501b9e2b6816 lib/libform/internals.c
--- a/lib/libform/internals.c Tue Apr 13 10:10:25 2021 +0000
+++ b/lib/libform/internals.c Tue Apr 13 13:13:03 2021 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: internals.c,v 1.39 2018/11/08 06:34:40 msaitoh Exp $ */
+/* $NetBSD: internals.c,v 1.40 2021/04/13 13:13:04 christos Exp $ */
/*-
* Copyright (c) 1998-1999 Brett Lymn
@@ -30,7 +30,7 @@
*/
#include <sys/cdefs.h>
-__RCSID("$NetBSD: internals.c,v 1.39 2018/11/08 06:34:40 msaitoh Exp $");
+__RCSID("$NetBSD: internals.c,v 1.40 2021/04/13 13:13:04 christos Exp $");
#include <limits.h>
#include <ctype.h>
@@ -219,19 +219,18 @@ copy_row(_FORMI_FIELD_LINES *row)
_FORMI_FIELD_LINES *new;
_formi_tab_t *tp, *newt;
- if ((new = (_FORMI_FIELD_LINES *) malloc(sizeof(_FORMI_FIELD_LINES)))
- == NULL) {
+ if ((new = malloc(sizeof(*new))) == NULL) {
return NULL;
}
- memcpy(new, row, sizeof(_FORMI_FIELD_LINES));
+ memcpy(new, row, sizeof(*new));
/* nuke the pointers from the source row so we don't get confused */
new->next = NULL;
new->prev = NULL;
new->tabs = NULL;
- if ((new->string = (char *) malloc((size_t)new->allocated)) == NULL) {
+ if ((new->string = malloc((size_t)new->allocated)) == NULL) {
free(new);
return NULL;
}
@@ -240,14 +239,13 @@ copy_row(_FORMI_FIELD_LINES *row)
if (row->tabs != NULL) {
tp = row->tabs;
- if ((new->tabs = (_formi_tab_t *) malloc(sizeof(_formi_tab_t)))
- == NULL) {
+ if ((new->tabs = malloc(sizeof(*new->tabs))) == NULL) {
free(new->string);
free(new);
return NULL;
}
- memcpy(new->tabs, row->tabs, sizeof(_formi_tab_t));
+ memcpy(new->tabs, row->tabs, sizeof(*new->tabs));
new->tabs->back = NULL;
new->tabs->fwd = NULL;
@@ -255,9 +253,7 @@ copy_row(_FORMI_FIELD_LINES *row)
newt = new->tabs;
while (tp != NULL) {
- if ((newt->fwd =
- (_formi_tab_t *) malloc(sizeof(_formi_tab_t)))
- == NULL) {
+ if ((newt->fwd = malloc(sizeof(*newt->fwd))) == NULL) {
/* error... unwind allocations */
tp = new->tabs;
while (tp != NULL) {
@@ -271,7 +267,7 @@ copy_row(_FORMI_FIELD_LINES *row)
return NULL;
}
- memcpy(newt->fwd, tp, sizeof(_formi_tab_t));
+ memcpy(newt->fwd, tp, sizeof(*newt->fwd));
newt->fwd->back = newt;
newt = newt->fwd;
newt->fwd = NULL;
@@ -1469,15 +1465,11 @@ int
{
int i, cur_page = 0;
- if ((form->page_starts = (_FORMI_PAGE_START *)
- malloc((form->max_page + 1) * sizeof(_FORMI_PAGE_START))) == NULL)
+ if ((form->page_starts = calloc((form->max_page + 1),
+ sizeof(*form->page_starts))) == NULL)
return E_SYSTEM_ERROR;
- /* initialise the page starts array */
- memset(form->page_starts, 0,
- (form->max_page + 1) * sizeof(_FORMI_PAGE_START));
-
- for (i =0; i < form->field_count; i++) {
+ for (i = 0; i < form->field_count; i++) {
if (form->fields[i]->page_break == 1)
cur_page++;
if (form->page_starts[cur_page].in_use == 0) {
@@ -1762,8 +1754,7 @@ int
* string. Everything should flow from there....
*/
if (row->string == NULL) {
- if ((row->string = (char *) malloc((size_t)INITIAL_LINE_ALLOC))
- == NULL)
+ if ((row->string = malloc((size_t)INITIAL_LINE_ALLOC)) == NULL)
return E_SYSTEM_ERROR;
row->string[0] = '\0';
row->allocated = INITIAL_LINE_ALLOC;
@@ -1827,7 +1818,7 @@ int
if (row->length + 2
>= row->allocated) {
new_size = row->allocated + 16 - (row->allocated % 16);
- if ((new = (char *) realloc(row->string,
+ if ((new = realloc(row->string,
(size_t) new_size )) == NULL)
return E_SYSTEM_ERROR;
row->allocated = new_size;
@@ -1836,7 +1827,7 @@ int
}
if ((field->overlay == 0) && (row->length > pos)) {
- bcopy(&row->string[pos], &row->string[pos + 1],
+ memmove(&row->string[pos + 1], &row->string[pos],
(size_t) (row->length - pos + 1));
}
@@ -1881,7 +1872,7 @@ int
* wrap failed for some reason, back out the
* char insert
*/
- bcopy(&row->string[pos + 1], &row->string[pos],
+ memmove(&row->string[pos], &row->string[pos + 1],
(size_t) (row->length - pos));
row->length--;
if (pos > 0)
@@ -2544,7 +2535,7 @@ int
}
saved = row->string[start];
- bcopy(&row->string[start + 1], &row->string[start],
+ memmove(&row->string[start], &row->string[start + 1],
(size_t) (end - start + 1));
row->string[end] = '\0';
row->length--;
@@ -2612,8 +2603,8 @@ int
Home |
Main Index |
Thread Index |
Old Index