I've noticed this some time ago and, while it doesn't seem to be a problem in practice, it's still undefined. C says what happens with pointers: - within the bounds of an array - one past the last element of an array but nothing about a pointer one element before the first element of an array.
Thoughts? Index: history.c =================================================================== RCS file: /d/cvs/src/bin/ksh/history.c,v retrieving revision 1.69 diff -u -p -p -u -r1.69 history.c --- history.c 30 Aug 2017 17:08:45 -0000 1.69 +++ history.c 31 Aug 2017 12:01:48 -0000 @@ -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 + * lays within array bounds + */ + histbase = areallocarray(NULL, histsize + 1, sizeof(char *), + APERM); + history = histbase + 1; histptr = history - 1; } } -- jca | PGP : 0x1524E7EE / 5135 92C1 AD36 5293 2BDF DDCC 0DFA 74AE 1524 E7EE