From 48acc0cd387f90390ba2a7a6435e02b2f4866336 Mon Sep 17 00:00:00 2001 From: Travis Cross Date: Thu, 22 Sep 2011 06:35:24 +0000 Subject: [PATCH] fs_cli: do not assume that LineInfo buffers are null-terminated man editline(3el) says that they're not. --- libs/esl/fs_cli.c | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/libs/esl/fs_cli.c b/libs/esl/fs_cli.c index f1b8ba38ce..832d89e048 100644 --- a/libs/esl/fs_cli.c +++ b/libs/esl/fs_cli.c @@ -797,17 +797,26 @@ static void set_fn_keys(cli_profile_t *profile) static char* end_of_str(char *s) { return (*s == '\0' ? s : s + strlen(s) - 1); } -static unsigned char esl_console_complete(const char *buffer, const char *cursor) +static char* _strndup(const char *s, int n) +{ + char *d = (char*)malloc(n + 1); + while (*s && n>0) { + *d = *s; + d++; s++; + } + *d = 0; + return d; +} + +static unsigned char esl_console_complete(const char *buffer, const char *cursor, const char *lastchar) { char cmd_str[2048] = ""; unsigned char ret = CC_REDISPLAY; - char *dup = strdup(buffer); + int buflen = (lastchar - buffer); + char *dup = _strndup(buffer, buflen); char *buf = dup; - int pos = 0, sc = 0; + int sc = 0, offset = (cursor - buffer), pos = (offset > 0) ? offset : 0; char *p; - if (!esl_strlen_zero(cursor) && !esl_strlen_zero(buffer)) { - pos = (int)(cursor - buffer); - } if (pos > 0) { *(buf + pos) = '\0'; } @@ -880,7 +889,7 @@ static unsigned char esl_console_complete(const char *buffer, const char *cursor static unsigned char complete(EditLine *el, int ch) { const LineInfo *lf = el_line(el); - return esl_console_complete(lf->buffer, lf->cursor); + return esl_console_complete(lf->buffer, lf->cursor, lf->lastchar); } #endif @@ -1250,9 +1259,9 @@ int main(int argc, char *argv[]) #ifdef HAVE_EDITLINE { const LineInfo *lf = el_line(el); - char *s = (char *) lf->buffer; - el_deletestr(el, strlen(s) + 1); - memset(s, 0, strlen(s)); + int len = (lf->lastchar - lf->buffer); + el_deletestr(el, len + 1); + memset((char*)lf->buffer, 0, len); } #endif }