This is an automated email from the ASF dual-hosted git repository.

GUIDINGLI pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx-apps.git

commit 4c65878c7f09815f66ac3a4fd6f3dac402203142
Author: zhangshuai39 <[email protected]>
AuthorDate: Tue Jun 16 15:31:17 2026 +0800

    netutils/ftpd: fix undefined behavior in PORT command
    
    Fix two issues that triggered UBSan in ftpd_command_port:
    
    1. When an octet value > 255 is received, the error response was sent
       but execution continued without returning. Return immediately after
       the 501 response to prevent the truncated value from being used.
    
    2. The shift expression `value[N] << 24` promoted uint8_t to signed
       int. For any byte >= 128, the result overflows int and triggers
       UB. Cast operands to unsigned int before shifting.
    
    Signed-off-by: zhangshuai39 <[email protected]>
    Assisted-by: Kiro:claude-opus-4
---
 netutils/ftpd/ftpd.c | 18 ++++++++----------
 1 file changed, 8 insertions(+), 10 deletions(-)

diff --git a/netutils/ftpd/ftpd.c b/netutils/ftpd/ftpd.c
index d9a24eeb8..4899c3fc7 100644
--- a/netutils/ftpd/ftpd.c
+++ b/netutils/ftpd/ftpd.c
@@ -2791,14 +2791,9 @@ static int ftpd_command_port(FAR struct ftpd_session_s 
*session)
       temp = atoi(str);
       if (temp < 0 || temp > 255)
         {
-          ret = ftpd_response(session->cmd.sd, session->txtimeout,
-                              g_respfmt1, 501, ' ',
-                              "Illegal PORT command");
-          if (ret < 0)
-            {
-              nerr("ERROR: ftpd_response failed: %d\n", ret);
-              return ret;
-            }
+          return ftpd_response(session->cmd.sd, session->txtimeout,
+                               g_respfmt1, 501, ' ',
+                               "Illegal PORT command");
         }
 
       value[index++] = (uint8_t)temp;
@@ -2818,10 +2813,13 @@ static int ftpd_command_port(FAR struct ftpd_session_s 
*session)
 
   session->data.addr.in4.sin_family = AF_INET;
 
-  utemp = (value[0] << 24) | (value[1] << 16) | (value[2] << 8) | (value[3]);
+  utemp = ((unsigned int)value[0] << 24) |
+          ((unsigned int)value[1] << 16) |
+          ((unsigned int)value[2] << 8) |
+          (unsigned int)value[3];
   session->data.addr.in4.sin_addr.s_addr = htonl((long)utemp);
 
-  utemp = (value[4] << 8) | (value[5]);
+  utemp = ((unsigned int)value[4] << 8) | (unsigned int)value[5];
   session->data.addr.in4.sin_port = htons((short)utemp);
 
 #else /* Follow command socket address */

Reply via email to