Copilot commented on code in PR #21749:
URL: https://github.com/apache/nuttx/pull/21749#discussion_r4564225115


##########
fs/rpmsgfs/rpmsgfs.h:
##########
@@ -106,7 +106,7 @@ begin_packed_struct struct rpmsgfs_lseek_s
   struct rpmsgfs_header_s header;
   int32_t                 fd;
   int32_t                 whence;

Review Comment:
   Introducing an `int64_t` field inside a packed struct makes `msg->offset` 
potentially unaligned. Directly reading/writing this field is undefined 
behavior in C and can fault on architectures that trap unaligned 64-bit 
accesses. A safer approach is to keep the on-wire layout packed but 
serialize/deserialize `offset` with `memcpy` (or an existing unaligned-access 
helper) into/from a properly aligned local `int64_t` before use.



##########
fs/rpmsgfs/rpmsgfs_server.c:
##########
@@ -528,7 +528,10 @@ static int rpmsgfs_lseek_handler(FAR struct rpmsg_endpoint 
*ept,
   filep = rpmsgfs_get_file(priv, msg->fd);
   if (filep != NULL)
     {
-      ret = file_seek(filep, msg->offset, msg->whence);
+      /* header.result is 32 bits, so return the position in offset */
+
+      msg->offset = file_seek(filep, msg->offset, msg->whence);
+      ret = msg->offset < 0 ? msg->offset : 0;

Review Comment:
   This code both (1) directly accesses `msg->offset` (now `int64_t`) in a 
packed message (risking unaligned access), and (2) assigns an `int64_t` 
negative value into `int ret` via the ternary, which relies on implicit 
narrowing. Consider using an aligned local variable for the seek result 
(loaded/stored via `memcpy` for the packed field) and set `ret` via an explicit 
cast only for the negative errno case.



##########
fs/rpmsgfs/rpmsgfs_client.c:
##########
@@ -638,8 +649,12 @@ off_t rpmsgfs_client_lseek(FAR void *handle, int fd,
     .whence = whence,
   };
 
-  return rpmsgfs_send_recv(handle, RPMSGFS_LSEEK, true,
-           (struct rpmsgfs_header_s *)&msg, sizeof(msg), NULL);
+  off_t ret;
+
+  ret = rpmsgfs_send_recv(handle, RPMSGFS_LSEEK, true,
+                          (struct rpmsgfs_header_s *)&msg, sizeof(msg),
+                          NULL);
+  return ret < 0 ? ret : msg.offset;

Review Comment:
   `rpmsgfs_send_recv()` returns an `int` status, but it’s stored in an `off_t` 
and then used as the error discriminator. This conflates 'status' and 
'position' types and can hide bugs if `rpmsgfs_send_recv()` ever returns a 
positive non-zero value (e.g., byte count). Prefer using `int status = 
rpmsgfs_send_recv(...);` and returning `status < 0 ? (off_t)status : 
msg.offset` (with packed-field access handled safely if needed).



-- 
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]

Reply via email to