apps/nshlib: Fix all places where cle() and readline() are used. readline() returns EOF on a failure. cle() returns a negated errno value. Checking only for EOF causes failues to be missed (and infinite loops ensuing).

This commit is contained in:
Valmantas Paliksa 2019-05-21 08:43:09 -06:00 committed by Gregory Nutt
parent 891ea23356
commit 5cad41c973
5 changed files with 128 additions and 80 deletions

View File

@ -1,7 +1,7 @@
/****************************************************************************
* apps/nshlib/nsh_login.c
*
* Copyright (C) 2016 Gregory Nutt. All rights reserved.
* Copyright (C) 2016, 2019 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
@ -171,16 +171,21 @@ int nsh_login(FAR struct console_stdio_s *pstate)
fputs(g_userprompt, pstate->cn_outstream);
fflush(pstate->cn_outstream);
username[0] = '\0';
#ifdef CONFIG_NSH_CLE
/* cle() returns a negated errno value on failure */
ret = cle(pstate->cn_line, CONFIG_NSH_LINELEN,
INSTREAM(pstate), OUTSTREAM(pstate));
if (ret >= 0)
#else
/* readline() returns EOF on failure */
ret = readline(pstate->cn_line, CONFIG_NSH_LINELEN,
INSTREAM(pstate), OUTSTREAM(pstate));
#endif
username[0] = '\0';
if (ret != EOF)
#endif
{
/* Parse out the username */

View File

@ -1,7 +1,8 @@
/****************************************************************************
* apps/nshlib/nsh_session.c
*
* Copyright (C) 2007-2009, 2011-2014, 2016 Gregory Nutt. All rights reserved.
* Copyright (C) 2007-2009, 2011-2014, 2016, 2019 Gregory Nutt. All rights
* reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
@ -142,31 +143,43 @@ int nsh_session(FAR struct console_stdio_s *pstate)
*/
#ifdef CONFIG_NSH_CLE
ret = cle(pstate->cn_line, CONFIG_NSH_LINELEN,
INSTREAM(pstate), OUTSTREAM(pstate));
#else
ret = readline(pstate->cn_line, CONFIG_NSH_LINELEN,
INSTREAM(pstate), OUTSTREAM(pstate));
#endif
if (ret != EOF)
{
/* Parse process the command */
(void)nsh_parse(vtbl, pstate->cn_line);
fflush(pstate->cn_outstream);
}
/* Readline normally returns the number of characters read,
* but will return EOF on end of file or if an error occurs.
* EOF will cause the session to terminate.
/* cle() normally returns the number of characters read, but will
* return a negated errno value on end of file or if an error occurs.
* Either will cause the session to terminate.
*/
else
ret = cle(pstate->cn_line, CONFIG_NSH_LINELEN,
INSTREAM(pstate), OUTSTREAM(pstate));
if (ret < 0)
{
fprintf(pstate->cn_errstream, g_fmtcmdfailed, "nsh_session",
"readline", NSH_ERRNO_OF(-ret));
"cle", NSH_ERRNO_OF(-ret));
return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}
#else
/* readline() normally returns the number of characters read, but will
* return EOF on end of file or if an error occurs. EOF
* will cause the session to terminate.
*/
ret = readline(pstate->cn_line, CONFIG_NSH_LINELEN,
INSTREAM(pstate), OUTSTREAM(pstate));
if (ret == EOF)
{
/* NOTE: readline() does not set the errno variable, but perhaps we
* will be lucky and it will still be valid.
*/
fprintf(pstate->cn_errstream, g_fmtcmdfailed, "nsh_session",
"readline", NSH_ERRNO);
return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}
#endif
/* Parse process the command */
(void)nsh_parse(vtbl, pstate->cn_line);
fflush(pstate->cn_outstream);
}
/* We do not get here, but this is necessary to keep some compilers happy.

View File

@ -1,7 +1,7 @@
/****************************************************************************
* apps/nshlib/nsh_stdlogin.c
*
* Copyright (C) 2016 Gregory Nutt. All rights reserved.
* Copyright (C) 2016, 2019 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
@ -170,17 +170,22 @@ int nsh_stdlogin(FAR struct console_stdio_s *pstate)
printf("%s", g_userprompt);
/* Get the reponse, handling all possible cases */
#ifdef CONFIG_NSH_CLE
ret = cle(pstate->cn_line, CONFIG_NSH_LINELEN,
stdin, stdout);
#else
ret = std_readline(pstate->cn_line, CONFIG_NSH_LINELEN);
#endif
/* Get the response, handling all possible cases */
username[0] = '\0';
#ifdef CONFIG_NSH_CLE
/* cle() returns a negated errno value on failure */
ret = cle(pstate->cn_line, CONFIG_NSH_LINELEN,
stdin, stdout);
if (ret >= 0)
#else
/* readline() returns EOF on failure */
ret = std_readline(pstate->cn_line, CONFIG_NSH_LINELEN);
if (ret != EOF)
#endif
{
/* Parse out the username */

View File

@ -1,7 +1,7 @@
/****************************************************************************
* apps/nshlib/nsh_stdsession.c
*
* Copyright (C) 2013-2014, 2016 Gregory Nutt. All rights reserved.
* Copyright (C) 2013-2014, 2016, 2019 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
@ -127,33 +127,43 @@ int nsh_session(FAR struct console_stdio_s *pstate)
printf("%s", g_nshprompt);
/* Get the next line of input. readline() returns EOF on end-of-file
* or any read failure.
*/
/* Get the next line of input. */
#ifdef CONFIG_NSH_CLE
ret = cle(pstate->cn_line, CONFIG_NSH_LINELEN,
stdin, stdout);
#else
ret = std_readline(pstate->cn_line, CONFIG_NSH_LINELEN);
#endif
if (ret != EOF)
{
/* Parse process the command */
(void)nsh_parse(vtbl, pstate->cn_line);
}
/* Readline normally returns the number of characters read,
* but will return EOF on end of file or if an error occurs.
* EOF will cause the session to terminate.
/* cle() normally returns the number of characters read, but will
* return a negated errno value on end of file or if an error occurs.
* Either will cause the session to terminate.
*/
else
ret = cle(pstate->cn_line, CONFIG_NSH_LINELEN,
stdin, stdout);
if (ret < 0)
{
printf(g_fmtcmdfailed, "nsh_session", "readline", NSH_ERRNO_OF(-ret));
printf(g_fmtcmdfailed, "nsh_session", "cle", NSH_ERRNO_OF(-ret));
return EXIT_SUCCESS;
}
#else
/* readline () normally returns the number of characters read, but will
* return EOF on end of file or if an error occurs. Either will cause
* the session to terminate.
*/
ret = std_readline(pstate->cn_line, CONFIG_NSH_LINELEN);
if (ret == EOF)
{
/* NOTE: readline() does not set the errno variable, but perhaps we
* will be lucky and it will still be valid.
*/
printf(g_fmtcmdfailed, "nsh_session", "readline", NSH_ERRNO);
return EXIT_SUCCESS;
}
#endif
/* Parse process the command */
(void)nsh_parse(vtbl, pstate->cn_line);
}
/* We do not get here, but this is necessary to keep some compilers happy.

View File

@ -1,7 +1,8 @@
/****************************************************************************
* apps/nshlib/nsh_telnetd.c
*
* Copyright (C) 2007-2013, 2016-2017 Gregory Nutt. All rights reserved.
* Copyright (C) 2007-2013, 2016-2017, 2019 Gregory Nutt. All rights
* reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
@ -151,39 +152,53 @@ static int nsh_telnetmain(int argc, char *argv[])
fflush(pstate->cn_outstream);
/* Get the next line of input from the Telnet client */
#ifdef CONFIG_TELNET_CHARACTER_MODE
#ifdef CONFIG_NSH_CLE
/* cle() returns a negated errno value on failure (errno is not set) */
ret = cle(pstate->cn_line, CONFIG_NSH_LINELEN,
INSTREAM(pstate), OUTSTREAM(pstate));
#else
ret = readline(pstate->cn_line, CONFIG_NSH_LINELEN,
INSTREAM(pstate), OUTSTREAM(pstate));
#endif
#else
if (fgets(pstate->cn_line, CONFIG_NSH_LINELEN,
INSTREAM(pstate)) != NULL)
{
ret = strlen(pstate->cn_line);
}
else
{
ret = EOF;
}
#endif
if (ret != EOF)
{
/* Parse process the received Telnet command */
(void)nsh_parse(vtbl, pstate->cn_line);
fflush(pstate->cn_outstream);
}
else
if (ret < 0)
{
fprintf(pstate->cn_errstream, g_fmtcmdfailed, "nsh_telnetmain",
"cle/readline/fgets", NSH_ERRNO);
"cle", NSH_ERRNO_OF(-ret));
nsh_exit(vtbl, 1);
}
#else
/* readline() returns EOF on failure (errno is not set) */
ret = readline(pstate->cn_line, CONFIG_NSH_LINELEN,
INSTREAM(pstate), OUTSTREAM(pstate));
if (ret == EOF)
{
/* NOTE: readline() does not set the errno variable, but perhaps we
* will be lucky and it will still be valid.
*/
fprintf(pstate->cn_errstream, g_fmtcmdfailed, "nsh_telnetmain",
"readline", NSH_ERRNO);
nsh_exit(vtbl, 1);
}
#endif
#else
/* fgets() returns NULL on failure (errno will be set) */
if (fgets(pstate->cn_line, CONFIG_NSH_LINELEN,
INSTREAM(pstate)) == NULL)
{
fprintf(pstate->cn_errstream, g_fmtcmdfailed, "nsh_telnetmain",
"fgets", NSH_ERRNO);
nsh_exit(vtbl, 1);
}
ret = strlen(pstate->cn_line);
#endif
/* Parse process the received Telnet command */
(void)nsh_parse(vtbl, pstate->cn_line);
fflush(pstate->cn_outstream);
}
/* Clean up */