]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
support: Clean up NSS formatting and checking functions
authorFlorian Weimer <fweimer@redhat.com>
Thu, 12 Feb 2026 11:18:54 +0000 (12:18 +0100)
committerFlorian Weimer <fweimer@redhat.com>
Thu, 12 Feb 2026 11:18:54 +0000 (12:18 +0100)
Move <netdb.h> includes into the implementation files.  Use const
where appropriate.  Introduce support_check_nss and use it to
implement the check_* functions.

Reviewed-by: DJ Delorie <dj@redhat.com>
21 files changed:
nss/tst-nss-gai-actions.c
resolv/tst-resolv-ai_idn-common.c
resolv/tst-resolv-basic.c
resolv/tst-resolv-getaddrinfo-fqdn.c
resolv/tst-resolv-search.c
resolv/tst-resolv-semi-failure.c
resolv/tst-resolv-short-response.c
resolv/tst-resolv-txnid-collision.c
support/Makefile
support/check_addrinfo.c
support/check_dns_packet.c
support/check_hostent.c
support/check_netent.c
support/check_nss.h
support/format_nss.h
support/support_check_nss.c [new file with mode: 0644]
support/support_format_address_family.c
support/support_format_addrinfo.c
support/support_format_herrno.c
support/support_format_hostent.c
support/support_format_netent.c

index e42bbd6caeaa37a6fc377fa4f8e6b30348c71b9b..4dd8a0918ffccb7e95c5e337d17dad38bfbf57e6 100644 (file)
@@ -18,6 +18,7 @@
 
 #include <dlfcn.h>
 #include <gnu/lib-names.h>
+#include <netdb.h>
 #include <nss.h>
 #include <stdio.h>
 #include <stdlib.h>
index d2a17d6f25e181e2f23b59750829af1c5efc09cc..f55405eb2785ce585e0af4d95a5d5a29dcd78ab3 100644 (file)
@@ -20,6 +20,7 @@
    0, depending on whether a UTF-8 locale is used or a Latin-1
    locale.  */
 
+#include <netdb.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
index d64da33898f97e662d503518d8071a3e46ba44f4..5457d68e9eddc735151fd676091592be4ff046b2 100644 (file)
@@ -17,6 +17,7 @@
    <https://www.gnu.org/licenses/>.  */
 
 #include <errno.h>
+#include <netdb.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
index 8703afc0428246b0726fa6837995d869125ad3ba..094b443f6ad2548f73fb63ea0141fd417fc4daea 100644 (file)
@@ -17,6 +17,7 @@
    License along with the GNU C Library; if not, see
    <https://www.gnu.org/licenses/>.  */
 
+#include <netdb.h>
 #include <resolv.h>
 #include <stdlib.h>
 #include <string.h>
index 2c0f80da213587cd74d352538301300eea294c3b..5d905c0188edd7d995e598215d35fd8235290bb3 100644 (file)
@@ -16,6 +16,7 @@
    License along with the GNU C Library; if not, see
    <https://www.gnu.org/licenses/>.  */
 
+#include <netdb.h>
 #include <resolv.h>
 #include <stdlib.h>
 #include <string.h>
index f82840cc569cf1e1d920f2d4f3ffd4bff6ad9516..da5a0c72eaf754d75fe5b8c21140fa1032468e26 100644 (file)
@@ -16,6 +16,7 @@
    License along with the GNU C Library; if not, see
    <https://www.gnu.org/licenses/>.  */
 
+#include <netdb.h>
 #include <resolv.h>
 #include <support/check.h>
 #include <support/resolv_test.h>
index ddb91f158f85426aae8e4161ff06779667577dcb..c39afe67fcd5b23719a2ae22ca61f42eb38b3ee5 100644 (file)
@@ -16,6 +16,7 @@
    License along with the GNU C Library; if not, see
    <https://www.gnu.org/licenses/>.  */
 
+#include <netdb.h>
 #include <resolv.h>
 #include <support/check.h>
 #include <support/resolv_test.h>
