nuttx:Change fs strncpy to strlcpy to avoid losing'\0'

Signed-off-by: zhouliang3 <zhouliang3@xiaomi.com>
This commit is contained in:
zhouliang3
2021-12-29 20:17:34 +08:00
committed by Xiang Xiao
parent 76b2f7199d
commit 1f53a058fa
20 changed files with 60 additions and 55 deletions
+1 -1
View File
@@ -326,7 +326,7 @@ static int binfs_readdir(struct inode *mountpt, struct fs_dirent_s *dir)
finfo("Entry %d: \"%s\"\n", index, name); finfo("Entry %d: \"%s\"\n", index, name);
dir->fd_dir.d_type = DTYPE_FILE; dir->fd_dir.d_type = DTYPE_FILE;
strncpy(dir->fd_dir.d_name, name, NAME_MAX); strlcpy(dir->fd_dir.d_name, name, sizeof(dir->fd_dir.d_name));
/* The application list is terminated by an entry with a NULL name. /* The application list is terminated by an entry with a NULL name.
* Therefore, there is at least one more entry in the list. * Therefore, there is at least one more entry in the list.
+1 -1
View File
@@ -1289,7 +1289,7 @@ static int cromfs_readdir(struct inode *mountpt, struct fs_dirent_s *dir)
name = (FAR char *)cromfs_offset2addr(fs, node->cn_name); name = (FAR char *)cromfs_offset2addr(fs, node->cn_name);
finfo("Entry %" PRIu32 ": %s\n", offset, name); finfo("Entry %" PRIu32 ": %s\n", offset, name);
strncpy(dir->fd_dir.d_name, name, NAME_MAX); strlcpy(dir->fd_dir.d_name, name, sizeof(dir->fd_dir.d_name));
switch (node->cn_mode & S_IFMT) switch (node->cn_mode & S_IFMT)
{ {
+2 -2
View File
@@ -60,8 +60,8 @@ static inline int readpseudodir(struct fs_dirent_s *idir)
/* Copy the inode name into the dirent structure */ /* Copy the inode name into the dirent structure */
strncpy(idir->fd_dir.d_name, idir->u.pseudo.fd_next->i_name, strlcpy(idir->fd_dir.d_name, idir->u.pseudo.fd_next->i_name,
NAME_MAX); sizeof(idir->fd_dir.d_name));
/* If the node has file operations, we will say that it is a file. */ /* If the node has file operations, we will say that it is a file. */
+2 -1
View File
@@ -227,7 +227,8 @@ static int part_ioctl(FAR struct inode *inode, int cmd, unsigned long arg)
info->sectorsize = dev->sectorsize; info->sectorsize = dev->sectorsize;
info->startsector = dev->firstsector; info->startsector = dev->firstsector;
strncpy(info->parent, dev->parent->i_name, NAME_MAX); strlcpy(info->parent, dev->parent->i_name,
sizeof(info->parent));
ret = OK; ret = OK;
} }
+6 -6
View File
@@ -195,7 +195,7 @@ static void hostfs_mkpath(FAR struct hostfs_mountpt_s *fs,
/* Copy base host path to output */ /* Copy base host path to output */
strncpy(path, fs->fs_root, pathlen); strlcpy(path, fs->fs_root, pathlen);
/* Be sure we aren't trying to use ".." to display outside of our /* Be sure we aren't trying to use ".." to display outside of our
* mounted path. * mounted path.
@@ -1051,7 +1051,7 @@ static int hostfs_bind(FAR struct inode *blkdriver, FAR const void *data,
{ {
if ((strncmp(ptr, "fs=", 3) == 0)) if ((strncmp(ptr, "fs=", 3) == 0))
{ {
strncpy(fs->fs_root, &ptr[3], sizeof(fs->fs_root)); strlcpy(fs->fs_root, &ptr[3], sizeof(fs->fs_root));
} }
ptr = strtok_r(NULL, ",", &saveptr); ptr = strtok_r(NULL, ",", &saveptr);
@@ -1344,10 +1344,10 @@ int hostfs_rename(FAR struct inode *mountpt, FAR const char *oldrelpath,
/* Append to the host's root directory */ /* Append to the host's root directory */
strncpy(oldpath, fs->fs_root, sizeof(oldpath)); strlcpy(oldpath, fs->fs_root, sizeof(oldpath));
strncat(oldpath, oldrelpath, sizeof(oldpath)-strlen(oldpath)-1); strlcat(oldpath, oldrelpath, sizeof(oldpath));
strncpy(newpath, fs->fs_root, sizeof(newpath)); strlcpy(newpath, fs->fs_root, sizeof(newpath));
strncat(newpath, newrelpath, sizeof(newpath)-strlen(newpath)-1); strlcat(newpath, newrelpath, sizeof(newpath));
/* Call the host FS to do the mkdir */ /* Call the host FS to do the mkdir */
+1 -1
View File
@@ -782,7 +782,7 @@ static int littlefs_readdir(FAR struct inode *mountpt,
dir->fd_dir.d_type = DTYPE_DIRECTORY; dir->fd_dir.d_type = DTYPE_DIRECTORY;
} }
strcpy(dir->fd_dir.d_name, info.name); strlcpy(dir->fd_dir.d_name, info.name, sizeof(dir->fd_dir.d_name));
} }
else if (ret == 0) else if (ret == 0)
{ {
+1 -1
View File
@@ -133,7 +133,7 @@ int nxffs_readdir(FAR struct inode *mountpt, FAR struct fs_dirent_s *dir)
finfo("Offset %jd: \"%s\"\n", (intmax_t)entry.hoffset, entry.name); finfo("Offset %jd: \"%s\"\n", (intmax_t)entry.hoffset, entry.name);
dir->fd_dir.d_type = DTYPE_FILE; dir->fd_dir.d_type = DTYPE_FILE;
strncpy(dir->fd_dir.d_name, entry.name, NAME_MAX); strlcpy(dir->fd_dir.d_name, entry.name, sizeof(dir->fd_dir.d_name));
/* Discard this entry and set the next offset. */ /* Discard this entry and set the next offset. */
+1 -1
View File
@@ -129,7 +129,7 @@ int parse_ptable_partition(FAR struct partition_state_s *state,
/* Convert the entry to partition */ /* Convert the entry to partition */
strncpy(part.name, entry->name, sizeof(part.name)); strlcpy(part.name, entry->name, sizeof(part.name));
part.index = entry - ptable->entries; part.index = entry - ptable->entries;
part.firstblock = entry->offset / state->blocksize; part.firstblock = entry->offset / state->blocksize;
part.nblocks = entry->length / state->blocksize; part.nblocks = entry->length / state->blocksize;
+3 -3
View File
@@ -590,7 +590,8 @@ static int procfs_opendir(FAR struct inode *mountpt, FAR const char *relpath,
} }
else else
{ {
int x, ret; int x;
int ret;
int len = strlen(relpath); int len = strlen(relpath);
/* Search the static array of procfs_entries */ /* Search the static array of procfs_entries */
@@ -799,8 +800,7 @@ static int procfs_readdir(struct inode *mountpt, struct fs_dirent_s *dir)
level0->lastlen = strcspn(name, "/"); level0->lastlen = strcspn(name, "/");
level0->lastread = name; level0->lastread = name;
strncpy(dir->fd_dir.d_name, name, level0->lastlen); strlcpy(dir->fd_dir.d_name, name, level0->lastlen);
dir->fd_dir.d_name[level0->lastlen] = '\0';
/* If the entry is a directory type OR if the reported name is /* If the entry is a directory type OR if the reported name is
* only a sub-string of the entry (meaning that it contains * only a sub-string of the entry (meaning that it contains
+1 -1
View File
@@ -1779,7 +1779,7 @@ static int proc_readdir(struct fs_dirent_s *dir)
/* Save the filename and file type */ /* Save the filename and file type */
dir->fd_dir.d_type = node->dtype; dir->fd_dir.d_type = node->dtype;
strncpy(dir->fd_dir.d_name, node->name, NAME_MAX); strlcpy(dir->fd_dir.d_name, node->name, sizeof(dir->fd_dir.d_name));
/* Set up the next directory entry offset. NOTE that we could use the /* Set up the next directory entry offset. NOTE that we could use the
* standard f_pos instead of our own private index. * standard f_pos instead of our own private index.
+1 -1
View File
@@ -440,7 +440,7 @@ static int skel_readdir(FAR struct fs_dirent_s *dir)
/* TODO: Specify the type of entry */ /* TODO: Specify the type of entry */
dir->fd_dir.d_type = DTYPE_FILE; dir->fd_dir.d_type = DTYPE_FILE;
strncpy(dir->fd_dir.d_name, filename, NAME_MAX); strlcpy(dir->fd_dir.d_name, filename, sizeof(dir->fd_dir.d_name));
/* Set up the next directory entry offset. NOTE that we could use the /* Set up the next directory entry offset. NOTE that we could use the
* standard f_pos instead of our own private index. * standard f_pos instead of our own private index.
+6 -6
View File
@@ -210,7 +210,7 @@ static void rpmsgfs_mkpath(FAR struct rpmsgfs_mountpt_s *fs,
/* Copy base host path to output */ /* Copy base host path to output */
strncpy(path, fs->fs_root, pathlen); strlcpy(path, fs->fs_root, pathlen);
/* Be sure we aren't trying to use ".." to display outside of our /* Be sure we aren't trying to use ".." to display outside of our
* mounted path. * mounted path.
@@ -1069,7 +1069,7 @@ static int rpmsgfs_bind(FAR struct inode *blkdriver, FAR const void *data,
{ {
if ((strncmp(ptr, "fs=", 3) == 0)) if ((strncmp(ptr, "fs=", 3) == 0))
{ {
strncpy(fs->fs_root, &ptr[3], sizeof(fs->fs_root)); strlcpy(fs->fs_root, &ptr[3], sizeof(fs->fs_root));
} }
else if ((strncmp(ptr, "cpu=", 4) == 0)) else if ((strncmp(ptr, "cpu=", 4) == 0))
{ {
@@ -1362,10 +1362,10 @@ int rpmsgfs_rename(FAR struct inode *mountpt, FAR const char *oldrelpath,
/* Append to the host's root directory */ /* Append to the host's root directory */
strncpy(oldpath, fs->fs_root, sizeof(oldpath)); strlcpy(oldpath, fs->fs_root, sizeof(oldpath));
strncat(oldpath, oldrelpath, sizeof(oldpath)-strlen(oldpath)-1); strlcat(oldpath, oldrelpath, sizeof(oldpath));
strncpy(newpath, fs->fs_root, sizeof(newpath)); strlcpy(newpath, fs->fs_root, sizeof(newpath));
strncat(newpath, newrelpath, sizeof(newpath)-strlen(newpath)-1); strlcat(newpath, newrelpath, sizeof(newpath));
/* Call the host FS to do the mkdir */ /* Call the host FS to do the mkdir */
+2 -3
View File
@@ -168,8 +168,7 @@ static int rpmsgfs_readdir_handler(FAR struct rpmsg_endpoint *ept,
cookie->result = header->result; cookie->result = header->result;
if (cookie->result >= 0) if (cookie->result >= 0)
{ {
strncpy(entry->d_name, rsp->name, NAME_MAX); strlcpy(entry->d_name, rsp->name, sizeof(entry->d_name));
entry->d_name[NAME_MAX] = '\0';
entry->d_type = rsp->type; entry->d_type = rsp->type;
} }
@@ -587,7 +586,7 @@ int rpmsgfs_client_bind(FAR void **handle, FAR const char *cpuname)
return -ENOMEM; return -ENOMEM;
} }
strncpy(priv->cpuname, cpuname, RPMSG_NAME_SIZE); strlcpy(priv->cpuname, cpuname, sizeof(priv->cpuname));
ret = rpmsg_register_callback(priv, ret = rpmsg_register_callback(priv,
rpmsgfs_device_created, rpmsgfs_device_created,
rpmsgfs_device_destroy, rpmsgfs_device_destroy,
+17 -11
View File
@@ -604,7 +604,8 @@ static int smartfs_closedir(FAR struct fs_dirent_s *dir)
static int smartfs_readdir(struct fs_dirent_s *dir) static int smartfs_readdir(struct fs_dirent_s *dir)
{ {
FAR struct smartfs_level1_s *level1; FAR struct smartfs_level1_s *level1;
int ret, index; int ret;
int index;
DEBUGASSERT(dir && dir->u.procfs); DEBUGASSERT(dir && dir->u.procfs);
level1 = dir->u.procfs; level1 = dir->u.procfs;
@@ -640,8 +641,8 @@ static int smartfs_readdir(struct fs_dirent_s *dir)
} }
dir->fd_dir.d_type = DTYPE_DIRECTORY; dir->fd_dir.d_type = DTYPE_DIRECTORY;
strncpy(dir->fd_dir.d_name, level1->mount->fs_blkdriver->i_name, strlcpy(dir->fd_dir.d_name, level1->mount->fs_blkdriver->i_name,
NAME_MAX); sizeof(dir->fd_dir.d_name));
/* Advance to next entry */ /* Advance to next entry */
@@ -653,16 +654,16 @@ static int smartfs_readdir(struct fs_dirent_s *dir)
/* Listing the contents of a specific mount */ /* Listing the contents of a specific mount */
dir->fd_dir.d_type = g_direntry[level1->base.index].type; dir->fd_dir.d_type = g_direntry[level1->base.index].type;
strncpy(dir->fd_dir.d_name, g_direntry[level1->base.index++].name, strlcpy(dir->fd_dir.d_name, g_direntry[level1->base.index++].name,
NAME_MAX); sizeof(dir->fd_dir.d_name));
} }
else if (level1->base.level == 3) else if (level1->base.level == 3)
{ {
/* Listing the contents of a specific entry */ /* Listing the contents of a specific entry */
dir->fd_dir.d_type = g_direntry[level1->base.index].type; dir->fd_dir.d_type = g_direntry[level1->base.index].type;
strncpy(dir->fd_dir.d_name, g_direntry[level1->direntry].name, strlcpy(dir->fd_dir.d_name, g_direntry[level1->direntry].name,
NAME_MAX); sizeof(dir->fd_dir.d_name));
level1->base.index++; level1->base.index++;
} }
@@ -874,7 +875,8 @@ static size_t smartfs_mem_read(FAR struct file *filep, FAR char *buffer,
FAR struct smartfs_file_s *priv; FAR struct smartfs_file_s *priv;
int ret; int ret;
uint16_t x; uint16_t x;
size_t len, total; size_t len;
size_t total;
priv = (FAR struct smartfs_file_s *) filep->f_priv; priv = (FAR struct smartfs_file_s *) filep->f_priv;
@@ -938,9 +940,13 @@ static size_t smartfs_erasemap_read(FAR struct file *filep,
{ {
struct mtd_smart_procfs_data_s procfs_data; struct mtd_smart_procfs_data_s procfs_data;
FAR struct smartfs_file_s *priv; FAR struct smartfs_file_s *priv;
int ret, rows, cols; int ret;
size_t x, y; int rows;
size_t len, copylen; int cols;
size_t x;
size_t y;
size_t len;
size_t copylen;
priv = (FAR struct smartfs_file_s *) filep->f_priv; priv = (FAR struct smartfs_file_s *) filep->f_priv;
+2 -9
View File
@@ -1272,7 +1272,6 @@ static int smartfs_readdir(struct inode *mountpt, struct fs_dirent_s *dir)
struct smartfs_mountpt_s *fs; struct smartfs_mountpt_s *fs;
int ret; int ret;
uint16_t entrysize; uint16_t entrysize;
uint16_t namelen;
struct smartfs_chain_header_s *header; struct smartfs_chain_header_s *header;
struct smart_read_write_s readwrite; struct smart_read_write_s readwrite;
struct smartfs_entry_header_s *entry; struct smartfs_entry_header_s *entry;
@@ -1350,14 +1349,8 @@ static int smartfs_readdir(struct inode *mountpt, struct fs_dirent_s *dir)
/* Copy the entry name to dirent */ /* Copy the entry name to dirent */
namelen = fs->fs_llformat.namesize; strlcpy(dir->fd_dir.d_name, entry->name,
if (namelen > NAME_MAX) sizeof(dir->fd_dir.d_name));
{
namelen = NAME_MAX;
}
memset(dir->fd_dir.d_name, 0, namelen);
strncpy(dir->fd_dir.d_name, entry->name, namelen);
/* Now advance to the next entry */ /* Now advance to the next entry */
+2 -2
View File
@@ -342,9 +342,9 @@ static int spiffs_readdir_callback(FAR struct spiffs_s *fs,
} }
#endif #endif
strncpy(entryp->d_name, strlcpy(entryp->d_name,
(FAR char *)objhdr.name + SPIFFS_LEADING_SLASH_SIZE, (FAR char *)objhdr.name + SPIFFS_LEADING_SLASH_SIZE,
NAME_MAX); sizeof(entryp->d_name));
entryp->d_type = objhdr.type; entryp->d_type = objhdr.type;
return OK; return OK;
} }
+1 -1
View File
@@ -2035,7 +2035,7 @@ static int tmpfs_readdir(FAR struct inode *mountpt,
/* Copy the entry name */ /* Copy the entry name */
strncpy(dir->fd_dir.d_name, tde->tde_name, NAME_MAX); strlcpy(dir->fd_dir.d_name, tde->tde_name, sizeof(dir->fd_dir.d_name));
/* Save the index for next time */ /* Save the index for next time */
+3 -2
View File
@@ -1710,7 +1710,7 @@ static int unionfs_readdir(struct inode *mountpt, struct fs_dirent_s *dir)
* directories. * directories.
*/ */
strncpy(dir->fd_dir.d_name, um->um_prefix, NAME_MAX); strlcpy(dir->fd_dir.d_name, um->um_prefix, sizeof(dir->fd_dir.d_name));
/* Describe this as a read only directory */ /* Describe this as a read only directory */
@@ -1784,7 +1784,8 @@ static int unionfs_readdir(struct inode *mountpt, struct fs_dirent_s *dir)
* be multiple directories. * be multiple directories.
*/ */
strncpy(dir->fd_dir.d_name, um->um_prefix, NAME_MAX); strlcpy(dir->fd_dir.d_name, um->um_prefix,
sizeof(dir->fd_dir.d_name));
/* Describe this as a read only directory */ /* Describe this as a read only directory */
+1 -2
View File
@@ -106,8 +106,7 @@ ssize_t readlink(FAR const char *path, FAR char *buf, size_t bufsize)
/* Copy the link target pathto the user-provided buffer. */ /* Copy the link target pathto the user-provided buffer. */
*buf = '\0'; strlcpy(buf, node->u.i_link, bufsize);
strncpy(buf, node->u.i_link, bufsize);
/* Release our reference on the inode and return the length */ /* Release our reference on the inode and return the length */
+6
View File
@@ -251,6 +251,8 @@ static const char *g_white_list[] =
/* Ref: /* Ref:
* fs/nfs/rpc.h * fs/nfs/rpc.h
* fs/nfs/nfs_proto.h * fs/nfs/nfs_proto.h
* fs/nfs/nfs_mount.h
* fs/nfs/nfs_vfsops.c
*/ */
"CREATE3args", "CREATE3args",
@@ -275,6 +277,10 @@ static const char *g_white_list[] =
"SETATTR3args", "SETATTR3args",
"SETATTR3resok", "SETATTR3resok",
"FS3args", "FS3args",
"SIZEOF_rpc_reply_read",
"SIZEOF_rpc_call_write",
"SIZEOF_rpc_reply_readdir",
"SIZEOF_nfsmount",
/* Ref: /* Ref:
* mm/kasan/kasan.c * mm/kasan/kasan.c