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

acassis 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 89266d89f51 libs/libc/netdb: Fail the build if NETDB_BUFSIZE is too 
small.
89266d89f51 is described below

commit 89266d89f5163075df37c9a620dd6c0ef5d78d3d
Author: Marco Casaroli <[email protected]>
AuthorDate: Thu Nov 26 15:36:27 2026 +0100

    libs/libc/netdb: Fail the build if NETDB_BUFSIZE is too small.
    
    getaddrinfo(), gethostbyname() and gethostbyaddr() use a buffer of
    CONFIG_NETDB_BUFSIZE bytes.  The look-up puts struct hostent_info_s at
    the start of the buffer, and the size of this struct grows with
    CONFIG_NETDB_MAX_IPv4ADDR and CONFIG_NETDB_MAX_IPv6ADDR (the hosts
    file form with CONFIG_NETDB_MAX_ALTNAMES).  With 8 IPv4 and 8 IPv6
    addresses on a 64-bit target the struct has 280 bytes, more than the
    default buffer of 256 bytes.  Then each look-up fails with ERANGE,
    also of a numeric address such as "10.0.2.2": getaddrinfo() gives
    EAI_NONAME and gethostbyname() gives HOST_NOT_FOUND.  Nothing tells the
    user that the buffer is the cause.
    
    Add the macro NETDB_CHECK_BUFSIZE() to lib_netdb.h.  It is a
    static_assert() that the buffer holds the struct of the caller and
    the host data.  lib_gethostentbynamer.c and lib_parsehostfile.c use
    it with their struct, one IPv6 address and its text.  Say in the
    Kconfig help that the buffer must grow with the address counts.  The
    default configurations do not change.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 libs/libc/netdb/Kconfig                 |  5 +++++
 libs/libc/netdb/lib_gethostentbynamer.c |  5 +++++
 libs/libc/netdb/lib_netdb.h             | 15 +++++++++++++++
 libs/libc/netdb/lib_parsehostfile.c     |  5 +++++
 4 files changed, 30 insertions(+)

diff --git a/libs/libc/netdb/Kconfig b/libs/libc/netdb/Kconfig
index c647b52652a..a808ba3c1d9 100644
--- a/libs/libc/netdb/Kconfig
+++ b/libs/libc/netdb/Kconfig
@@ -71,6 +71,11 @@ config NETDB_BUFSIZE
        int "gethostbyname/gethostbyaddr buffer size"
        depends on LIBC_NETDB
        default 256
+       ---help---
+               The size of the buffer of getaddrinfo(), gethostbyname() and
+               gethostbyaddr().  It holds the address list of one host, so it
+               must grow with NETDB_MAX_IPv4ADDR, NETDB_MAX_IPv6ADDR and
+               NETDB_MAX_ALTNAMES.  The build fails if it is too small.
 
 if NET_IPv4
 
diff --git a/libs/libc/netdb/lib_gethostentbynamer.c 
b/libs/libc/netdb/lib_gethostentbynamer.c
index bf9d3478c5a..c3897c862c8 100644
--- a/libs/libc/netdb/lib_gethostentbynamer.c
+++ b/libs/libc/netdb/lib_gethostentbynamer.c
@@ -58,6 +58,11 @@ struct hostent_info_s
   char      hi_data[1];
 };
 
+/* The buffer must hold struct hostent_info_s, one address and its text */
+
+NETDB_CHECK_BUFSIZE(struct hostent_info_s,
+                    sizeof(struct in6_addr) + INET6_ADDRSTRLEN);
+
 /****************************************************************************
  * Private functions
  ****************************************************************************/
diff --git a/libs/libc/netdb/lib_netdb.h b/libs/libc/netdb/lib_netdb.h
index 33c54f7c1ce..68c7ad52135 100644
--- a/libs/libc/netdb/lib_netdb.h
+++ b/libs/libc/netdb/lib_netdb.h
@@ -29,6 +29,7 @@
 
 #include <nuttx/config.h>
 
+#include <assert.h>
 #include <netdb.h>
 #include <stdbool.h>
 #include <stdio.h>
@@ -81,6 +82,20 @@
 #  define NETDB_HOSTBYNAME_FAMILY AF_UNSPEC
 #endif
 
+/* getaddrinfo(), gethostbyname() and gethostbyaddr() use a buffer of
+ * CONFIG_NETDB_BUFSIZE bytes.  A look-up puts its struct hostent_info_s at
+ * the start of this buffer and the host data after it.  If the buffer
+ * cannot hold the struct and the host data, each look-up fails with ERANGE
+ * at run time.  This macro fails the build instead: the buffer must hold
+ * the struct "type" plus "extra" bytes.
+ */
+
+#define NETDB_CHECK_BUFSIZE(type, extra) \
+  static_assert(sizeof(type) + (extra) <= CONFIG_NETDB_BUFSIZE, \
+                "CONFIG_NETDB_BUFSIZE is too small for " \
+                "CONFIG_NETDB_MAX_IPv4ADDR, CONFIG_NETDB_MAX_IPv6ADDR " \
+                "and CONFIG_NETDB_MAX_ALTNAMES")
+
 /****************************************************************************
  * Public Types
  ****************************************************************************/
diff --git a/libs/libc/netdb/lib_parsehostfile.c 
b/libs/libc/netdb/lib_parsehostfile.c
index 07e1bad1606..f7da523387a 100644
--- a/libs/libc/netdb/lib_parsehostfile.c
+++ b/libs/libc/netdb/lib_parsehostfile.c
@@ -65,6 +65,11 @@ struct hostent_info_s
   char      hi_data[1];
 };
 
+/* The buffer must hold struct hostent_info_s, one address and a name */
+
+NETDB_CHECK_BUFSIZE(struct hostent_info_s,
+                    sizeof(struct in6_addr) + INET6_ADDRSTRLEN);
+
 /****************************************************************************
  * Private functions
  ****************************************************************************/

Reply via email to