summaryrefslogtreecommitdiff
path: root/lib/libedit/hist.c
diff options
context:
space:
mode:
authorchristos <christos@NetBSD.org>2017-03-05 17:30:38 +0000
committerchristos <christos@NetBSD.org>2017-03-05 17:30:38 +0000
commit47ddee1acdfd2f06f42f32cdddbcddaac07e0a95 (patch)
treeee045bc5fe972f7335cd9de551a4182767e8962a /lib/libedit/hist.c
parent7092728e7eaa955ac3ab3e91abb016c28efcfb10 (diff)
Grow the buffer for event search if there was not enough space.
From Gerry Swislow
Diffstat (limited to 'lib/libedit/hist.c')
-rw-r--r--lib/libedit/hist.c27
1 files changed, 17 insertions, 10 deletions
diff --git a/lib/libedit/hist.c b/lib/libedit/hist.c
index 4f01ade243e..d4e8c707add 100644
--- a/lib/libedit/hist.c
+++ b/lib/libedit/hist.c
@@ -1,4 +1,4 @@
-/* $NetBSD: hist.c,v 1.30 2016/11/07 15:30:18 christos Exp $ */
+/* $NetBSD: hist.c,v 1.31 2017/03/05 17:30:38 christos Exp $ */
/*-
* Copyright (c) 1992, 1993
@@ -37,7 +37,7 @@
#if 0
static char sccsid[] = "@(#)hist.c 8.1 (Berkeley) 6/4/93";
#else
-__RCSID("$NetBSD: hist.c,v 1.30 2016/11/07 15:30:18 christos Exp $");
+__RCSID("$NetBSD: hist.c,v 1.31 2017/03/05 17:30:38 christos Exp $");
#endif
#endif /* not lint && not SCCSID */
@@ -102,6 +102,7 @@ hist_get(EditLine *el)
{
const wchar_t *hp;
int h;
+ size_t blen, hlen;
if (el->el_history.eventno == 0) { /* if really the current line */
(void) wcsncpy(el->el_line.buffer, el->el_history.buf,
@@ -127,14 +128,16 @@ hist_get(EditLine *el)
return CC_ERROR;
for (h = 1; h < el->el_history.eventno; h++)
- if ((hp = HIST_NEXT(el)) == NULL) {
- el->el_history.eventno = h;
- return CC_ERROR;
- }
- (void) wcsncpy(el->el_line.buffer, hp,
- (size_t)(el->el_line.limit - el->el_line.buffer));
- el->el_line.buffer[el->el_line.limit - el->el_line.buffer - 1] = '\0';
- el->el_line.lastchar = el->el_line.buffer + wcslen(el->el_line.buffer);
+ if ((hp = HIST_NEXT(el)) == NULL)
+ goto out;
+
+ hlen = wcslen(hp);
+ blen = (size_t)(el->el_line.limit - el->el_line.buffer);
+ if (hlen > blen && !ch_enlargebufs(el, hlen))
+ goto out;
+
+ memcpy(el->el_line.buffer, hp, hlen * sizeof(*hp));
+ el->el_line.lastchar = el->el_line.buffer + hlen - 1;
if (el->el_line.lastchar > el->el_line.buffer
&& el->el_line.lastchar[-1] == '\n')
@@ -150,6 +153,10 @@ hist_get(EditLine *el)
el->el_line.cursor = el->el_line.lastchar;
return CC_REFRESH;
+out:
+ el->el_history.eventno = h;
+ return CC_ERROR;
+
}