mirror of
https://github.com/apache/nuttx-apps.git
synced 2026-08-01 20:29:00 +00:00
apps: Fix unchecked strdup()/asprintf() as requested in #1727
Add missing failure handling for direct strdup() and asprintf() calls where the allocated result is consumed locally before any NULL/error check. This keeps the scope to the functions named in #1727 and avoids changing pass-through return sites where callers already receive NULL on allocation failure. Signed-off-by: Nightt <87569709+nightt5879@users.noreply.github.com>
This commit is contained in:
parent
58b780c280
commit
c44f9a0556
12 changed files with 134 additions and 20 deletions
|
|
@ -6,3 +6,5 @@
|
||||||
object = new Controlse::CCertificate(
|
object = new Controlse::CCertificate(
|
||||||
Controlse::CCertificate cert(se, settings->key_id);
|
Controlse::CCertificate cert(se, settings->key_id);
|
||||||
auto certificate = Controlse::CCertificate(
|
auto certificate = Controlse::CCertificate(
|
||||||
|
* |---------- [-rw-r--r-- 15] afile.txt
|
||||||
|
g_afile.name = "afile.txt";
|
||||||
|
|
|
||||||
|
|
@ -217,6 +217,13 @@ static void show_directories(const char *path, int indent)
|
||||||
snprintf(g_namebuffer, sizeof(g_namebuffer),
|
snprintf(g_namebuffer, sizeof(g_namebuffer),
|
||||||
"%s/%s", path, direntry->d_name);
|
"%s/%s", path, direntry->d_name);
|
||||||
subdir = strdup(g_namebuffer);
|
subdir = strdup(g_namebuffer);
|
||||||
|
if (subdir == NULL)
|
||||||
|
{
|
||||||
|
printf("show_directories: ERROR out of memory\n");
|
||||||
|
g_nerrors++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
show_directories(subdir, indent + 1);
|
show_directories(subdir, indent + 1);
|
||||||
free(subdir);
|
free(subdir);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -394,6 +394,12 @@ static void readdirectories(const char *path, struct node_s *entry)
|
||||||
snprintf(g_scratchbuffer, sizeof(g_scratchbuffer),
|
snprintf(g_scratchbuffer, sizeof(g_scratchbuffer),
|
||||||
"%s/%s", path, direntry->d_name);
|
"%s/%s", path, direntry->d_name);
|
||||||
fullpath = strdup(g_scratchbuffer);
|
fullpath = strdup(g_scratchbuffer);
|
||||||
|
if (fullpath == NULL)
|
||||||
|
{
|
||||||
|
printf(" ERROR: Out of memory\n");
|
||||||
|
g_nerrors++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
if (DIRENT_ISDIRECTORY(direntry->d_type))
|
if (DIRENT_ISDIRECTORY(direntry->d_type))
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -108,6 +108,8 @@ static int chat_tokenise(FAR struct chat *priv,
|
||||||
|
|
||||||
int tok_on_delimiter(void)
|
int tok_on_delimiter(void)
|
||||||
{
|
{
|
||||||
|
FAR struct chat_token *newtok;
|
||||||
|
|
||||||
if (!tok_pos && !quoted && !no_termin)
|
if (!tok_pos && !quoted && !no_termin)
|
||||||
{
|
{
|
||||||
/* a) the first character in the script is a delimiter or
|
/* a) the first character in the script is a delimiter or
|
||||||
|
|
@ -122,9 +124,29 @@ static int chat_tokenise(FAR struct chat *priv,
|
||||||
/* Terminate the temporary */
|
/* Terminate the temporary */
|
||||||
|
|
||||||
tok_str[tok_pos] = '\0';
|
tok_str[tok_pos] = '\0';
|
||||||
|
newtok = malloc(sizeof(struct chat_token));
|
||||||
|
if (newtok == NULL)
|
||||||
|
{
|
||||||
|
/* out of memory */
|
||||||
|
|
||||||
|
return -ENOMEM;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Copy the temporary */
|
||||||
|
|
||||||
|
newtok->string = strdup(tok_str);
|
||||||
|
if (newtok->string == NULL)
|
||||||
|
{
|
||||||
|
free(newtok);
|
||||||
|
return -ENOMEM;
|
||||||
|
}
|
||||||
|
|
||||||
|
newtok->no_termin = no_termin;
|
||||||
|
newtok->next = NULL;
|
||||||
|
|
||||||
if (tok)
|
if (tok)
|
||||||
{
|
{
|
||||||
tok->next = malloc(sizeof(struct chat_token));
|
tok->next = newtok;
|
||||||
|
|
||||||
/* The terminated token becomes previous */
|
/* The terminated token becomes previous */
|
||||||
|
|
||||||
|
|
@ -134,26 +156,10 @@ static int chat_tokenise(FAR struct chat *priv,
|
||||||
{
|
{
|
||||||
/* There was no previous token */
|
/* There was no previous token */
|
||||||
|
|
||||||
*first_tok = malloc(sizeof(struct chat_token));
|
*first_tok = newtok;
|
||||||
tok = *first_tok;
|
tok = *first_tok;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!tok)
|
|
||||||
{
|
|
||||||
/* out of memory */
|
|
||||||
|
|
||||||
return -ENOMEM;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Copy the temporary */
|
|
||||||
|
|
||||||
tok->string = strdup(tok_str);
|
|
||||||
tok->no_termin = no_termin;
|
|
||||||
|
|
||||||
/* Initialize the next token */
|
|
||||||
|
|
||||||
tok->next = NULL;
|
|
||||||
|
|
||||||
/* Reset the buffer position */
|
/* Reset the buffer position */
|
||||||
|
|
||||||
tok_pos = 0;
|
tok_pos = 0;
|
||||||
|
|
|
||||||
|
|
@ -108,6 +108,11 @@ SESSION ftpc_connect(FAR union ftpc_sockaddr_u *server)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
session->homeldir = strdup(ftpc_lpwd());
|
session->homeldir = strdup(ftpc_lpwd());
|
||||||
|
if (session->homeldir == NULL)
|
||||||
|
{
|
||||||
|
nerr("ERROR: Failed to allocate local home directory\n");
|
||||||
|
goto errout_with_alloc;
|
||||||
|
}
|
||||||
|
|
||||||
/* And (Re-)connect to the server */
|
/* And (Re-)connect to the server */
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -257,6 +257,7 @@ FAR struct ftpc_dirlist_s *ftpc_listdir(SESSION handle,
|
||||||
FAR char *tmpfname;
|
FAR char *tmpfname;
|
||||||
bool iscurrdir;
|
bool iscurrdir;
|
||||||
unsigned int nnames;
|
unsigned int nnames;
|
||||||
|
unsigned int i;
|
||||||
int allocsize;
|
int allocsize;
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
|
|
@ -365,6 +366,17 @@ FAR struct ftpc_dirlist_s *ftpc_listdir(SESSION handle,
|
||||||
|
|
||||||
ftpc_nlstparse(filestream, ftpc_addname, dirlist);
|
ftpc_nlstparse(filestream, ftpc_addname, dirlist);
|
||||||
DEBUGASSERT(nnames == dirlist->nnames);
|
DEBUGASSERT(nnames == dirlist->nnames);
|
||||||
|
|
||||||
|
for (i = 0; i < dirlist->nnames; i++)
|
||||||
|
{
|
||||||
|
if (dirlist->name[i] == NULL)
|
||||||
|
{
|
||||||
|
nerr("ERROR: Failed to allocate directory name\n");
|
||||||
|
ftpc_dirfree(dirlist);
|
||||||
|
dirlist = NULL;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
errout:
|
errout:
|
||||||
|
|
|
||||||
|
|
@ -172,6 +172,10 @@ int ftpc_relogin(FAR struct ftpc_session_s *session)
|
||||||
if (session->homerdir != NULL)
|
if (session->homerdir != NULL)
|
||||||
{
|
{
|
||||||
session->currdir = strdup(session->homerdir);
|
session->currdir = strdup(session->homerdir);
|
||||||
|
if (session->currdir == NULL)
|
||||||
|
{
|
||||||
|
return -ENOMEM;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* If the user has requested a special start up directory, then change to
|
/* If the user has requested a special start up directory, then change to
|
||||||
|
|
|
||||||
|
|
@ -627,6 +627,11 @@ static bool ftpd_account_login(FAR struct ftpd_session_s *session,
|
||||||
if (account->home != NULL)
|
if (account->home != NULL)
|
||||||
{
|
{
|
||||||
home = strdup(account->home);
|
home = strdup(account->home);
|
||||||
|
if (home == NULL)
|
||||||
|
{
|
||||||
|
ftpd_account_free(account);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
flags = account->flags;
|
flags = account->flags;
|
||||||
|
|
@ -645,6 +650,11 @@ static bool ftpd_account_login(FAR struct ftpd_session_s *session,
|
||||||
{
|
{
|
||||||
home = strdup(home);
|
home = strdup(home);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (home == NULL)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((flags & FTPD_ACCOUNTFLAG_ADMIN) != 0)
|
if ((flags & FTPD_ACCOUNTFLAG_ADMIN) != 0)
|
||||||
|
|
@ -652,6 +662,12 @@ static bool ftpd_account_login(FAR struct ftpd_session_s *session,
|
||||||
/* admin user */
|
/* admin user */
|
||||||
|
|
||||||
session->home = strdup("/");
|
session->home = strdup("/");
|
||||||
|
if (session->home == NULL)
|
||||||
|
{
|
||||||
|
free(home);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
session->work = home;
|
session->work = home;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|
@ -660,6 +676,12 @@ static bool ftpd_account_login(FAR struct ftpd_session_s *session,
|
||||||
|
|
||||||
session->home = home;
|
session->home = home;
|
||||||
session->work = strdup("/");
|
session->work = strdup("/");
|
||||||
|
if (session->work == NULL)
|
||||||
|
{
|
||||||
|
free(home);
|
||||||
|
session->home = NULL;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|
@ -2517,6 +2539,17 @@ static int ftpd_command_user(FAR struct ftpd_session_s *session)
|
||||||
session->loggedin = false;
|
session->loggedin = false;
|
||||||
session->home = strdup(home == NULL ? "/" : home);
|
session->home = strdup(home == NULL ? "/" : home);
|
||||||
session->work = strdup("/");
|
session->work = strdup("/");
|
||||||
|
if (session->home == NULL || session->work == NULL)
|
||||||
|
{
|
||||||
|
free(session->home);
|
||||||
|
free(session->work);
|
||||||
|
session->home = NULL;
|
||||||
|
session->work = NULL;
|
||||||
|
|
||||||
|
return ftpd_response(session->cmd.sd, session->txtimeout,
|
||||||
|
g_respfmt1, 451, ' ',
|
||||||
|
"Memory exhausted !");
|
||||||
|
}
|
||||||
|
|
||||||
return ftpd_response(session->cmd.sd, session->txtimeout,
|
return ftpd_response(session->cmd.sd, session->txtimeout,
|
||||||
g_respfmt1, 230, ' ', "Login successful.");
|
g_respfmt1, 230, ' ', "Login successful.");
|
||||||
|
|
|
||||||
|
|
@ -196,7 +196,11 @@ ssize_t httpd_dirlist(int outfd, FAR struct httpd_fs_file *file)
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = asprintf(&path, "%s/%s", file->path, dent->d_name);
|
ret = asprintf(&path, "%s/%s", file->path, dent->d_name);
|
||||||
ASSERT(ret > 0 && path);
|
if (ret < 0 || path == NULL)
|
||||||
|
{
|
||||||
|
nerr("ERROR: asprintf failed\n");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
/* call stat() to obtain modified time and size */
|
/* call stat() to obtain modified time and size */
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -257,17 +257,38 @@ int cmd_cd(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
|
||||||
else if (strcmp(path, "-") == 0)
|
else if (strcmp(path, "-") == 0)
|
||||||
{
|
{
|
||||||
alloc = strdup(nsh_getwd(g_oldpwd));
|
alloc = strdup(nsh_getwd(g_oldpwd));
|
||||||
|
if (alloc == NULL)
|
||||||
|
{
|
||||||
|
nsh_error(vtbl, g_fmtcmdoutofmemory, argv[0]);
|
||||||
|
ret = ERROR;
|
||||||
|
goto errout;
|
||||||
|
}
|
||||||
|
|
||||||
path = alloc;
|
path = alloc;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
else if (strcmp(path, "..") == 0)
|
else if (strcmp(path, "..") == 0)
|
||||||
{
|
{
|
||||||
alloc = strdup(nsh_getcwd(vtbl));
|
alloc = strdup(nsh_getcwd(vtbl));
|
||||||
|
if (alloc == NULL)
|
||||||
|
{
|
||||||
|
nsh_error(vtbl, g_fmtcmdoutofmemory, argv[0]);
|
||||||
|
ret = ERROR;
|
||||||
|
goto errout;
|
||||||
|
}
|
||||||
|
|
||||||
path = dirname(alloc);
|
path = dirname(alloc);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
fullpath = nsh_getfullpath(vtbl, path);
|
fullpath = nsh_getfullpath(vtbl, path);
|
||||||
|
if (fullpath == NULL)
|
||||||
|
{
|
||||||
|
nsh_error(vtbl, g_fmtcmdoutofmemory, argv[0]);
|
||||||
|
ret = ERROR;
|
||||||
|
goto errout;
|
||||||
|
}
|
||||||
|
|
||||||
path = fullpath;
|
path = fullpath;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -288,6 +309,7 @@ int cmd_cd(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
|
||||||
|
|
||||||
/* Free any memory that was allocated */
|
/* Free any memory that was allocated */
|
||||||
|
|
||||||
|
errout:
|
||||||
if (alloc)
|
if (alloc)
|
||||||
{
|
{
|
||||||
free(alloc);
|
free(alloc);
|
||||||
|
|
|
||||||
|
|
@ -1555,6 +1555,12 @@ int cmd_wget(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
|
||||||
if (localfile == NULL)
|
if (localfile == NULL)
|
||||||
{
|
{
|
||||||
allocfile = strdup(url);
|
allocfile = strdup(url);
|
||||||
|
if (allocfile == NULL)
|
||||||
|
{
|
||||||
|
fmt = g_fmtcmdoutofmemory;
|
||||||
|
goto errout;
|
||||||
|
}
|
||||||
|
|
||||||
localfile = basename(allocfile);
|
localfile = basename(allocfile);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -40,7 +40,7 @@
|
||||||
/* Leveraged from libtelnet, https://github.com/seanmiddleditch/libtelnet.
|
/* Leveraged from libtelnet, https://github.com/seanmiddleditch/libtelnet.
|
||||||
* Modified and re-released under the BSD license.
|
* Modified and re-released under the BSD license.
|
||||||
*
|
*
|
||||||
* The original authors of libtelnet are listed below. Per their licesne,
|
* The original authors of libtelnet are listed below. Per their license,
|
||||||
* "The author or authors of this code dedicate any and all copyright
|
* "The author or authors of this code dedicate any and all copyright
|
||||||
* interest in this code to the public domain. We make this dedication for
|
* interest in this code to the public domain. We make this dedication for
|
||||||
* the benefit of the public at large and to the detriment of our heirs and
|
* the benefit of the public at large and to the detriment of our heirs and
|
||||||
|
|
@ -52,6 +52,7 @@
|
||||||
* (Also listed in the AUTHORS file are Jack Kelly <endgame.dos@gmail.com>
|
* (Also listed in the AUTHORS file are Jack Kelly <endgame.dos@gmail.com>
|
||||||
* and Katherine Flavel <kate@elide.org>)
|
* and Katherine Flavel <kate@elide.org>)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* Included Files
|
* Included Files
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
@ -254,6 +255,12 @@ static void _online(const char *line, int overflow, void *ud)
|
||||||
/* Keep name */
|
/* Keep name */
|
||||||
|
|
||||||
user->name = strdup(line);
|
user->name = strdup(line);
|
||||||
|
if (user->name == NULL)
|
||||||
|
{
|
||||||
|
telnet_printf(user->telnet, "Out of memory.\nEnter name: ");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
telnet_printf(user->telnet, "Welcome, %s!\n", line);
|
telnet_printf(user->telnet, "Welcome, %s!\n", line);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue