[PATCH v2 1/5] support: Clean up NSS formatting and checking functions
Florian Weimer
fweimer@redhat.com
Wed Feb 11 15:39:42 GMT 2026
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>
---
nss/tst-nss-gai-actions.c | 1 +
resolv/tst-resolv-ai_idn-common.c | 1 +
resolv/tst-resolv-basic.c | 1 +
resolv/tst-resolv-getaddrinfo-fqdn.c | 1 +
resolv/tst-resolv-search.c | 1 +
resolv/tst-resolv-semi-failure.c | 1 +
resolv/tst-resolv-short-response.c | 1 +
resolv/tst-resolv-txnid-collision.c | 1 +
support/Makefile | 1 +
support/check_addrinfo.c | 20 ++----------
support/check_dns_packet.c | 15 +--------
support/check_hostent.c | 18 ++---------
support/check_netent.c | 18 ++---------
support/check_nss.h | 18 ++++++++---
support/format_nss.h | 12 +++++---
support/support_check_nss.c | 41 +++++++++++++++++++++++++
support/support_format_address_family.c | 1 +
support/support_format_addrinfo.c | 15 ++++-----
support/support_format_herrno.c | 1 +
support/support_format_hostent.c | 3 +-
support/support_format_netent.c | 3 +-
21 files changed, 94 insertions(+), 80 deletions(-)
create mode 100644 support/support_check_nss.c
diff --git a/nss/tst-nss-gai-actions.c b/nss/tst-nss-gai-actions.c
index e42bbd6cae..4dd8a0918f 100644
--- a/nss/tst-nss-gai-actions.c
+++ b/nss/tst-nss-gai-actions.c
@@ -18,6 +18,7 @@
#include <dlfcn.h>
#include <gnu/lib-names.h>
+#include <netdb.h>
#include <nss.h>
#include <stdio.h>
#include <stdlib.h>
diff --git a/resolv/tst-resolv-ai_idn-common.c b/resolv/tst-resolv-ai_idn-common.c
index d2a17d6f25..f55405eb27 100644
--- a/resolv/tst-resolv-ai_idn-common.c
+++ b/resolv/tst-resolv-ai_idn-common.c
@@ -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>
diff --git a/resolv/tst-resolv-basic.c b/resolv/tst-resolv-basic.c
index d64da33898..5457d68e9e 100644
--- a/resolv/tst-resolv-basic.c
+++ b/resolv/tst-resolv-basic.c
@@ -17,6 +17,7 @@
<https://www.gnu.org/licenses/>. */
#include <errno.h>
+#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
diff --git a/resolv/tst-resolv-getaddrinfo-fqdn.c b/resolv/tst-resolv-getaddrinfo-fqdn.c
index 8703afc042..094b443f6a 100644
--- a/resolv/tst-resolv-getaddrinfo-fqdn.c
+++ b/resolv/tst-resolv-getaddrinfo-fqdn.c
@@ -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>
diff --git a/resolv/tst-resolv-search.c b/resolv/tst-resolv-search.c
index 2c0f80da21..5d905c0188 100644
--- a/resolv/tst-resolv-search.c
+++ b/resolv/tst-resolv-search.c
@@ -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>
diff --git a/resolv/tst-resolv-semi-failure.c b/resolv/tst-resolv-semi-failure.c
index f82840cc56..da5a0c72ea 100644
--- a/resolv/tst-resolv-semi-failure.c
+++ b/resolv/tst-resolv-semi-failure.c
@@ -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>
diff --git a/resolv/tst-resolv-short-response.c b/resolv/tst-resolv-short-response.c
index ddb91f158f..c39afe67fc 100644
--- a/resolv/tst-resolv-short-response.c
+++ b/resolv/tst-resolv-short-response.c
@@ -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>
diff --git a/resolv/tst-resolv-txnid-collision.c b/resolv/tst-resolv-txnid-collision.c
index a7af10a938..b9f95fac51 100644
--- a/resolv/tst-resolv-txnid-collision.c
+++ b/resolv/tst-resolv-txnid-collision.c
@@ -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>
diff --git a/support/Makefile b/support/Makefile
index bd60466ad0..935503c9b3 100644
--- a/support/Makefile
+++ b/support/Makefile
@@ -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 \
diff --git a/support/check_addrinfo.c b/support/check_addrinfo.c
index d643737469..e42184a662 100644
--- a/support/check_addrinfo.c
+++ b/support/check_addrinfo.c
@@ -18,26 +18,12 @@
#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);
}
diff --git a/support/check_dns_packet.c b/support/check_dns_packet.c
index 57e6623ba4..f143ef36c0 100644
--- a/support/check_dns_packet.c
+++ b/support/check_dns_packet.c
@@ -18,12 +18,7 @@
#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);
}
diff --git a/support/check_hostent.c b/support/check_hostent.c
index 09123486ed..72ddb25ba1 100644
--- a/support/check_hostent.c
+++ b/support/check_hostent.c
@@ -18,26 +18,12 @@
#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);
}
diff --git a/support/check_netent.c b/support/check_netent.c
index 822e1aba3f..fa7a892bb0 100644
--- a/support/check_netent.c
+++ b/support/check_netent.c
@@ -18,26 +18,12 @@
#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);
}
diff --git a/support/check_nss.h b/support/check_nss.h
index ec87fa44a1..6bacb9978f 100644
--- a/support/check_nss.h
+++ b/support/check_nss.h
@@ -19,23 +19,33 @@
#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
diff --git a/support/format_nss.h b/support/format_nss.h
index 7ce7cc9c71..a81658cfcb 100644
--- a/support/format_nss.h
+++ b/support/format_nss.h
@@ -19,22 +19,26 @@
#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
index 0000000000..5c91af32cb
--- /dev/null
+++ b/support/support_check_nss.c
@@ -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);
+}
diff --git a/support/support_format_address_family.c b/support/support_format_address_family.c
index 5be0f8ab67..30c4c69fc3 100644
--- a/support/support_format_address_family.c
+++ b/support/support_format_address_family.c
@@ -18,6 +18,7 @@
#include <support/format_nss.h>
+#include <netdb.h>
#include <support/support.h>
char *
diff --git a/support/support_format_addrinfo.c b/support/support_format_addrinfo.c
index f6fda5ab49..dc6041f74b 100644
--- a/support/support_format_addrinfo.c
+++ b/support/support_format_addrinfo.c
@@ -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;
diff --git a/support/support_format_herrno.c b/support/support_format_herrno.c
index 4644b022d4..9c3a52c78e 100644
--- a/support/support_format_herrno.c
+++ b/support/support_format_herrno.c
@@ -18,6 +18,7 @@
#include <support/format_nss.h>
+#include <netdb.h>
#include <support/support.h>
char *
diff --git a/support/support_format_hostent.c b/support/support_format_hostent.c
index e1b3f078d4..f76ec459cd 100644
--- a/support/support_format_hostent.c
+++ b/support/support_format_hostent.c
@@ -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)
{
diff --git a/support/support_format_netent.c b/support/support_format_netent.c
index e96e71c737..7f08476dd3 100644
--- a/support/support_format_netent.c
+++ b/support/support_format_netent.c
@@ -19,13 +19,14 @@
#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)
{
base-commit: 807b3d5e48ffa63561de6cf5b4b8f6b877cd9880
--
2.53.0
More information about the Libc-alpha
mailing list