summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJérémie Courrèges-Anglas <jca@wxcvbn.org>2014-01-03 23:28:19 +0100
committerJérémie Courrèges-Anglas <jca@wxcvbn.org>2014-01-03 23:28:19 +0100
commiteb4d8278e5828217bcc5b8045147544c62c28372 (patch)
tree58f875f1ae4002d91ef33ec3fcfe9b4616028c10
parent3efe2d8254fe4cd5aecb56eb0f0861bd043b6782 (diff)
downloadratpoison-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.c14
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);
}