Copilot commented on code in PR #21752:
URL: https://github.com/apache/nuttx/pull/21752#discussion_r4564229830
##########
fs/vfs/fs_readlink.c:
##########
@@ -139,10 +139,10 @@ ssize_t readlink(FAR const char *path, FAR char *buf,
size_t bufsize)
goto errout_with_inode;
}
- /* Copy the link target path to the user-provided buffer. */
+ /* readlink() does not NUL-terminate: copy at most bufsize bytes */
- nread = strlcpy(buf, node->u.i_link, bufsize);
- nread = MIN(nread, bufsize);
+ nread = MIN(strlen(node->u.i_link), bufsize);
+ memcpy(buf, node->u.i_link, nread);
Review Comment:
`readlink()` with `bufsize == 0` should not touch `buf`. As written, `nread`
becomes 0, but calling `memcpy(buf, ..., 0)` can still be undefined behavior in
C if `buf` is not a valid pointer. Add an explicit `if (bufsize == 0) { nread =
0; } else { ... memcpy ... }` (or guard the `memcpy` with `if (nread > 0)`).
##########
fs/vfs/fs_rmdir.c:
##########
@@ -62,8 +63,24 @@ int rmdir(FAR const char *pathname)
{
struct inode_search_s desc;
FAR struct inode *inode;
+ size_t len;
int ret;
+ /* The lookup collapses ".", so reject a last component of "." first */
+
+ len = pathname != NULL ? strlen(pathname) : 0;
+ while (len > 1 && pathname[len - 1] == '/')
+ {
+ len--;
+ }
+
+ if ((len == 1 && pathname[0] == '.') ||
+ (len >= 2 && pathname[len - 1] == '.' && pathname[len - 2] == '/'))
+ {
+ ret = -EINVAL;
+ goto errout;
+ }
Review Comment:
This new pre-check correctly rejects a last component of `.` (e.g.,
`dir/.`), but POSIX also requires `EINVAL` when the last component is `..`
(e.g., `dir/..` or `..`). If the underlying lookup collapses `..` similarly (or
otherwise treats it specially), `rmdir(\"dir/..\")` could resolve unexpectedly.
Consider extending this check to reject a trailing `..` component as well.
##########
fs/vfs/fs_readlink.c:
##########
@@ -139,10 +139,10 @@ ssize_t readlink(FAR const char *path, FAR char *buf,
size_t bufsize)
goto errout_with_inode;
}
- /* Copy the link target path to the user-provided buffer. */
+ /* readlink() does not NUL-terminate: copy at most bufsize bytes */
- nread = strlcpy(buf, node->u.i_link, bufsize);
- nread = MIN(nread, bufsize);
+ nread = MIN(strlen(node->u.i_link), bufsize);
Review Comment:
Computing `strlen(node->u.i_link)` scans the entire link target even when
`bufsize` is small. Since `readlink()` only needs to copy up to `bufsize` bytes
(and return the number copied), prefer a bounded length computation (e.g.,
`strnlen(node->u.i_link, bufsize)`) to avoid unnecessary work for long link
targets.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]