index a7af10a9387c537c13367fc42db6e040b4ffb1f9..b9f95fac513975a89cfc8fffc3eb77ce3651e8bd 100644 (file)
@@ -22,6 +22,7 @@
 #include <resolv_context.h>
 #include <stdbool.h>
 #include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
 #include <support/check.h>
 #include <support/check_nss.h>
index bd60466ad06bf67d3219f0e13d348009c671d3e6..935503c9b3c4a2e33efbad1e9420fb93473e3506 100644 (file)
@@ -46,6 +46,7 @@ libsupport-routines = \
   support_can_chroot \
   support_capture_subprocess \
   support_capture_subprocess_check \
+  support_check_nss \
   support_check_stat_fd \
   support_check_stat_path \
   support_chroot \
index d643737469985320b211f7548db7b193700369b5..e42184a662f2bd45c1cba17358a6deffcb9b9a34 100644 (file)
 
 #include <support/check_nss.h>
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <support/check.h>
 #include <support/format_nss.h>
-#include <support/run_diff.h>
 
 void
-check_addrinfo (const char *query_description, struct addrinfo *ai, int ret,
-                const char *expected)
+check_addrinfo (const char *query_description, const struct addrinfo *ai,
+                int ret, const char *expected)
 {
   char *formatted = support_format_addrinfo (ai, ret);
-  if (strcmp (formatted, expected) != 0)
-    {
-      support_record_failure ();
-      printf ("error: addrinfo comparison failure\n");
-      if (query_description != NULL)
-        printf ("query: %s\n", query_description);
-      support_run_diff ("expected", expected,
-                        "actual", formatted);
-    }
-  free (formatted);
+  support_check_nss (query_description, "addrinfo", formatted, expected);
 }
index 57e6623ba4ede929988ebbbaf6906a7917f4b856..f143ef36c07597d913b4287dc883caf52b02da6f 100644 (file)
 
 #include <support/check_nss.h>
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <support/check.h>
 #include <support/format_nss.h>
-#include <support/run_diff.h>
 
 void
 check_dns_packet (const char *query_description,
@@ -31,13 +26,5 @@ check_dns_packet (const char *query_description,
                   const char *expected)
 {
   char *formatted = support_format_dns_packet (buffer, length);
-  if (strcmp (formatted, expected) != 0)
-    {
-      support_record_failure ();
-      printf ("error: packet comparison failure\n");
-      if (query_description != NULL)
-        printf ("query: %s\n", query_description);
-      support_run_diff ("expected", expected, "actual", formatted);
-    }
-  free (formatted);
+  support_check_nss (query_description, "packet", formatted, expected);
 }
index 09123486eddd68ecd494ca6ddd7f87bc114f1d19..72ddb25ba108821cf9522924461941395055ef93 100644 (file)
 
 #include <support/check_nss.h>
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <support/check.h>
 #include <support/format_nss.h>
-#include <support/run_diff.h>
 
 void
-check_hostent (const char *query_description, struct hostent *h,
+check_hostent (const char *query_description, const struct hostent *h,
                const char *expected)
 {
   char *formatted = support_format_hostent (h);
-  if (strcmp (formatted, expected) != 0)
-    {
-      support_record_failure ();
-      printf ("error: hostent comparison failure\n");
-      if (query_description != NULL)
-        printf ("query: %s\n", query_description);
-      support_run_diff ("expected", expected,
-                        "actual", formatted);
-    }
-  free (formatted);
+  support_check_nss (query_description, "hostent", formatted, expected);
 }
index 822e1aba3f9693d5e9be999e95505fede9c7a1c4..fa7a892bb07e0ceb075d8e1d86ed3a4d6dd1ea5d 100644 (file)
 
 #include <support/check_nss.h>
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <support/check.h>
 #include <support/format_nss.h>
-#include <support/run_diff.h>
 
 void
-check_netent (const char *query_description, struct netent *e,
+check_netent (const char *query_description, const struct netent *e,
               const char *expected)
 {
   char *formatted = support_format_netent (e);
-  if (strcmp (formatted, expected) != 0)
-    {
-      support_record_failure ();
-      printf ("error: netent comparison failure\n");
-      if (query_description != NULL)
-        printf ("query: %s\n", query_description);
-      support_run_diff ("expected", expected,
-                        "actual", formatted);
-    }
-  free (formatted);
+  support_check_nss (query_description, "netent", formatted, expected);
 }
index ec87fa44a1627ab257df55b48efa1aeb6c7a6906..6bacb9978f6425b181f2ea213c94ff25f1c67a5d 100644 (file)
 #ifndef SUPPORT_CHECK_NSS_H
 #define SUPPORT_CHECK_NSS_H
 
-#include <netdb.h>
 #include <sys/cdefs.h>
+#include <stddef.h>
 
 __BEGIN_DECLS
 
+struct addrinfo;
+struct hostent;
+struct netent;
+
 /* Compare the data structures against the expected values (which have
    to be formatted according to the support_format_* functions in
    <support/format_nss.h>).  If there is a difference, a delayed test
    failure is recorded, and a diff is written to standard output.  */
 void check_addrinfo (const char *query_description,
-                     struct addrinfo *, int ret, const char *expected);
+                     const struct addrinfo *, int ret, const char *expected);
 void check_dns_packet (const char *query_description,
                        const unsigned char *, size_t, const char *expected);
 void check_hostent (const char *query_description,
-                    struct hostent *, const char *expected);
+                    const struct hostent *, const char *expected);
 void check_netent (const char *query_description,
-                   struct netent *, const char *expected);
+                   const struct netent *, const char *expected);
+
+/* Helper routine for implementing the functions above.  Report an
+   error if ACTUAL and EXPECTED are not equal.  ACTUAL is always freed.  */
+void support_check_nss (const char *query_description,
+                        const char *type_name,
+                        char *actual, const char *expected);
 
 __END_DECLS
 
