Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/lib/libedit don't crash in memory shortage conditions.
details: https://anonhg.NetBSD.org/src/rev/d5bc609bbcc8
branches: trunk
changeset: 538729:d5bc609bbcc8
user: christos <christos%NetBSD.org@localhost>
date: Sun Oct 27 20:24:28 2002 +0000
description:
don't crash in memory shortage conditions.
diffstat:
lib/libedit/history.c | 80 ++++++++++++++++------
lib/libedit/readline.c | 165 +++++++++++++++++++++++++++++++++++++++++------
lib/libedit/tokenizer.c | 25 +++++-
3 files changed, 218 insertions(+), 52 deletions(-)
diffs (truncated from 618 to 300 lines):
diff -r d3f86772e2bb -r d5bc609bbcc8 lib/libedit/history.c
--- a/lib/libedit/history.c Sun Oct 27 20:16:41 2002 +0000
+++ b/lib/libedit/history.c Sun Oct 27 20:24:28 2002 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: history.c,v 1.20 2002/10/13 17:15:53 christos Exp $ */
+/* $NetBSD: history.c,v 1.21 2002/10/27 20:24:28 christos Exp $ */
/*-
* Copyright (c) 1992, 1993
@@ -41,7 +41,7 @@
#if 0
static char sccsid[] = "@(#)history.c 8.1 (Berkeley) 6/4/93";
#else
-__RCSID("$NetBSD: history.c,v 1.20 2002/10/13 17:15:53 christos Exp $");
+__RCSID("$NetBSD: history.c,v 1.21 2002/10/27 20:24:28 christos Exp $");
#endif
#endif /* not lint && not SCCSID */
@@ -139,7 +139,7 @@
private int history_def_set(ptr_t, HistEvent *, const int n);
private int history_def_enter(ptr_t, HistEvent *, const char *);
private int history_def_add(ptr_t, HistEvent *, const char *);
-private void history_def_init(ptr_t *, HistEvent *, int);
+private int history_def_init(ptr_t *, HistEvent *, int);
private void history_def_clear(ptr_t, HistEvent *);
private int history_def_insert(history_t *, HistEvent *, const char *);
private void history_def_delete(history_t *, HistEvent *, hentry_t *);
@@ -345,13 +345,13 @@
return (history_def_enter(p, ev, str));
len = strlen(evp->str) + strlen(str) + 1;
s = (char *) h_malloc(len);
- if (!s) {
+ if (s == NULL) {
he_seterrev(ev, _HE_MALLOC_FAILED);
return (-1);
}
(void) strlcpy(s, h->cursor->ev.str, len);
(void) strlcat(s, str, len);
- h_free(evp->str);
+ h_free((ptr_t)evp->str);
evp->str = s;
*ev = h->cursor->ev;
return (0);
@@ -384,11 +384,11 @@
{
h->cursor = (hentry_t *) h_malloc(sizeof(hentry_t));
- if (h->cursor)
- h->cursor->ev.str = strdup(str);
- if (!h->cursor || !h->cursor->ev.str) {
- he_seterrev(ev, _HE_MALLOC_FAILED);
- return (-1);
+ if (h->cursor == NULL)
+ goto oomem;
+ if ((h->cursor->ev.str = strdup(str)) == NULL) {
+ h_free((ptr_t)h->cursor);
+ goto oomem;
}
h->cursor->ev.num = ++h->eventid;
h->cursor->next = h->list.next;
@@ -399,6 +399,9 @@
*ev = h->cursor->ev;
return (0);
+oomem:
+ he_seterrev(ev, _HE_MALLOC_FAILED);
+ return (-1);
}
@@ -428,10 +431,12 @@
* Default history initialization function
*/
/* ARGSUSED */
-private void
+private int
history_def_init(ptr_t *p, HistEvent *ev, int n)
{
history_t *h = (history_t *) h_malloc(sizeof(history_t));
+ if (h == NULL)
+ return -1;
if (n <= 0)
n = 0;
@@ -443,6 +448,7 @@
h->list.ev.num = 0;
h->cursor = &h->list;
*p = (ptr_t) h;
+ return 0;
}
@@ -471,10 +477,15 @@
public History *
history_init(void)
{
+ HistEvent ev;
History *h = (History *) h_malloc(sizeof(History));
- HistEvent ev;
+ if (h == NULL)
+ return NULL;
- history_def_init(&h->h_ref, &ev, 0);
+ if (history_def_init(&h->h_ref, &ev, 0) == -1) {
+ h_free((ptr_t)h);
+ return NULL;
+ }
h->h_ent = -1;
h->h_next = history_def_next;
h->h_first = history_def_first;
@@ -613,6 +624,8 @@
goto done;
ptr = h_malloc(max_size = 1024);
+ if (ptr == NULL)
+ goto done;
for (i = 0; (line = fgetln(fp, &sz)) != NULL; i++) {
char c = line[sz];
@@ -622,15 +635,24 @@
line[sz] = '\0';
if (max_size < sz) {
+ char *nptr;
max_size = (sz + 1023) & ~1023;
- ptr = h_realloc(ptr, max_size);
+ nptr = h_realloc(ptr, max_size);
+ if (nptr == NULL) {
+ i = -1;
+ goto oomem;
+ }
+ ptr = nptr;
}
(void) strunvis(ptr, line);
line[sz] = c;
- HENTER(h, &ev, ptr);
+ if (HENTER(h, &ev, ptr) == -1) {
+ h_free((ptr_t)ptr);
+ return -1;
+ }
}
- h_free(ptr);
-
+oomem:
+ h_free((ptr_t)ptr);
done:
(void) fclose(fp);
return (i);
@@ -645,28 +667,40 @@
{
FILE *fp;
HistEvent ev;
- int i = 0, retval;
+ int i = -1, retval;
size_t len, max_size;
char *ptr;
if ((fp = fopen(fname, "w")) == NULL)
return (-1);
- (void) fchmod(fileno(fp), S_IRUSR|S_IWUSR);
- (void) fputs(hist_cookie, fp);
+ if (fchmod(fileno(fp), S_IRUSR|S_IWUSR) == -1)
+ goto done;
+ if (fputs(hist_cookie, fp) == EOF)
+ goto done;
ptr = h_malloc(max_size = 1024);
- for (retval = HLAST(h, &ev);
+ if (ptr == NULL)
+ goto done;
+ for (i = 0, retval = HLAST(h, &ev);
retval != -1;
retval = HPREV(h, &ev), i++) {
len = strlen(ev.str) * 4;
if (len >= max_size) {
+ char *nptr;
max_size = (len + 1023) & 1023;
- ptr = h_realloc(ptr, max_size);
+ nptr = h_realloc(ptr, max_size);
+ if (nptr == NULL) {
+ i = -1;
+ goto oomem;
+ }
+ ptr = nptr;
}
(void) strvis(ptr, ev.str, VIS_WHITE);
(void) fprintf(fp, "%s\n", ptr);
}
- h_free(ptr);
+oomem:
+ h_free((ptr_t)ptr);
+done:
(void) fclose(fp);
return (i);
}
diff -r d3f86772e2bb -r d5bc609bbcc8 lib/libedit/readline.c
--- a/lib/libedit/readline.c Sun Oct 27 20:16:41 2002 +0000
+++ b/lib/libedit/readline.c Sun Oct 27 20:24:28 2002 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: readline.c,v 1.22 2002/04/09 01:57:34 thorpej Exp $ */
+/* $NetBSD: readline.c,v 1.23 2002/10/27 20:24:28 christos Exp $ */
/*-
* Copyright (c) 1997 The NetBSD Foundation, Inc.
@@ -38,7 +38,7 @@
#include "config.h"
#if !defined(lint) && !defined(SCCSID)
-__RCSID("$NetBSD: readline.c,v 1.22 2002/04/09 01:57:34 thorpej Exp $");
+__RCSID("$NetBSD: readline.c,v 1.23 2002/10/27 20:24:28 christos Exp $");
#endif /* not lint && not SCCSID */
#include <sys/types.h>
@@ -219,6 +219,11 @@
/* for proper prompt printing in readline() */
el_rl_prompt = strdup("");
+ if (el_rl_prompt == NULL) {
+ history_end(h);
+ el_end(e);
+ return -1;
+ }
el_set(e, EL_PROMPT, _get_prompt);
el_set(e, EL_SIGNAL, 1);
@@ -283,6 +288,8 @@
if (strcmp(el_rl_prompt, prompt) != 0) {
free(el_rl_prompt);
el_rl_prompt = strdup(prompt);
+ if (el_rl_prompt == NULL)
+ return NULL;
}
/* get one line from input stream */
ret = el_gets(e, &count);
@@ -291,6 +298,8 @@
int lastidx;
buf = strdup(ret);
+ if (buf == NULL)
+ return NULL;
lastidx = count - 1;
if (buf[lastidx] == '\n')
buf[lastidx] = '\0';
@@ -334,6 +343,8 @@
size_t size, i;
result = malloc((size = 16));
+ if (result == NULL)
+ return NULL;
temp = str;
with_len = strlen(with);
what_len = strlen(what);
@@ -344,8 +355,14 @@
i = new - temp;
add = i + with_len;
if (i + add + 1 >= size) {
+ char *nresult;
size += add + 1;
- result = realloc(result, size);
+ nresult = realloc(result, size);
+ if (nresult == NULL) {
+ free(result);
+ return NULL;
+ }
+ result = nresult;
}
(void) strncpy(&result[len], temp, i);
len += i;
@@ -355,8 +372,14 @@
} else {
add = strlen(temp);
if (len + add + 1 >= size) {
+ char *nresult;
size += add + 1;
- result = realloc(result, size);
+ nresult = realloc(result, size);
+ if (nresult == NULL) {
+ free(result);
+ return NULL;
+ }
+ result = nresult;
}
(void) strcpy(&result[len], temp); /* safe */
len += add;
@@ -499,6 +522,8 @@
cmd++;
line = strdup(event_data);
+ if (line == NULL)
+ return NULL;
for (; *cmd; cmd++) {
if (*cmd == ':')
continue;
@@ -525,23 +550,36 @@
delim = *(++cmd), cmd++;
size = 16;
what = realloc(from, size);
+ if (what == NULL) {
+ free(from);
+ return NULL;
Home |
Main Index |
Thread Index |
Old Index