fs_cli: do not assume that LineInfo buffers are null-terminated

man editline(3el) says that they're not.
This commit is contained in:
Travis Cross 2011-09-22 06:35:24 +00:00
parent e87af9011a
commit 48acc0cd38
1 changed files with 19 additions and 10 deletions

View File

@ -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
}