summaryrefslogtreecommitdiff
path: root/usr.bin/less/line.c
diff options
context:
space:
mode:
authorTodd C. Miller <millert@cvs.openbsd.org>2004-04-08 15:18:29 +0000
committerTodd C. Miller <millert@cvs.openbsd.org>2004-04-08 15:18:29 +0000
commit00b51a3cf5847b1c1d23c2bdc068c1a1c8557021 (patch)
tree9cc7c7a4d339c22a626bd7de73b913b25e8d3c65 /usr.bin/less/line.c
parentb42a153f2ead9d9ec3b0c08fbf863771074b7622 (diff)
When reallocating the line buffer, double the existing buffer
size instead of just incrementing it by LINEBUF_SIZE and use realloc() + memset() instead of calloc() + memcpy(). Prevents excessive resource usage when displaying very long lines. OK deraadt@, krw@ and otto@
Diffstat (limited to 'usr.bin/less/line.c')
-rw-r--r--usr.bin/less/line.c12
1 files changed, 5 insertions, 7 deletions
diff --git a/usr.bin/less/line.c b/usr.bin/less/line.c
index bb9caa6e18a..07858120eb0 100644
--- a/usr.bin/less/line.c
+++ b/usr.bin/less/line.c
@@ -79,9 +79,9 @@ init_line()
static int
expand_linebuf()
{
- int new_size = size_linebuf + LINEBUF_SIZE;
- char *new_buf = (char *) calloc(new_size, sizeof(char));
- char *new_attr = (char *) calloc(new_size, sizeof(char));
+ int new_size = size_linebuf * 2;
+ char *new_buf = (char *) realloc(linebuf, new_size);
+ char *new_attr = (char *) realloc(attr, new_size);
if (new_buf == NULL || new_attr == NULL)
{
if (new_attr != NULL)
@@ -90,10 +90,8 @@ expand_linebuf()
free(new_buf);
return 1;
}
- memcpy(new_buf, linebuf, size_linebuf * sizeof(char));
- memcpy(new_attr, attr, size_linebuf * sizeof(char));
- free(attr);
- free(linebuf);
+ memset(new_buf + size_linebuf, 0, new_size - size_linebuf);
+ memset(new_attr + size_linebuf, 0, new_size - size_linebuf);
linebuf = new_buf;
attr = new_attr;
size_linebuf = new_size;