index 7ce7cc9c7152245c136ffe6ca3244ab15ea2bc49..a81658cfcbcaa1fdea7c3dd753cf242483977cf3 100644 (file)
 #ifndef SUPPORT_FORMAT_NSS_H
 #define SUPPORT_FORMAT_NSS_H
 
-#include <netdb.h>
 #include <sys/cdefs.h>
+#include <stddef.h>
 
 __BEGIN_DECLS
 
+struct addrinfo;
+struct hostent;
+struct netent;
+
 /* The following functions format their arguments as human-readable
    strings (which can span multiple lines).  The caller must free the
    returned buffer.  For NULL pointers or failure status arguments,
    error variables such as h_errno and errno are included in the
    result.  */
 char *support_format_address_family (int);
-char *support_format_addrinfo (struct addrinfo *, int ret);
+char *support_format_addrinfo (const struct addrinfo *, int ret);
 char *support_format_dns_packet (const unsigned char *buffer, size_t length);
 char *support_format_herrno (int);
-char *support_format_hostent (struct hostent *);
-char *support_format_netent (struct netent *);
+char *support_format_hostent (const struct hostent *);
+char *support_format_netent (const struct netent *);
 
 __END_DECLS
 
diff --git a/support/support_check_nss.c b/support/support_check_nss.c
new file mode 100644 (file)
index 0000000..5c91af3
--- /dev/null
@@ -0,0 +1,41 @@
+/* Report a NSS struct comparison failure.
+   Copyright (C) 2016-2026 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#include <support/check_nss.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <support/check.h>
+#include <support/run_diff.h>
+
+void
+support_check_nss (const char *query_description, const char *type_name,
+                   char *actual, const char *expected)
+{
+  if (strcmp (actual, expected) != 0)
+    {
+      support_record_failure ();
+      printf ("error: %s comparison failure\n", type_name);
+      if (query_description != NULL)
+        printf ("query: %s\n", query_description);
+      support_run_diff ("expected", expected,
+                        "actual", actual);
+    }
+  free (actual);
+}
index 5be0f8ab670b823f19b36997a3fbf237c148b398..30c4c69fc39832526513ee6145037a1552694a30 100644 (file)
@@ -18,6 +18,7 @@
 
 #include <support/format_nss.h>
 
+#include <netdb.h>
 #include <support/support.h>
 
 char *
index f6fda5ab49d5fded69d82443b0e8b0a12efc105e..dc6041f74bf6f9327cb5e2b0574d2eb9206abc6b 100644 (file)
@@ -20,6 +20,7 @@
 
 #include <arpa/inet.h>
 #include <errno.h>
+#include <netdb.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -41,8 +42,8 @@ socket_address_length (int family)
 }
 
 static void
-format_ai_flags_1 (FILE *out, struct addrinfo *ai, int flag, const char *name,
-                   int * flags_printed)
+format_ai_flags_1 (FILE *out, const struct addrinfo *ai, int flag,
+                   const char *name, int *flags_printed)
 {
   if ((ai->ai_flags & flag) != 0)
     fprintf (out, " %s", name);
@@ -50,7 +51,7 @@ format_ai_flags_1 (FILE *out, struct addrinfo *ai, int flag, const char *name,
 }
 
 static void
-format_ai_flags (FILE *out, struct addrinfo *ai)
+format_ai_flags (FILE *out, const struct addrinfo *ai)
 {
   if (ai == NULL)
     return;
@@ -91,7 +92,7 @@ format_ai_flags (FILE *out, struct addrinfo *ai)
 }
 
 static void
-format_ai_canonname (FILE *out, struct addrinfo *ai)
+format_ai_canonname (FILE *out, const struct addrinfo *ai)
 {
   if (ai == NULL)
     return;
@@ -113,7 +114,7 @@ format_ai_canonname (FILE *out, struct addrinfo *ai)
 }
 
 static void
-format_ai_one (FILE *out, struct addrinfo *ai)
+format_ai_one (FILE *out, const struct addrinfo *ai)
 {
   {
     char type_buf[32];
@@ -200,7 +201,7 @@ format_ai_one (FILE *out, struct addrinfo *ai)
 
 /* Format all the addresses in one address family.  */
 static void
