diff options
author | Jérémie Courrèges-Anglas <jca@wxcvbn.org> | 2014-01-03 23:28:19 +0100 |
---|---|---|
committer | Jérémie Courrèges-Anglas <jca@wxcvbn.org> | 2014-01-03 23:28:19 +0100 |
commit | eb4d8278e5828217bcc5b8045147544c62c28372 (patch) | |
tree | 58f875f1ae4002d91ef33ec3fcfe9b4616028c10 | |
parent | 3efe2d8254fe4cd5aecb56eb0f0861bd043b6782 (diff) | |
download | ratpoison-eb4d8278e5828217bcc5b8045147544c62c28372.zip |
Make our fallback getline more compliant.
* don't use xmalloc/xrealloc, but return -1 in case of memory allocation
failure
* modify the parameters only when allocation succeeds
-rw-r--r-- | src/history.c | 14 |
1 files changed, 10 insertions, 4 deletions
diff --git a/src/history.c b/src/history.c index 7a6cf15..10496d2 100644 --- a/src/history.c +++ b/src/history.c @@ -75,12 +75,14 @@ static struct history { #ifndef HAVE_GETLINE ssize_t -getline(char **lineptr, size_t *n, FILE *f) +getline (char **lineptr, size_t *n, FILE *f) { size_t ofs; - if (!*lineptr) { - *lineptr = xmalloc (4096); + if (*lineptr == NULL) { + *lineptr = malloc (4096); + if (*lineptr == NULL) + return -1; *n = 4096; } ofs = 0; @@ -99,10 +101,14 @@ getline(char **lineptr, size_t *n, FILE *f) if (ofs > 0 && (*lineptr)[ofs-1] == '\n') return ofs; if (ofs + 1 == *n) { + void *tmp; if (*n >= INT_MAX - 4096) return -1; + tmp = realloc (*lineptr, *n + 4096); + if (tmp == NULL) + return -1; + *lineptr = tmp; *n += 4096; - *lineptr = xrealloc(*lineptr, *n); } } while(1); } |