xhh233 commented on issue #4107:
URL: https://github.com/apache/nuttx-apps/issues/4107#issuecomment-7075482041
Patch (applies to master 7196658d; git apply --check rc=0):
```diff
--- a/system/readline/readline_common.c
+++ b/system/readline/readline_common.c
@@ -1275,6 +1275,7 @@
escape = 0;
nch = 0;
+ bool overflow = false;
#ifdef CONFIG_READLINE_EDIT
cursor = 0;
#endif
@@ -1297,6 +1298,15 @@
{
/* Did we already received some data? */
+ if (overflow)
+ {
+ /* Discard a truncated overlong line on disconnect. */
+
+ overflow = false;
+ buf[0] = '\0';
+ return 0;
+ }
+
if (nch > 0)
{
/* Yes.. Terminate the line (which might be zero length)
@@ -1310,6 +1320,26 @@
return EOF;
}
+ else if (overflow)
+ {
+ /* An overlong line is being discarded. Never submit its
+ * truncated prefix; consume bytes through the next EOL and
+ * return an empty line so the caller cannot execute a partial
+ * command (e.g. "poweroff <200 bytes>").
+ */
+
+ if (ch == '\n' || ch == '\r')
+ {
+ overflow = false;
+ buf[0] = '\0';
+#ifdef CONFIG_READLINE_ECHO
+ RL_PUTC(vtbl, '\n');
+#endif
+ return 0;
+ }
+
+ continue;
+ }
#ifdef CONFIG_READLINE_EDIT_EMACS_REVERSE_SEARCH
/* Are we in reverse incremental search mode (Ctrl+R)? If so,
@@ -1995,19 +2025,32 @@
}
# endif
}
-#else
- buf[nch++] = ch;
-#endif /* CONFIG_READLINE_EDIT */
-
- /* Check if there is room for another character and the line's
- * null terminator. If not then we have to end the line now.
- */
+ else
+ {
+ /* No room for this character plus the null terminator.
+ * Keep the already-full line intact so a line that exactly
+ * fills the buffer can still be submitted, but mark the
+ * overflow so later characters are discarded through EOL.
+ */
- if (nch + 1 >= buflen)
+ overflow = true;
+#ifdef CONFIG_READLINE_ECHO
+ RL_PUTC(vtbl, '\a');
+#endif
+ }
+#else
+ if (nch + 1 < buflen)
{
- buf[nch] = '\0';
- return nch;
+ buf[nch++] = ch;
}
+ else
+ {
+ overflow = true;
+#ifdef CONFIG_READLINE_ECHO
+ RL_PUTC(vtbl, '\a');
+#endif
+ }
+#endif /* CONFIG_READLINE_EDIT */
}
#ifdef CONFIG_READLINE_TABCOMPLETION
else if (ch == '\t') /* TAB character */
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]