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

commit be4d4c4eb7d5741b9433bf80d8af440caa2da938
Author: daichuan <[email protected]>
AuthorDate: Fri Oct 16 21:29:17 2026 +0800

    libc/net: Bound the output of ether_ntoa_r().
    
    ether_ntoa_r() formatted the address with sprintf() and no limit on the
    caller's buffer.  Add MAC_ADDR_STR_LEN, the size of the formatted
    address including the terminating NUL, next to the ether_ntoa APIs and
    format with snprintf() bounded by it.
    
    Signed-off-by: daichuan <[email protected]>
    Signed-off-by: ganjing <[email protected]>
---
 include/netinet/ether.h       | 4 ++++
 libs/libc/net/lib_etherntoa.c | 2 +-
 2 files changed, 5 insertions(+), 1 deletion(-)

diff --git a/include/netinet/ether.h b/include/netinet/ether.h
index c2f98154b63..f2cf0968ec0 100644
--- a/include/netinet/ether.h
+++ b/include/netinet/ether.h
@@ -35,6 +35,10 @@
  * Pre-processor Definitions
  ****************************************************************************/
 
+/* Size of the buffer ether_ntoa_r() writes: "xx:xx:xx:xx:xx:xx" + NUL */
+
+#define MAC_ADDR_STR_LEN                 (3 * ETHER_ADDR_LEN)
+
 /****************************************************************************
  * Public Type Definitions
  ****************************************************************************/
diff --git a/libs/libc/net/lib_etherntoa.c b/libs/libc/net/lib_etherntoa.c
index 31beaaf73af..ea5364bffd6 100644
--- a/libs/libc/net/lib_etherntoa.c
+++ b/libs/libc/net/lib_etherntoa.c
@@ -46,7 +46,7 @@
 
 FAR char *ether_ntoa_r(FAR const struct ether_addr *addr, FAR char *buf)
 {
-  sprintf(buf, "%02x:%02x:%02x:%02x:%02x:%02x",
+  snprintf(buf, MAC_ADDR_STR_LEN, "%02x:%02x:%02x:%02x:%02x:%02x",
           addr->ether_addr_octet[0], addr->ether_addr_octet[1],
           addr->ether_addr_octet[2], addr->ether_addr_octet[3],
           addr->ether_addr_octet[4], addr->ether_addr_octet[5]);

Reply via email to