Module Name: src
Committed By: christos
Date: Sun Mar 5 17:30:38 UTC 2017
Modified Files:
src/lib/libedit: hist.c
Log Message:
Grow the buffer for event search if there was not enough space.
>From Gerry Swislow
To generate a diff of this commit:
cvs rdiff -u -r1.30 -r1.31 src/lib/libedit/hist.c
Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.
Modified files:
Index: src/lib/libedit/hist.c
diff -u src/lib/libedit/hist.c:1.30 src/lib/libedit/hist.c:1.31
--- src/lib/libedit/hist.c:1.30 Mon Nov 7 10:30:18 2016
+++ src/lib/libedit/hist.c Sun Mar 5 12:30:38 2017
@@ -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;
+
}