- fix memory leak

- xxx questionable allocation
- remove casts
- use sizeof(*var)
- bcopy -> memcpy/memmove
This commit is contained in:
christos 2021-04-13 13:13:03 +00:00
parent e43c684a44
commit 0cf983201b
9 changed files with 83 additions and 95 deletions

View File

@ -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, brett_lymn@yahoo.com.au)
@ -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 @@ _formi_create_field(FIELD *prototype, int rows, int cols, int frow,
(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, int fcol, int nrows, int nbuf)
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, int fcol, int nrows, int nbuf)
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 fcol)
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;
}

View File

@ -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 */

View File

@ -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 @@ _formi_find_pages(FORM *form)
{
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 @@ _formi_add_char(FIELD *field, unsigned int pos, char c)
* 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 @@ _formi_add_char(FIELD *field, unsigned int pos, char c)
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 @@ _formi_add_char(FIELD *field, unsigned int pos, char c)
}
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 @@ _formi_add_char(FIELD *field, unsigned int pos, char c)
* 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 @@ _formi_manipulate_field(FORM *form, int c)
}
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 @@ _formi_manipulate_field(FORM *form, int c)
if ((cur->rows + cur->nrows) > 1) {
if (_formi_wrap_field(cur, row) != E_OK) {
bcopy(&row->string[start],
&row->string[start + 1],
memmove(&row->string[start + 1],
&row->string[start],
(size_t) (end - start));
row->length++;
row->string[start] = saved;
@ -2675,7 +2666,7 @@ _formi_manipulate_field(FORM *form, int c)
* without losing a char.
*/
saved = row->string[start - 1];
bcopy(&row->string[start], &row->string[start - 1],
memmove(&row->string[start - 1], &row->string[start],
(size_t) (end - start + 1));
row->length--;
row->string[row->length] = '\0';
@ -2707,8 +2698,8 @@ _formi_manipulate_field(FORM *form, int c)
}
if ((_formi_wrap_field(cur, row) != E_OK)) {
bcopy(&row->string[start - 1],
&row->string[start],
memmove(&row->string[start],
&row->string[start - 1],
(size_t) (end - start));
row->length++;
row->string[start - 1] = saved;
@ -2797,7 +2788,7 @@ _formi_manipulate_field(FORM *form, int c)
start = find_sow(start, &row);
str = row->string;
/* XXXX hmmmm what if start and end on diff rows? XXXX */
bcopy(&str[end], &str[start],
memmove(&str[start], &str[end],
(size_t) (row->length - end + 1));
len = end - start;
row->length -= len;
@ -3150,9 +3141,8 @@ field_sort_compare(const void *one, const void *two)
const FIELD *a, *b;
int tl;
/* LINTED const castaway; we don't modify these! */
a = (const FIELD *) *((const FIELD **) one);
b = (const FIELD *) *((const FIELD **) two);
a = *(const FIELD **) __UNCONST(one);
b = *(const FIELD **) __UNCONST(two);
if (a == NULL)
return 1;
@ -3191,8 +3181,7 @@ _formi_sort_fields(FORM *form)
== NULL)
return;
bcopy(form->fields, sort_area,
(size_t) (sizeof(FIELD *) * form->field_count));
memcpy(sort_area, form->fields, sizeof(*sort_area) * form->field_count);
qsort(sort_area, (size_t) form->field_count, sizeof(FIELD *),
field_sort_compare);
@ -3392,8 +3381,7 @@ _formi_calculate_tabs(_FORMI_FIELD_LINES *row)
for (i = 0, j = 0; i < row->length; i++, j++) {
if (row->string[i] == '\t') {
if (*tsp == NULL) {
if ((*tsp = (_formi_tab_t *)
malloc(sizeof(_formi_tab_t))) == NULL)
if ((*tsp = malloc(sizeof(*tsp))) == NULL)
return;
(*tsp)->back = old_ts;
(*tsp)->fwd = NULL;
@ -3552,7 +3540,7 @@ _formi_sync_buffer(FIELD *field)
* init nstr up front, just in case there are no line contents,
* this could happen if the field just contains hard returns.
*/
if ((nstr = malloc(sizeof(char))) == NULL)
if ((nstr = malloc(sizeof(*nstr))) == NULL)
return E_SYSTEM_ERROR;
nstr[0] = '\0';

View File

@ -1,4 +1,4 @@
/* $NetBSD: type_alnum.c,v 1.11 2021/04/13 00:29:42 mrg Exp $ */
/* $NetBSD: type_alnum.c,v 1.12 2021/04/13 13:13:04 christos Exp $ */
/*-
* Copyright (c) 1998-1999 Brett Lymn
@ -54,12 +54,12 @@ create_alnum_args(va_list *args)
{
alnum_args *new;
new = (alnum_args *) malloc(sizeof(alnum_args));
new = malloc(sizeof(*new));
if (new != NULL)
new->width = va_arg(*args, int);
return (void *) new;
return (void *)new;
}
/*
@ -70,12 +70,12 @@ copy_alnum_args(char *args)
{
alnum_args *new;
new = (alnum_args *) malloc(sizeof(alnum_args));
new = malloc(sizeof(*new));
if (new != NULL)
new->width = ((alnum_args *) (void *)args)->width;
return (char *) (void *) new;
return (void *)new;
}
/*
@ -133,7 +133,7 @@ alnum_check_field(FIELD *field, char *args)
if (buf[cur] != '\0')
return FALSE;
if ((new = (char *) malloc(sizeof(char) * (end - start + 1))) == NULL)
if ((new = malloc(sizeof(*new) * (end - start + 1))) == NULL)
return FALSE;
if ((end - start) >= 1) {

View File

@ -1,4 +1,4 @@
/* $NetBSD: type_alpha.c,v 1.12 2021/04/13 00:29:42 mrg Exp $ */
/* $NetBSD: type_alpha.c,v 1.13 2021/04/13 13:13:04 christos Exp $ */
/*-
* Copyright (c) 1998-1999 Brett Lymn
@ -30,7 +30,7 @@
*/
#include <sys/cdefs.h>
__RCSID("$NetBSD: type_alpha.c,v 1.12 2021/04/13 00:29:42 mrg Exp $");
__RCSID("$NetBSD: type_alpha.c,v 1.13 2021/04/13 13:13:04 christos Exp $");
#include <stdlib.h>
#include <string.h>
@ -56,7 +56,7 @@ create_alpha_args(va_list *args)
{
alpha_args *new;
new = (alpha_args *) malloc(sizeof(alpha_args));
new = malloc(sizeof(*new));
if (new != NULL)
new->width = va_arg(*args, int);
@ -72,7 +72,7 @@ copy_alpha_args(char *args)
{
alpha_args *new;
new = (alpha_args *) malloc(sizeof(alpha_args));
new = malloc(sizeof(*new));
if (new != NULL)
new->width = ((alpha_args *) (void *) args)->width;
@ -136,7 +136,7 @@ alpha_check_field(FIELD *field, char *args)
return FALSE;
/* set buffer 0 to the new string */
if ((new = (char *) malloc(sizeof(char) * (end - start + 1))) == NULL)
if ((new = malloc(sizeof(*new) * (end - start + 1))) == NULL)
return FALSE;
if ((end - start) >= 1) {

View File

@ -1,4 +1,4 @@
/* $NetBSD: type_enum.c,v 1.12 2016/03/09 19:47:13 christos Exp $ */
/* $NetBSD: type_enum.c,v 1.13 2021/04/13 13:13:04 christos Exp $ */
/*-
* Copyright (c) 1998-1999 Brett Lymn
@ -30,7 +30,7 @@
*/
#include <sys/cdefs.h>
__RCSID("$NetBSD: type_enum.c,v 1.12 2016/03/09 19:47:13 christos Exp $");
__RCSID("$NetBSD: type_enum.c,v 1.13 2021/04/13 13:13:04 christos Exp $");
#include <ctype.h>
#include <stdlib.h>
@ -121,10 +121,10 @@ copy_enum_args(char *args)
{
enum_args *new;
new = (enum_args *) malloc(sizeof(enum_args));
new = malloc(sizeof(*new));
if (new != NULL)
bcopy(args, new, sizeof(enum_args));
memcpy(new, args, sizeof(*new));
return (void *) new;
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: type_integer.c,v 1.8 2004/10/28 21:14:52 dsl Exp $ */
/* $NetBSD: type_integer.c,v 1.9 2021/04/13 13:13:04 christos Exp $ */
/*-
* Copyright (c) 1998-1999 Brett Lymn
@ -30,7 +30,7 @@
*/
#include <sys/cdefs.h>
__RCSID("$NetBSD: type_integer.c,v 1.8 2004/10/28 21:14:52 dsl Exp $");
__RCSID("$NetBSD: type_integer.c,v 1.9 2021/04/13 13:13:04 christos Exp $");
#include <stdlib.h>
#include <strings.h>
@ -58,7 +58,7 @@ create_integer_args(va_list *args)
{
integer_args *new;
new = (integer_args *) malloc(sizeof(integer_args));
new = malloc(sizeof(*new));
if (new != NULL) {
new->precision = va_arg(*args, unsigned);
@ -77,10 +77,10 @@ copy_integer_args(char *args)
{
integer_args *new;
new = (integer_args *) malloc(sizeof(integer_args));
new = malloc(sizeof(*new));
if (new != NULL)
bcopy(args, new, sizeof(integer_args));
memcpy(new, args, sizeof(*new));
return (void *) new;
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: type_numeric.c,v 1.9 2016/05/30 17:48:29 dholland Exp $ */
/* $NetBSD: type_numeric.c,v 1.10 2021/04/13 13:13:04 christos Exp $ */
/*-
* Copyright (c) 1998-1999 Brett Lymn
@ -30,7 +30,7 @@
*/
#include <sys/cdefs.h>
__RCSID("$NetBSD: type_numeric.c,v 1.9 2016/05/30 17:48:29 dholland Exp $");
__RCSID("$NetBSD: type_numeric.c,v 1.10 2021/04/13 13:13:04 christos Exp $");
#include <stdlib.h>
#include <string.h>
@ -58,7 +58,7 @@ create_numeric_args(va_list *args)
{
numeric_args *new;
new = (numeric_args *) malloc(sizeof(numeric_args));
new = malloc(sizeof(*new));
if (new != NULL) {
new->precision = va_arg(*args, unsigned);
@ -77,10 +77,10 @@ copy_numeric_args(char *args)
{
numeric_args *new;
new = (numeric_args *) malloc(sizeof(numeric_args));
new = malloc(sizeof(*new));
if (new != NULL)
bcopy(args, new, sizeof(numeric_args));
memcpy(new, args, sizeof(*args));
return (void *) new;
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: type_regex.c,v 1.7 2004/11/24 11:57:09 blymn Exp $ */
/* $NetBSD: type_regex.c,v 1.8 2021/04/13 13:13:04 christos Exp $ */
/*-
* Copyright (c) 1998-1999 Brett Lymn
@ -30,7 +30,7 @@
*/
#include <sys/cdefs.h>
__RCSID("$NetBSD: type_regex.c,v 1.7 2004/11/24 11:57:09 blymn Exp $");
__RCSID("$NetBSD: type_regex.c,v 1.8 2021/04/13 13:13:04 christos Exp $");
#include <stdlib.h>
#include <sys/types.h>
@ -58,7 +58,7 @@ create_regex_args(va_list *args)
regex_args *new;
char *expression;
new = (regex_args *) malloc(sizeof(regex_args));
new = malloc(sizeof(*new));
if (new != NULL) {
new->references = 1;