resolv: Set h_errno during resolver context allocation failure
diff mbox series

Message ID 871s71m2cn.fsf@oldenburg.str.redhat.com
State New
Headers show
Series
  • resolv: Set h_errno during resolver context allocation failure
Related show

Commit Message

Florian Weimer Dec. 1, 2018, 8:14 p.m. UTC
This simplifies the libresolv/libc interface.

In addition, this change fixes a minor bug where the res_h_errno
member of the per-thread (_res) state was updated, not the member
of the specified resolver state.

2018-12-01  Florian Weimer  <fweimer@redhat.com>

	* resolv/res_query.c (context_query_common, context_search_common)
	(context_querydomain_common, context_hostalias_common):
	Do not set h_errno on failure.
	* resolv/res_send.c (context_send_common): Likewise.
	* resolv/resolv_context.c (context_alloc): Set h_errno on memory
	allocation failure.

Patch
diff mbox series

diff --git a/resolv/res_query.c b/resolv/res_query.c
index ebbe5a6a4e..047a133772 100644
--- a/resolv/res_query.c
+++ b/resolv/res_query.c
@@ -285,10 +285,7 @@  context_query_common (struct resolv_context *ctx,
 		      unsigned char *answer, int anslen)
 {
   if (ctx == NULL)
-    {
-      RES_SET_H_ERRNO (&_res, NETDB_INTERNAL);
-      return -1;
-    }
+    return -1;
   int result = __res_context_query (ctx, name, class, type, answer, anslen,
 				    NULL, NULL, NULL, NULL, NULL);
   __resolv_context_put (ctx);
@@ -524,10 +521,7 @@  context_search_common (struct resolv_context *ctx,
 		       unsigned char *answer, int anslen)
 {
   if (ctx == NULL)
-    {
-      RES_SET_H_ERRNO (&_res, NETDB_INTERNAL);
-      return -1;
-    }
+    return -1;
   int result = __res_context_search (ctx, name, class, type, answer, anslen,
 				     NULL, NULL, NULL, NULL, NULL);
   __resolv_context_put (ctx);
@@ -603,10 +597,7 @@  context_querydomain_common (struct resolv_context *ctx,
 			    unsigned char *answer, int anslen)
 {
   if (ctx == NULL)
-    {
-      RES_SET_H_ERRNO (&_res, NETDB_INTERNAL);
-      return -1;
-    }
+    return -1;
   int result = __res_context_querydomain (ctx, name, domain, class, type,
 					  answer, anslen,
 					  NULL, NULL, NULL, NULL, NULL);
@@ -681,10 +672,7 @@  context_hostalias_common (struct resolv_context *ctx,
 			  const char *name, char *dst, size_t siz)
 {
   if (ctx == NULL)
-    {
-      RES_SET_H_ERRNO (&_res, NETDB_INTERNAL);
-      return NULL;
-    }
+    return NULL;
   const char *result = __res_context_hostalias (ctx, name, dst, siz);
   __resolv_context_put (ctx);
   return result;
diff --git a/resolv/res_send.c b/resolv/res_send.c
index 47e9de1f5b..c966f0ea67 100644
--- a/resolv/res_send.c
+++ b/resolv/res_send.c
@@ -554,10 +554,7 @@  context_send_common (struct resolv_context *ctx,
 		     unsigned char *ans, int anssiz)
 {
   if (ctx == NULL)
-    {
-      RES_SET_H_ERRNO (&_res, NETDB_INTERNAL);
-      return -1;
-    }
+    return -1;
   int result = __res_context_send (ctx, buf, buflen, NULL, 0, ans, anssiz,
 				   NULL, NULL, NULL, NULL, NULL);
   __resolv_context_put (ctx);
diff --git a/resolv/resolv_context.c b/resolv/resolv_context.c
index 4bd79111f0..7db8272292 100644
--- a/resolv/resolv_context.c
+++ b/resolv/resolv_context.c
@@ -22,6 +22,7 @@ 
 
 #include <assert.h>
 #include <errno.h>
+#include <netdb.h>
 #include <stdlib.h>
 #include <stdio.h>
 
@@ -126,13 +127,18 @@  maybe_init (struct resolv_context *ctx, bool preinit)
 }
 
 /* Allocate a new context object and initialize it.  The object is put
-   on the current list.  */
+   on the current list.  On memory allocation failure, h_errno is set
+   to NETDB_INTERNAL.  */
 static struct resolv_context *
 context_alloc (struct __res_state *resp)
 {
   struct resolv_context *ctx = malloc (sizeof (*ctx));
   if (ctx == NULL)
-    return NULL;
+    {
+      resp->res_h_errno = NETDB_INTERNAL;
+      __set_h_errno (NETDB_INTERNAL);
+      return NULL;
+    }
   ctx->resp = resp;
   ctx->conf = __resolv_conf_get (resp);
   ctx->__refcount = 1;