diff options
author | Todd C. Miller <millert@cvs.openbsd.org> | 1998-08-14 21:11:46 +0000 |
---|---|---|
committer | Todd C. Miller <millert@cvs.openbsd.org> | 1998-08-14 21:11:46 +0000 |
commit | 283fd495b05f3ef5c93d53cd4be8b0f97cb4f416 (patch) | |
tree | d941f937f4eff424406f1a354298c99447279880 /lib | |
parent | 209a6bcb90e34d7503b2c334d45efe7c8afb3019 (diff) |
fix realloc usage
Diffstat (limited to 'lib')
-rw-r--r-- | lib/libcurses/comp_expand.c | 13 | ||||
-rw-r--r-- | lib/libcurses/lib_doupdate.c | 10 | ||||
-rw-r--r-- | lib/libcurses/lib_tparm.c | 13 | ||||
-rw-r--r-- | lib/libcurses/read_termcap.c | 26 | ||||
-rw-r--r-- | lib/libcurses/safe_sprintf.c | 22 | ||||
-rw-r--r-- | lib/libcurses/trace_buf.c | 26 | ||||
-rw-r--r-- | lib/libcurses/wresize.c | 12 |
7 files changed, 86 insertions, 36 deletions
diff --git a/lib/libcurses/comp_expand.c b/lib/libcurses/comp_expand.c index a85de82121e..286326d56e4 100644 --- a/lib/libcurses/comp_expand.c +++ b/lib/libcurses/comp_expand.c @@ -1,4 +1,4 @@ -/* $OpenBSD: comp_expand.c,v 1.1 1998/07/23 21:17:25 millert Exp $ */ +/* $OpenBSD: comp_expand.c,v 1.2 1998/08/14 21:11:38 millert Exp $ */ /**************************************************************************** * Copyright (c) 1998 Free Software Foundation, Inc. * @@ -58,17 +58,22 @@ static size_t length; int bufp; const char *ptr, *str = VALID_STRING(srcp) ? srcp : ""; +char *nbuffer; bool islong = (strlen(str) > 3); size_t need = (2 + strlen(str)) * 4; int ch; if (buffer == 0) { - buffer = malloc(length = need); + nbuffer = malloc(length = need); } else if (need > length) { - buffer = realloc(buffer, length = need); + nbuffer = realloc(buffer, length = need); } - if (buffer == 0) + if (nbuffer == 0) { + if (buffer != 0) + free(buffer); return(NULL); + } + buffer = nbuffer; bufp = 0; ptr = str; diff --git a/lib/libcurses/lib_doupdate.c b/lib/libcurses/lib_doupdate.c index b29bab54e6f..e13052a0cea 100644 --- a/lib/libcurses/lib_doupdate.c +++ b/lib/libcurses/lib_doupdate.c @@ -1,4 +1,4 @@ -/* $OpenBSD: lib_doupdate.c,v 1.6 1998/07/23 21:18:42 millert Exp $ */ +/* $OpenBSD: lib_doupdate.c,v 1.7 1998/08/14 21:11:39 millert Exp $ */ /**************************************************************************** * Copyright (c) 1998 Free Software Foundation, Inc. * @@ -929,16 +929,18 @@ int top = total; int last = min(screen_columns, newscr->_maxx+1); size_t length = sizeof(chtype) * last; chtype blank = newscr->_line[total-1].text[last-1]; /* lower right char */ +chtype *ntstLine; if(!clr_eos || !can_clear_with(blank)) return total; if (tstLine == 0) - tstLine = (chtype *)malloc(length); + ntstLine = (chtype *)malloc(length); else if (length > lenLine) - tstLine = (chtype *)realloc(tstLine, length); + ntstLine = (chtype *)realloc(tstLine, length); - if (tstLine != 0) { + if (ntstLine != 0) { + tstLine = ntstLine; lenLine = length; for (col = 0; col < last; col++) tstLine[col] = blank; diff --git a/lib/libcurses/lib_tparm.c b/lib/libcurses/lib_tparm.c index 7f734678153..672ae532e46 100644 --- a/lib/libcurses/lib_tparm.c +++ b/lib/libcurses/lib_tparm.c @@ -1,4 +1,4 @@ -/* $OpenBSD: lib_tparm.c,v 1.1 1998/07/23 21:19:38 millert Exp $ */ +/* $OpenBSD: lib_tparm.c,v 1.2 1998/08/14 21:11:40 millert Exp $ */ /**************************************************************************** * Copyright (c) 1998 Free Software Foundation, Inc. * @@ -302,10 +302,17 @@ static int static_vars[NUM_VARS]; } } if ((size_t)(cp - string) > len_fmt) { + char *nformat; + len_fmt = (cp - string) + len_fmt + 2; - format = format ? realloc(format, len_fmt) : malloc(len_fmt); - if (format == 0) + nformat = format ? realloc(format, len_fmt) : malloc(len_fmt); + if (nformat != 0) { + format = nformat; + } else { + if (format != 0) + free(format); return 0; + } } if (number > 9) number = 9; diff --git a/lib/libcurses/read_termcap.c b/lib/libcurses/read_termcap.c index bed10b8b456..af23d89e3b3 100644 --- a/lib/libcurses/read_termcap.c +++ b/lib/libcurses/read_termcap.c @@ -1,4 +1,4 @@ -/* $OpenBSD: read_termcap.c,v 1.2 1998/07/27 03:37:35 millert Exp $ */ +/* $OpenBSD: read_termcap.c,v 1.3 1998/08/14 21:11:41 millert Exp $ */ /**************************************************************************** * Copyright (c) 1998 Free Software Foundation, Inc. * @@ -398,16 +398,20 @@ _nc_getent( if (rp >= r_end) { unsigned int pos; size_t newsize; + char *nrecord; pos = rp - record; newsize = r_end - record + BFRAG; - record = realloc(record, newsize); - if (record == 0) { - errno = ENOMEM; + nrecord = realloc(record, newsize); + if (nrecord == 0) { + if (record != 0) + free(nrecord); if (myfd) (void)close(fd); + errno = ENOMEM; return (TC_SYS_ERR); } + record = nrecord; r_end = record + newsize; rp = record + pos; } @@ -531,19 +535,23 @@ _nc_getent( if (diff >= r_end - rp) { unsigned int pos, tcpos, tcposend; size_t newsize; + char *nrecord; pos = rp - record; newsize = r_end - record + diff + BFRAG; tcpos = tcstart - record; tcposend = tcend - record; - record = realloc(record, newsize); + nrecord = realloc(record, newsize); if (record == 0) { - errno = ENOMEM; + if (record != 0) + free(record); if (myfd) (void)close(fd); free(icap); + errno = ENOMEM; return (TC_SYS_ERR); } + record = nrecord; r_end = record + newsize; rp = record + pos; tcstart = record + tcpos; @@ -575,10 +583,14 @@ _nc_getent( (void)close(fd); *len = rp - record - 1; /* don't count NUL */ if (r_end > rp) { - if ((record = realloc(record, (size_t)(rp - record))) == 0) { + char *nrecord; + if ((nrecord = realloc(record, (size_t)(rp - record))) == 0) { + if (record != 0) + free(record); errno = ENOMEM; return (TC_SYS_ERR); } + record = nrecord; } *cap = record; diff --git a/lib/libcurses/safe_sprintf.c b/lib/libcurses/safe_sprintf.c index 1b9f690d5df..01f98d470cd 100644 --- a/lib/libcurses/safe_sprintf.c +++ b/lib/libcurses/safe_sprintf.c @@ -1,4 +1,4 @@ -/* $OpenBSD: safe_sprintf.c,v 1.2 1998/07/23 21:20:01 millert Exp $ */ +/* $OpenBSD: safe_sprintf.c,v 1.3 1998/08/14 21:11:42 millert Exp $ */ /**************************************************************************** * Copyright (c) 1998 Free Software Foundation, Inc. * @@ -150,12 +150,17 @@ _nc_printf_length(const char *fmt, va_list ap) if (prec < 0) prec = strlen(pval); if (prec > (int)length) { + char *nbuffer; + length = length + prec; - buffer = realloc(buffer, length); - if (buffer == 0) { + nbuffer = realloc(buffer, length); + if (nbuffer == 0) { + if (buffer != 0) + free(buffer); free(format); return -1; } + buffer = nbuffer; } used = 'p'; break; @@ -221,17 +226,22 @@ _nc_printf_string(const char *fmt, va_list ap) static int rows, cols; static char *buf; static size_t len; + char *nbuf; if (screen_lines > rows || screen_columns > cols) { if (screen_lines > rows) rows = screen_lines; if (screen_columns > cols) cols = screen_columns; len = (rows * (cols + 1)) + 1; if (buf == 0) - buf = malloc(len); + nbuf = malloc(len); else - buf = realloc(buf, len); - if (buf == NULL) + nbuf = realloc(buf, len); + if (nbuf == NULL) { + if (buf != NULL) + free(buf); return(NULL); + } + buf = nbuf; } if (buf != 0) { diff --git a/lib/libcurses/trace_buf.c b/lib/libcurses/trace_buf.c index d06a068f6b8..bc15ceee441 100644 --- a/lib/libcurses/trace_buf.c +++ b/lib/libcurses/trace_buf.c @@ -1,4 +1,4 @@ -/* $OpenBSD: trace_buf.c,v 1.2 1998/07/23 21:20:06 millert Exp $ */ +/* $OpenBSD: trace_buf.c,v 1.3 1998/08/14 21:11:44 millert Exp $ */ /**************************************************************************** * Copyright (c) 1998 Free Software Foundation, Inc. * @@ -44,7 +44,7 @@ char * _nc_trace_buf(int bufnum, size_t want) static struct { char *text; size_t size; - } *list; + } *list, *nlist; static size_t have; #if NO_LEAKS @@ -62,10 +62,13 @@ char * _nc_trace_buf(int bufnum, size_t want) if ((size_t)(bufnum+1) > have) { size_t need = (bufnum + 1) * 2; size_t used = sizeof(*list) * need; - list = (list == 0) ? malloc(used) : realloc(list, used); - if (list == 0) { + nlist = (list == 0) ? malloc(used) : realloc(list, used); + if (nlist == 0) { + if (list != 0) + free(list); return(NULL); } + list = nlist; while (need > have) list[have++].text = 0; } @@ -73,12 +76,19 @@ char * _nc_trace_buf(int bufnum, size_t want) if (list[bufnum].text == 0) { list[bufnum].text = malloc(want); - list[bufnum].size = want; } - else if (want > list[bufnum].size) { - list[bufnum].text = realloc(list[bufnum].text, want); - list[bufnum].size = want; + else if (want > list[bufnum].size) + { + char *p = realloc(list[bufnum].text, want); + + if (p != 0) { + list[bufnum].text = p; + } else { + free(list[bufnum].text); + list[bufnum].text = 0; + } } + list[bufnum].size = want; if (list[bufnum].text != 0) *(list[bufnum].text) = '\0'; return list[bufnum].text; diff --git a/lib/libcurses/wresize.c b/lib/libcurses/wresize.c index 6fa64f9a5f7..6d28cb0b6f9 100644 --- a/lib/libcurses/wresize.c +++ b/lib/libcurses/wresize.c @@ -1,4 +1,4 @@ -/* $OpenBSD: wresize.c,v 1.4 1998/07/23 21:20:11 millert Exp $ */ +/* $OpenBSD: wresize.c,v 1.5 1998/08/14 21:11:45 millert Exp $ */ /**************************************************************************** * Copyright (c) 1998 Free Software Foundation, Inc. * @@ -45,11 +45,15 @@ MODULE_ID("$From: wresize.c,v 1.9 1998/02/11 12:13:54 tom Exp $") static void *doalloc(void *p, size_t n) { + void *np; + if (p == 0) - p = malloc(n); + np = malloc(n); else - p = realloc(p, n); - return p; + np = realloc(p, n); + if (np == 0 && p != 0) + free(p); + return np; } #define DOALLOC(p,t,n) (t *)doalloc(p, sizeof(t)*(n)) |