This is an automated email from the ASF dual-hosted git repository.
xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git
The following commit(s) were added to refs/heads/master by this push:
new dd760d2f3c2 fs/userfs: include null terminator in pathlen and use
memcpy
dd760d2f3c2 is described below
commit dd760d2f3c22e562d04496c4dd8752fd2758f54a
Author: Xiang Xiao <[email protected]>
AuthorDate: Thu Nov 12 21:50:00 2026 +0800
fs/userfs: include null terminator in pathlen and use memcpy
Move the + 1 for the null terminator from the SIZEOF macro call
into pathlen itself so that pathlen consistently represents the
full buffer length including the trailing NUL. This lets us
replace every strlcpy with a simple memcpy (the length is already
bounded by the mxwrite check), and on the receiver side change
the validation from >= to > since expected == reqlen is now a
valid exact match.
Signed-off-by: Xiang Xiao <[email protected]>
Signed-off-by: jingfei <[email protected]>
---
fs/userfs/fs_userfs.c | 46 +++++++++++++++++++++----------------------
libs/libc/userfs/lib_userfs.c | 36 ++++++++++++++++-----------------
2 files changed, 41 insertions(+), 41 deletions(-)
diff --git a/fs/userfs/fs_userfs.c b/fs/userfs/fs_userfs.c
index 2a1248edb0e..515a1141b9c 100644
--- a/fs/userfs/fs_userfs.c
+++ b/fs/userfs/fs_userfs.c
@@ -221,7 +221,7 @@ static int userfs_open(FAR struct file *filep, FAR const
char *relpath,
/* Check the path length */
DEBUGASSERT(relpath != NULL);
- pathlen = strlen(relpath);
+ pathlen = strlen(relpath) + 1;
if (pathlen > priv->mxwrite)
{
return -E2BIG;
@@ -242,10 +242,10 @@ static int userfs_open(FAR struct file *filep, FAR const
char *relpath,
req->oflags = oflags;
req->mode = mode;
- strlcpy(req->relpath, relpath, priv->mxwrite);
+ memcpy(req->relpath, relpath, pathlen);
nsent = psock_sendto(&priv->psock, priv->iobuffer,
- SIZEOF_USERFS_OPEN_REQUEST_S(pathlen + 1), 0,
+ SIZEOF_USERFS_OPEN_REQUEST_S(pathlen), 0,
(FAR struct sockaddr *)&priv->server,
sizeof(struct sockaddr_in));
if (nsent < 0)
@@ -1187,7 +1187,7 @@ static int userfs_opendir(FAR struct inode *mountpt, FAR
const char *relpath,
/* Check the path length */
DEBUGASSERT(relpath != NULL);
- pathlen = strlen(relpath);
+ pathlen = strlen(relpath) + 1;
if (pathlen > priv->mxwrite)
{
return -E2BIG;
@@ -1206,10 +1206,10 @@ static int userfs_opendir(FAR struct inode *mountpt,
FAR const char *relpath,
req = (FAR struct userfs_opendir_request_s *)priv->iobuffer;
req->req = USERFS_REQ_OPENDIR;
- strlcpy(req->relpath, relpath, priv->mxwrite);
+ memcpy(req->relpath, relpath, pathlen);
nsent = psock_sendto(&priv->psock, priv->iobuffer,
- SIZEOF_USERFS_OPENDIR_REQUEST_S(pathlen + 1), 0,
+ SIZEOF_USERFS_OPENDIR_REQUEST_S(pathlen), 0,
(FAR struct sockaddr *)&priv->server,
sizeof(struct sockaddr_in));
if (nsent < 0)
@@ -1807,7 +1807,7 @@ static int userfs_unlink(FAR struct inode *mountpt,
/* Check the path length */
DEBUGASSERT(relpath != NULL);
- pathlen = strlen(relpath);
+ pathlen = strlen(relpath) + 1;
if (pathlen > priv->mxwrite)
{
return -E2BIG;
@@ -1826,10 +1826,10 @@ static int userfs_unlink(FAR struct inode *mountpt,
req = (FAR struct userfs_unlink_request_s *)priv->iobuffer;
req->req = USERFS_REQ_UNLINK;
- strlcpy(req->relpath, relpath, priv->mxwrite);
+ memcpy(req->relpath, relpath, pathlen);
nsent = psock_sendto(&priv->psock, priv->iobuffer,
- SIZEOF_USERFS_UNLINK_REQUEST_S(pathlen + 1), 0,
+ SIZEOF_USERFS_UNLINK_REQUEST_S(pathlen), 0,
(FAR struct sockaddr *)&priv->server,
sizeof(struct sockaddr_in));
if (nsent < 0)
@@ -1899,7 +1899,7 @@ static int userfs_mkdir(FAR struct inode *mountpt,
/* Check the path length */
DEBUGASSERT(relpath != NULL);
- pathlen = strlen(relpath);
+ pathlen = strlen(relpath) + 1;
if (pathlen > priv->mxwrite)
{
return -E2BIG;
@@ -1919,10 +1919,10 @@ static int userfs_mkdir(FAR struct inode *mountpt,
req->req = USERFS_REQ_MKDIR;
req->mode = mode;
- strlcpy(req->relpath, relpath, priv->mxwrite);
+ memcpy(req->relpath, relpath, pathlen);
nsent = psock_sendto(&priv->psock, priv->iobuffer,
- SIZEOF_USERFS_MKDIR_REQUEST_S(pathlen + 1), 0,
+ SIZEOF_USERFS_MKDIR_REQUEST_S(pathlen), 0,
(FAR struct sockaddr *)&priv->server,
sizeof(struct sockaddr_in));
if (nsent < 0)
@@ -1992,7 +1992,7 @@ static int userfs_rmdir(FAR struct inode *mountpt,
/* Check the path length */
DEBUGASSERT(relpath != NULL);
- pathlen = strlen(relpath);
+ pathlen = strlen(relpath) + 1;
if (pathlen > priv->mxwrite)
{
return -E2BIG;
@@ -2011,10 +2011,10 @@ static int userfs_rmdir(FAR struct inode *mountpt,
req = (FAR struct userfs_rmdir_request_s *)priv->iobuffer;
req->req = USERFS_REQ_RMDIR;
- strlcpy(req->relpath, relpath, priv->mxwrite);
+ memcpy(req->relpath, relpath, pathlen);
nsent = psock_sendto(&priv->psock, priv->iobuffer,
- SIZEOF_USERFS_RMDIR_REQUEST_S(pathlen + 1), 0,
+ SIZEOF_USERFS_RMDIR_REQUEST_S(pathlen), 0,
(FAR struct sockaddr *)&priv->server,
sizeof(struct sockaddr_in));
if (nsent < 0)
@@ -2118,8 +2118,8 @@ static int userfs_rename(FAR struct inode *mountpt,
req->req = USERFS_REQ_RENAME;
req->newoffset = oldpathlen;
- strlcpy(req->oldrelpath, oldrelpath, oldpathlen);
- strlcpy(&req->oldrelpath[oldpathlen], newrelpath, newpathlen);
+ memcpy(req->oldrelpath, oldrelpath, oldpathlen);
+ memcpy(&req->oldrelpath[oldpathlen], newrelpath, newpathlen);
nsent = psock_sendto(&priv->psock, priv->iobuffer,
SIZEOF_USERFS_RENAME_REQUEST_S(oldpathlen, newpathlen),
@@ -2192,7 +2192,7 @@ static int userfs_stat(FAR struct inode *mountpt, FAR
const char *relpath,
/* Check the path length */
DEBUGASSERT(relpath != NULL);
- pathlen = strlen(relpath);
+ pathlen = strlen(relpath) + 1;
if (pathlen > priv->mxwrite)
{
return -E2BIG;
@@ -2211,10 +2211,10 @@ static int userfs_stat(FAR struct inode *mountpt, FAR
const char *relpath,
req = (FAR struct userfs_stat_request_s *)priv->iobuffer;
req->req = USERFS_REQ_STAT;
- strlcpy(req->relpath, relpath, priv->mxwrite);
+ memcpy(req->relpath, relpath, pathlen);
nsent = psock_sendto(&priv->psock, priv->iobuffer,
- SIZEOF_USERFS_STAT_REQUEST_S(pathlen + 1), 0,
+ SIZEOF_USERFS_STAT_REQUEST_S(pathlen), 0,
(FAR struct sockaddr *)&priv->server,
sizeof(struct sockaddr_in));
if (nsent < 0)
@@ -2288,7 +2288,7 @@ static int userfs_chstat(FAR struct inode *mountpt, FAR
const char *relpath,
/* Check the path length */
DEBUGASSERT(relpath != NULL);
- pathlen = strlen(relpath);
+ pathlen = strlen(relpath) + 1;
if (pathlen > priv->mxwrite)
{
return -E2BIG;
@@ -2309,10 +2309,10 @@ static int userfs_chstat(FAR struct inode *mountpt, FAR
const char *relpath,
req->buf = *buf;
req->flags = flags;
- strlcpy(req->relpath, relpath, priv->mxwrite);
+ memcpy(req->relpath, relpath, pathlen);
nsent = psock_sendto(&priv->psock, priv->iobuffer,
- SIZEOF_USERFS_CHSTAT_REQUEST_S(pathlen + 1), 0,
+ SIZEOF_USERFS_CHSTAT_REQUEST_S(pathlen), 0,
(FAR struct sockaddr *)&priv->server,
sizeof(struct sockaddr_in));
if (nsent < 0)
diff --git a/libs/libc/userfs/lib_userfs.c b/libs/libc/userfs/lib_userfs.c
index a65b8de78f2..4feee2c5ccf 100644
--- a/libs/libc/userfs/lib_userfs.c
+++ b/libs/libc/userfs/lib_userfs.c
@@ -124,14 +124,14 @@ static inline int userfs_open_dispatch(FAR struct
userfs_info_s *info,
return -EINVAL;
}
- pathlen = strlen(req->relpath);
+ pathlen = strlen(req->relpath) + 1;
if (pathlen > info->mxwrite)
{
return -EINVAL;
}
expected = SIZEOF_USERFS_OPEN_REQUEST_S(pathlen);
- if (expected >= reqlen)
+ if (expected > reqlen)
{
return -EINVAL;
}
@@ -463,14 +463,14 @@ static inline int userfs_opendir_dispatch(FAR struct
userfs_info_s *info,
return -EINVAL;
}
- pathlen = strlen(req->relpath);
+ pathlen = strlen(req->relpath) + 1;
if (pathlen > info->mxwrite)
{
return -EINVAL;
}
expected = SIZEOF_USERFS_OPENDIR_REQUEST_S(pathlen);
- if (expected >= reqlen)
+ if (expected > reqlen)
{
return -EINVAL;
}
@@ -618,14 +618,14 @@ static inline int userfs_unlink_dispatch(FAR struct
userfs_info_s *info,
return -EINVAL;
}
- pathlen = strlen(req->relpath);
+ pathlen = strlen(req->relpath) + 1;
if (pathlen > info->mxwrite)
{
return -EINVAL;
}
expected = SIZEOF_USERFS_UNLINK_REQUEST_S(pathlen);
- if (expected >= reqlen)
+ if (expected > reqlen)
{
return -EINVAL;
}
@@ -660,14 +660,14 @@ static inline int userfs_mkdir_dispatch(FAR struct
userfs_info_s *info,
return -EINVAL;
}
- pathlen = strlen(req->relpath);
+ pathlen = strlen(req->relpath) + 1;
if (pathlen > info->mxwrite)
{
return -EINVAL;
}
expected = SIZEOF_USERFS_MKDIR_REQUEST_S(pathlen);
- if (expected >= reqlen)
+ if (expected > reqlen)
{
return -EINVAL;
}
@@ -702,14 +702,14 @@ static inline int userfs_rmdir_dispatch(FAR struct
userfs_info_s *info,
return -EINVAL;
}
- pathlen = strlen(req->relpath);
+ pathlen = strlen(req->relpath) + 1;
if (pathlen > info->mxwrite)
{
return -EINVAL;
}
expected = SIZEOF_USERFS_MKDIR_REQUEST_S(pathlen);
- if (expected >= reqlen)
+ if (expected > reqlen)
{
return -EINVAL;
}
@@ -747,16 +747,16 @@ static inline int userfs_rename_dispatch(FAR struct
userfs_info_s *info,
return -EINVAL;
}
- oldpathlen = strlen(req->oldrelpath);
+ oldpathlen = strlen(req->oldrelpath) + 1;
newoffset = req->newoffset;
- if (oldpathlen >= newoffset)
+ if (oldpathlen > newoffset)
{
return -EINVAL;
}
newrelpath = &req->oldrelpath[newoffset];
- newpathlen = strlen(newrelpath);
+ newpathlen = strlen(newrelpath) + 1;
if ((newpathlen + newoffset) > info->mxwrite)
{
@@ -764,7 +764,7 @@ static inline int userfs_rename_dispatch(FAR struct
userfs_info_s *info,
}
expected = SIZEOF_USERFS_RENAME_REQUEST_S(newoffset, newpathlen);
- if (expected >= reqlen)
+ if (expected > reqlen)
{
return -EINVAL;
}
@@ -800,14 +800,14 @@ static inline int userfs_stat_dispatch(FAR struct
userfs_info_s *info,
return -EINVAL;
}
- pathlen = strlen(req->relpath);
+ pathlen = strlen(req->relpath) + 1;
if (pathlen > info->mxwrite)
{
return -EINVAL;
}
expected = SIZEOF_USERFS_STAT_REQUEST_S(pathlen);
- if (expected >= reqlen)
+ if (expected > reqlen)
{
return -EINVAL;
}
@@ -912,14 +912,14 @@ static inline int userfs_chstat_dispatch(FAR struct
userfs_info_s *info,
return -EINVAL;
}
- pathlen = strlen(req->relpath);
+ pathlen = strlen(req->relpath) + 1;
if (pathlen > info->mxwrite)
{
return -EINVAL;
}
expected = SIZEOF_USERFS_CHSTAT_REQUEST_S(pathlen);
- if (expected >= reqlen)
+ if (expected > reqlen)
{
return -EINVAL;
}