-format_ai_family (FILE *out, struct addrinfo *ai, int family)
+format_ai_family (FILE *out, const struct addrinfo *ai, int family)
 {
   while (ai)
     {
@@ -211,7 +212,7 @@ format_ai_family (FILE *out, struct addrinfo *ai, int family)
 }
 
 char *
-support_format_addrinfo (struct addrinfo *ai, int ret)
+support_format_addrinfo (const struct addrinfo *ai, int ret)
 {
   int errno_copy = errno;
 
index 4644b022d45d7076c752e7bc294c0ef267d1caa5..9c3a52c78e219d5f90feec68cc71b5619cd07943 100644 (file)
@@ -18,6 +18,7 @@
 
 #include <support/format_nss.h>
 
+#include <netdb.h>
 #include <support/support.h>
 
 char *
index e1b3f078d4d0f7f06e4be132a56fe71ce71dbc43..f76ec459cdb80e8ca028f7902f23c0ddaab963e2 100644 (file)
@@ -20,6 +20,7 @@
 
 #include <arpa/inet.h>
 #include <errno.h>
+#include <netdb.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <support/support.h>
@@ -39,7 +40,7 @@ address_length (int family)
 }
 
 char *
-support_format_hostent (struct hostent *h)
+support_format_hostent (const struct hostent *h)
 {
   if (h == NULL)
     {
index e96e71c7372dcb4ff185aeeba2b3fb779502b321..7f08476dd3d18fe663ed28d84e4612338878be8d 100644 (file)
 #include <support/format_nss.h>
 
 #include <arpa/inet.h>
+#include <netdb.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <support/support.h>
 #include <support/xmemstream.h>
 
 char *
-support_format_netent (struct netent *e)
+support_format_netent (const struct netent *e)
 {
   if (e == NULL)
     {