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:
Nightt 2026-05-23 10:23:47 +08:00 committed by Matteo Golin
parent 58b780c280
commit c44f9a0556
12 changed files with 134 additions and 20 deletions

View file

@ -6,3 +6,5 @@
object = new Controlse::CCertificate(
Controlse::CCertificate cert(se, settings->key_id);
auto certificate = Controlse::CCertificate(
* |---------- [-rw-r--r-- 15] afile.txt
g_afile.name = "afile.txt";

View file

@ -217,6 +217,13 @@ static void show_directories(const char *path, int indent)
snprintf(g_namebuffer, sizeof(g_namebuffer),
"%s/%s", path, direntry->d_name);
subdir = strdup(g_namebuffer);
if (subdir == NULL)
{
printf("show_directories: ERROR out of memory\n");
g_nerrors++;
continue;
}
show_directories(subdir, indent + 1);
free(subdir);
}

View file

@ -394,6 +394,12 @@ static void readdirectories(const char *path, struct node_s *entry)
snprintf(g_scratchbuffer, sizeof(g_scratchbuffer),
"%s/%s", path, direntry->d_name);
fullpath = strdup(g_scratchbuffer);
if (fullpath == NULL)
{
printf(" ERROR: Out of memory\n");
g_nerrors++;
continue;
}
if (DIRENT_ISDIRECTORY(direntry->d_type))
{

View file

@ -108,6 +108,8 @@ static int chat_tokenise(FAR struct chat *priv,
int tok_on_delimiter(void)
{
FAR struct chat_token *newtok;
if (!tok_pos && !quoted && !no_termin)
{
/* 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 */
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)
{
tok->next = malloc(sizeof(struct chat_token));
tok->next = newtok;
/* The terminated token becomes previous */
@ -134,26 +156,10 @@ static int chat_tokenise(FAR struct chat *priv,
{
/* There was no previous token */
*first_tok = malloc(sizeof(struct chat_token));
*first_tok = newtok;
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 */
tok_pos = 0;

View file

@ -108,6 +108,11 @@ SESSION ftpc_connect(FAR union ftpc_sockaddr_u *server)
*/
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 */

View file

@ -257,6 +257,7 @@ FAR struct ftpc_dirlist_s *ftpc_listdir(SESSION handle,
FAR char *tmpfname;
bool iscurrdir;
unsigned int nnames;
unsigned int i;
int allocsize;
int ret;
@ -365,6 +366,17 @@ FAR struct ftpc_dirlist_s *ftpc_listdir(SESSION handle,
ftpc_nlstparse(filestream, ftpc_addname, dirlist);
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:

View file

@ -172,6 +172,10 @@ int ftpc_relogin(FAR struct ftpc_session_s *session)
if (session->homerdir != NULL)
{
session->currdir = strdup(session->homerdir);
if (session->currdir == NULL)
{
return -ENOMEM;
}
}
/* If the user has requested a special start up directory, then change to

View file

@ -627,6 +627,11 @@ static bool ftpd_account_login(FAR struct ftpd_session_s *session,
if (account->home != NULL)
{
home = strdup(account->home);
if (home == NULL)
{
ftpd_account_free(account);
return false;
}
}
flags = account->flags;
@ -645,6 +650,11 @@ static bool ftpd_account_login(FAR struct ftpd_session_s *session,
{
home = strdup(home);
}
if (home == NULL)
{
return false;
}
}
if ((flags & FTPD_ACCOUNTFLAG_ADMIN) != 0)
@ -652,6 +662,12 @@ static bool ftpd_account_login(FAR struct ftpd_session_s *session,
/* admin user */
session->home = strdup("/");
if (session->home == NULL)
{
free(home);
return false;
}
session->work = home;
}
else
@ -660,6 +676,12 @@ static bool ftpd_account_login(FAR struct ftpd_session_s *session,
session->home = home;
session->work = strdup("/");
if (session->work == NULL)
{
free(home);
session->home = NULL;
return false;
}
}
return true;
@ -2517,6 +2539,17 @@ static int ftpd_command_user(FAR struct ftpd_session_s *session)
session->loggedin = false;
session->home = strdup(home == NULL ? "/" : home);
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,
g_respfmt1, 230, ' ', "Login successful.");

View file

@ -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);
ASSERT(ret > 0 && path);
if (ret < 0 || path == NULL)
{
nerr("ERROR: asprintf failed\n");
break;
}
/* call stat() to obtain modified time and size */

View file

@ -257,17 +257,38 @@ int cmd_cd(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
else if (strcmp(path, "-") == 0)
{
alloc = strdup(nsh_getwd(g_oldpwd));
if (alloc == NULL)
{
nsh_error(vtbl, g_fmtcmdoutofmemory, argv[0]);
ret = ERROR;
goto errout;
}
path = alloc;
}
#endif
else if (strcmp(path, "..") == 0)
{
alloc = strdup(nsh_getcwd(vtbl));
if (alloc == NULL)
{
nsh_error(vtbl, g_fmtcmdoutofmemory, argv[0]);
ret = ERROR;
goto errout;
}
path = dirname(alloc);
}
else
{
fullpath = nsh_getfullpath(vtbl, path);
if (fullpath == NULL)
{
nsh_error(vtbl, g_fmtcmdoutofmemory, argv[0]);
ret = ERROR;
goto errout;
}
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 */
errout:
if (alloc)
{
free(alloc);

View file

@ -1555,6 +1555,12 @@ int cmd_wget(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
if (localfile == NULL)
{
allocfile = strdup(url);
if (allocfile == NULL)
{
fmt = g_fmtcmdoutofmemory;
goto errout;
}
localfile = basename(allocfile);
}

View file

@ -40,7 +40,7 @@
/* Leveraged from libtelnet, https://github.com/seanmiddleditch/libtelnet.
* 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
* 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
@ -52,6 +52,7 @@
* (Also listed in the AUTHORS file are Jack Kelly <endgame.dos@gmail.com>
* and Katherine Flavel <kate@elide.org>)
*/
/****************************************************************************
* Included Files
****************************************************************************/
@ -254,6 +255,12 @@ static void _online(const char *line, int overflow, void *ud)
/* Keep name */
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);
return;
}