diff options
author | Jeremie Courreges-Anglas <jca@cvs.openbsd.org> | 2017-09-07 19:08:33 +0000 |
---|---|---|
committer | Jeremie Courreges-Anglas <jca@cvs.openbsd.org> | 2017-09-07 19:08:33 +0000 |
commit | 45e6991aad5b4b65cdeb797fa723b768a5a51708 (patch) | |
tree | 77420a6c63ae4baa7d78f96a3bbe70df333b9d03 /bin | |
parent | e58a768f13ea703f65242c56b4359f666ad040e9 (diff) |
Keep histptr inside of the allocated history array
histptr == history -1 means that the history is empty, but the behavior
is then undefined. Allocate an extra array entry to work around this.
Input & ok tb@
Diffstat (limited to 'bin')
-rw-r--r-- | bin/ksh/history.c | 16 |
1 files changed, 12 insertions, 4 deletions
diff --git a/bin/ksh/history.c b/bin/ksh/history.c index 8d9c7fcecd0..44b1c1784da 100644 --- a/bin/ksh/history.c +++ b/bin/ksh/history.c @@ -1,4 +1,4 @@ -/* $OpenBSD: history.c,v 1.70 2017/08/31 11:10:03 jca Exp $ */ +/* $OpenBSD: history.c,v 1.71 2017/09/07 19:08:32 jca Exp $ */ /* * command history @@ -39,6 +39,7 @@ static char **hist_get_oldest(void); static void histbackup(void); static FILE *histfh; +static char **histbase; /* actual start of the history[] allocation */ static char **current; /* current position in history[] */ static char *hname; /* current name of history file */ static int hstarted; /* set after hist_init() called */ @@ -557,8 +558,9 @@ sethistsize(int n) memmove(history, histptr - offset, n * sizeof(char *)); } - history = areallocarray(history, n, sizeof(char *), APERM); histsize = n; + histbase = areallocarray(histbase, n + 1, sizeof(char *), APERM); + history = histbase + 1; histptr = history + offset; } } @@ -597,9 +599,15 @@ sethistfile(const char *name) void init_histvec(void) { - if (history == NULL) { + if (histbase == NULL) { histsize = HISTORYSIZE; - history = areallocarray(NULL, histsize, sizeof(char *), APERM); + /* + * allocate one extra element so that histptr always + * lies within array bounds + */ + histbase = areallocarray(NULL, histsize + 1, sizeof(char *), + APERM); + history = histbase + 1; histptr = history - 1; } } |