summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authormillert <millert@openbsd.org>2004-04-08 15:18:28 +0000
committermillert <millert@openbsd.org>2004-04-08 15:18:28 +0000
commit0343aed44140a8fdddad51c6cb7831122e1999ad (patch)
tree543d4485d3f9e430bd2ad920d0cb592de21750d8
parentsigh, really fix the error message this time, thanks Moritz Jodeit (diff)
downloadwireguard-openbsd-0343aed44140a8fdddad51c6cb7831122e1999ad.tar.xz
wireguard-openbsd-0343aed44140a8fdddad51c6cb7831122e1999ad.zip
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@
-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;