From 098b7bbfb39c1b14bf9c52659718839f595f5af1 Mon Sep 17 00:00:00 2001 From: Fotis Panagiotopoulos Date: Wed, 12 Apr 2023 15:32:35 +0300 Subject: [PATCH] Fixes in asprintf usage. --- fs/inode/fs_inodesearch.c | 12 +++++++----- fs/vfs/fs_dir.c | 8 +++++++- fs/vfs/fs_rename.c | 12 +++++++----- libs/libc/stdio/lib_tempnam.c | 5 +++-- tools/gencromfs.c | 12 ++++++++++-- 5 files changed, 34 insertions(+), 15 deletions(-) diff --git a/fs/inode/fs_inodesearch.c b/fs/inode/fs_inodesearch.c index a46e1602ee..7dda789359 100644 --- a/fs/inode/fs_inodesearch.c +++ b/fs/inode/fs_inodesearch.c @@ -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; } diff --git a/fs/vfs/fs_dir.c b/fs/vfs/fs_dir.c index 1fa68dbb66..8e4d6a6766 100644 --- a/fs/vfs/fs_dir.c +++ b/fs/vfs/fs_dir.c @@ -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); diff --git a/fs/vfs/fs_rename.c b/fs/vfs/fs_rename.c index add0f109e8..d7dfab2b03 100644 --- a/fs/vfs/fs_rename.c +++ b/fs/vfs/fs_rename.c @@ -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; } diff --git a/libs/libc/stdio/lib_tempnam.c b/libs/libc/stdio/lib_tempnam.c index 0c4a4a67e7..7151905eb4 100644 --- a/libs/libc/stdio/lib_tempnam.c +++ b/libs/libc/stdio/lib_tempnam.c @@ -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) diff --git a/tools/gencromfs.c b/tools/gencromfs.c index 55189cb91f..71399b6eac 100644 --- a/tools/gencromfs.c +++ b/tools/gencromfs.c @@ -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)