[PATCH 3/6] nss: Use test resolver for bug-erange
Adhemerval Zanella
adhemerval.zanella@linaro.org
Thu Jul 9 17:48:16 GMT 2026
Rewrite the gethostbyname_r ERANGE test to use the resolv_test
framework instead of querying the network, so it can run as a regular
test.
---
nss/Makefile | 7 ++--
nss/bug-erange.c | 90 +++++++++++++++++++++++++++++-------------------
2 files changed, 57 insertions(+), 40 deletions(-)
diff --git a/nss/Makefile b/nss/Makefile
index bbcb7201699..4cab4f74c42 100644
--- a/nss/Makefile
+++ b/nss/Makefile
@@ -312,6 +312,7 @@ tests-internal := \
# tests-internal
tests := \
+ bug-erange \
bug17079 \
test-digits-dots \
test-netdb \
@@ -343,10 +344,6 @@ tests := \
tst-shadow \
# tests
-xtests := \
- bug-erange \
- # xtests
-
tests-container := \
tst-initgroups1 \
tst-initgroups2 \
@@ -517,6 +514,8 @@ $(objpfx)tst-getaddrinfo4: \
$(common-objpfx)resolv/libresolv.so $(shared-thread-library)
$(objpfx)tst-getaddrinfo5: \
$(common-objpfx)resolv/libresolv.so $(shared-thread-library)
+$(objpfx)bug-erange: \
+ $(common-objpfx)resolv/libresolv.so $(shared-thread-library)
$(objpfx)tst-default-domain: $(objpfx)nisdomain.os
diff --git a/nss/bug-erange.c b/nss/bug-erange.c
index b709418b5cf..04b0a40c9ab 100644
--- a/nss/bug-erange.c
+++ b/nss/bug-erange.c
@@ -1,52 +1,70 @@
-/* Test case for gethostbyname_r bug when buffer expansion required. */
+/* Test case for gethostbyname_r bug when buffer expansion required.
+ Copyright (C) 2004-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 <netdb.h>
-#include <arpa/inet.h>
#include <errno.h>
-#include <string.h>
-#include <stdio.h>
+#include <netdb.h>
#include <stdlib.h>
-#include <unistd.h>
+#include <arpa/nameser.h>
+#include <support/check.h>
+#include <support/resolv_test.h>
+#include <support/support.h>
-int
-main (void)
+static void
+response (const struct resolv_response_context *ctx,
+ struct resolv_response_builder *b,
+ const char *qname, uint16_t qclass, uint16_t qtype)
{
- const char *host = "www.gnu.org";
+ resolv_response_init (b, (struct resolv_response_flags) { });
+ resolv_response_add_question (b, qname, qclass, qtype);
+ resolv_response_section (b, ns_s_an);
+ resolv_response_open_record (b, qname, qclass, qtype, 0);
+ resolv_response_add_data (b, "\xc0\x00\x02\x01", 4);
+ resolv_response_close_record (b);
+}
- /* This code approximates the example code in the library manual. */
+static int
+do_test (void)
+{
+ struct resolv_test *obj = resolv_test_start
+ ((struct resolv_redirect_config) { .response_callback = response });
struct hostent hostbuf, *hp;
- size_t hstbuflen;
- char *tmphstbuf;
- int res;
- int herr;
+ char *buf;
+ size_t buflen = 1; /* Make it way small to ensure ERANGE. */
+ int res, herr;
- hstbuflen = 16; /* Make it way small to ensure ERANGE. */
- /* Allocate buffer, remember to free it to avoid memory leakage. */
- tmphstbuf = malloc (hstbuflen);
-
- while ((res = gethostbyname_r (host, &hostbuf, tmphstbuf, hstbuflen,
+ buf = xmalloc (buflen);
+ while ((res = gethostbyname_r ("example.net", &hostbuf, buf, buflen,
&hp, &herr)) == ERANGE)
{
- /* Enlarge the buffer. */
- hstbuflen *= 2;
- tmphstbuf = realloc (tmphstbuf, hstbuflen);
+ buflen *= 2;
+ buf = xrealloc (buf, buflen);
}
- if (res != 0 || hp == NULL)
- {
- printf ("gethostbyname_r failed: %s (errno: %m)\n", strerror (res));
+ TEST_COMPARE (res, 0);
+ TEST_VERIFY_EXIT (hp != NULL);
+ TEST_COMPARE (hp->h_addrtype, AF_INET);
+ TEST_COMPARE (hp->h_length, 4);
+ TEST_COMPARE_BLOB (hp->h_addr_list[0], 4, "\xc0\x00\x02\x01", 4);
- if (access ("/etc/resolv.conf", R_OK))
- {
- puts ("DNS probably not set up");
- return 0;
- }
-
- return 1;
- }
-
- printf ("Got: %s %s\n", hp->h_name,
- inet_ntoa (*(struct in_addr *) hp->h_addr));
+ free (buf);
+ resolv_test_end (obj);
return 0;
}
+
+#include <support/test-driver.c>
--
2.43.0
More information about the Libc-alpha
mailing list