Fixes in asprintf usage.

This commit is contained in:
Fotis Panagiotopoulos 2023-04-12 15:32:35 +03:00 committed by Xiang Xiao
parent 433ec4c9de
commit 098b7bbfb3
5 changed files with 34 additions and 15 deletions

View File

@ -353,13 +353,15 @@ static int _inode_search(FAR struct inode_search_s *desc)
{
char *buffer = NULL;
asprintf(&buffer,
"%s/%s", desc->relpath, name);
if (buffer != NULL)
ret = asprintf(&buffer,
"%s/%s", desc->relpath,
name);
if (ret > 0)
{
lib_free(desc->buffer);
desc->buffer = buffer;
relpath = buffer;
ret = OK;
}
else
{
@ -481,8 +483,8 @@ int inode_search(FAR struct inode_search_s *desc)
if (*desc->path != '/')
{
asprintf(&desc->buffer, "%s/%s", _inode_getcwd(), desc->path);
if (desc->buffer == NULL)
ret = asprintf(&desc->buffer, "%s/%s", _inode_getcwd(), desc->path);
if (ret < 0)
{
return -ENOMEM;
}

View File

@ -600,7 +600,13 @@ int dir_allocate(FAR struct file *filep, FAR const char *relpath)
}
inode_getpath(inode, path_prefix);
asprintf(&dir->fd_path, "%s%s/", path_prefix, relpath);
ret = asprintf(&dir->fd_path, "%s%s/", path_prefix, relpath);
if (ret < 0)
{
dir->fd_path = NULL;
return ret;
}
filep->f_inode = &g_dir_inode;
filep->f_priv = dir;
inode_addref(&g_dir_inode);

View File

@ -128,9 +128,10 @@ next_subdir:
*/
subdirname = basename((FAR char *)oldpath);
asprintf(&subdir, "%s/%s", newpath, subdirname);
if (subdir == NULL)
ret = asprintf(&subdir, "%s/%s", newpath, subdirname);
if (ret < 0)
{
subdir = NULL;
ret = -ENOMEM;
goto errout;
}
@ -369,10 +370,11 @@ next_subdir:
}
else
{
asprintf(&subdir, "%s/%s", newrelpath,
subdirname);
if (subdir == NULL)
ret = asprintf(&subdir, "%s/%s", newrelpath,
subdirname);
if (ret < 0)
{
subdir = NULL;
ret = -ENOMEM;
goto errout_with_newinode;
}

View File

@ -74,9 +74,10 @@ FAR char *tempnam(FAR const char *dir, FAR const char *pfx)
{
FAR char *template;
FAR char *path;
int ret;
asprintf(&template, "%s/%s-XXXXXX", dir, pfx);
if (template)
ret = asprintf(&template, "%s/%s-XXXXXX", dir, pfx);
if (ret > 0)
{
path = mktemp(template);
if (path != NULL)

View File

@ -1112,7 +1112,11 @@ static int dir_notempty(const char *dirpath, const char *name,
int ret;
ret = asprintf(&path, "%s/%s", dirpath, name);
UNUSED(ret);
if (ret < 0)
{
fprintf(stderr, "ERROR: asprintf() failed\n");
exit(1);
}
/* stat() should not fail for any reason */
@ -1142,7 +1146,11 @@ static int process_direntry(const char *dirpath, const char *name,
int ret;
ret = asprintf(&path, "%s/%s", dirpath, name);
UNUSED(ret);
if (ret < 0)
{
fprintf(stderr, "ERROR: asprintf() failed\n");
exit(1);
}
ret = stat(path, &buf);
if (ret < 0)