]> Sergey Matveev's repositories - nnn.git/commitdiff
Limit imput length at readline prompt
authorArun Prakash Jana <engineerarun@gmail.com>
Sat, 5 Oct 2019 01:47:01 +0000 (07:17 +0530)
committerArun Prakash Jana <engineerarun@gmail.com>
Sat, 5 Oct 2019 01:47:01 +0000 (07:17 +0530)
src/nnn.c

index bcb500f38ae5028dbc5e4190c400c9e04b17a9a6..35a2a072ed8a2463eea7cd3359b4a7585642358f 100644 (file)
--- a/src/nnn.c
+++ b/src/nnn.c
 #define TOUPPER(ch) \
        (((ch) >= 'a' && (ch) <= 'z') ? ((ch) - 'a' + 'A') : (ch))
 #define CMD_LEN_MAX (PATH_MAX + ((NAME_MAX + 1) << 1))
+#define READLINE_MAX 128
 #define FILTER '/'
 #define MSGWAIT '$'
 #define REGEX_MAX 48
@@ -1818,7 +1819,7 @@ static char *xreadline(char *prefill, char *prompt)
        int x, r;
        const int WCHAR_T_WIDTH = sizeof(wchar_t);
        wint_t ch[2] = {0};
-       wchar_t * const buf = malloc(sizeof(wchar_t) * CMD_LEN_MAX);
+       wchar_t * const buf = malloc(sizeof(wchar_t) * READLINE_MAX);
 
        if (!buf)
                errexit();
@@ -1828,7 +1829,7 @@ static char *xreadline(char *prefill, char *prompt)
 
        if (prefill) {
                DPRINTF_S(prefill);
-               len = pos = mbstowcs(buf, prefill, CMD_LEN_MAX);
+               len = pos = mbstowcs(buf, prefill, READLINE_MAX);
        } else
                len = (size_t)-1;
 
@@ -1887,7 +1888,7 @@ static char *xreadline(char *prefill, char *prompt)
                                if (*ch < ASCII_MAX && keyname(*ch)[0] == '^')
                                        continue;
 
-                               if (pos < CMD_LEN_MAX - 1) {
+                               if (pos < READLINE_MAX - 1) {
                                        memmove(buf + pos + 1, buf + pos,
                                                (len - pos) * WCHAR_T_WIDTH);
                                        buf[pos] = *ch;
@@ -1945,9 +1946,9 @@ END:
 
        buf[len] = '\0';
 
-       pos = wcstombs(g_buf, buf, CMD_LEN_MAX - 1);
-       if (pos >= CMD_LEN_MAX - 1)
-               g_buf[CMD_LEN_MAX - 1] = '\0';
+       pos = wcstombs(g_buf, buf, READLINE_MAX - 1);
+       if (pos >= READLINE_MAX - 1)
+               g_buf[READLINE_MAX - 1] = '\0';
 
        free(buf);
        return g_buf;