[PATCH 2/3] nss: Improve network file parser (bz 32573)

Tobias Stoeckmann tobias@stoeckmann.org
Sun Jan 19 15:04:16 GMT 2025


Avoid malloca because /etc/networks lines can be arbitrarily long.
Instead of handcrafting the input for inet_network by adding ".0"
octets if they are missing, just left shift the result.

Also do not accept invalid entries, but ignore the line instead.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
---
 nss/nss_files/files-network.c | 20 ++++++--------------
 1 file changed, 6 insertions(+), 14 deletions(-)

diff --git a/nss/nss_files/files-network.c b/nss/nss_files/files-network.c
index ca94372024..f08daaf55f 100644
--- a/nss/nss_files/files-network.c
+++ b/nss/nss_files/files-network.c
@@ -42,7 +42,8 @@ LINE_PARSER

    STRING_FIELD (addr, isspace, 1);
    /* 'inet_network' does not add zeroes at the end if the network number
-      does not four byte values.  We add them ourselves if necessary.  */
+      does not contain four byte values.  We shift result ourselves if
+      necessary.  */
    cp = strchr (addr, '.');
    if (cp != NULL)
      {
@@ -56,20 +57,11 @@ LINE_PARSER
 	     ++n;
 	 }
      }
-   if (n < 4)
-     {
-       char *newp = (char *) alloca (strlen (addr) + (4 - n) * 2 + 1);
-       cp = stpcpy (newp, addr);
-       do
-	 {
-	   *cp++ = '.';
-	   *cp++ = '0';
-	 }
-       while (++n < 4);
-       *cp = '\0';
-       addr = newp;
-     }
    result->n_net = __inet_network (addr);
+   if (result->n_net == INADDR_NONE)
+     return 0;
+   if (n < 4)
+     result->n_net <<= 8 * (4 - n);
    result->n_addrtype = AF_INET;

  })
--
2.48.1



More information about the Libc-alpha mailing list