[PATCH] nslookup: fix exitcode with search domains, closes 12541

Natanael Copa ncopa at alpinelinux.org
Tue Feb 4 11:24:32 UTC 2020


Return success if there are any valid answer rather than exit with error.

bloatcheck on x86_64:

function                                             old     new   delta
nslookup_main                                        917     924      +7
send_queries                                        2115    2121      +6
.rodata                                           136606  136609      +3
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 3/0 up/down: 16/0)               Total: 16
bytes
   text    data     bss     dec     hex filename
 839394   14660    2080  856134   d1046 busybox_old
 839410   14660    2080  856150   d1056 busybox_unstripped

Signed-off-by: Natanael Copa <ncopa at alpinelinux.org>
---
 networking/nslookup.c | 34 ++++++++++++++++++----------------
 1 file changed, 18 insertions(+), 16 deletions(-)

diff --git a/networking/nslookup.c b/networking/nslookup.c
index c43e60558..d14412386 100644
--- a/networking/nslookup.c
+++ b/networking/nslookup.c
@@ -265,7 +265,7 @@ struct query {
 	const char *name;
 	unsigned qlen;
 //	unsigned latency;
-//	uint8_t rcode;
+	uint8_t rcode;
 	unsigned char query[512];
 //	unsigned char reply[512];
 };
@@ -321,7 +321,7 @@ struct globals {
 	struct query *query;
 	char *search;
 	smalluint have_search_directive;
-	smalluint exitcode;
+	smallint answer;
 } FIX_ALIASING;
 #define G (*(struct globals*)bb_common_bufsiz1)
 #define INIT_G() do { \
@@ -625,28 +625,31 @@ static int send_queries(struct ns *ns)
 
 		/* Process reply */
 		G.query[qn].qlen = 0; /* flag: "reply received" */
+		G.query[qn].rcode = rcode;
 		tcur = monotonic_ms();
 #if 1
 		if (option_mask32 & OPT_debug) {
 			printf("Query #%d completed in %ums:\n", qn, tcur - tstart);
 		}
 		if (rcode != 0) {
-			printf("** server can't find %s: %s\n",
-					G.query[qn].name, rcodes[rcode]);
-			G.exitcode = EXIT_FAILURE;
+			printf("** server can't find %s: %s\n\n",
+				G.query[qn].name, rcodes[rcode]);
 		} else {
 			switch (parse_reply(reply, recvlen)) {
 			case -1:
-				printf("*** Can't find %s: Parse error\n", G.query[qn].name);
-				G.exitcode = EXIT_FAILURE;
+				printf("*** Can't find %s: Parse error\n\n", G.query[qn].name);
+				G.query[qn].rcode = 0x10;
 				break;
 
 			case 0:
-				printf("*** Can't find %s: No answer\n", G.query[qn].name);
+				G.query[qn].rcode = 0x20;
+				break;
+			default:
+				bb_putchar('\n');
+				G.answer++;
 				break;
 			}
 		}
-		bb_putchar('\n');
 		n_replies++;
 		if (n_replies >= G.query_count)
 			goto ret;
@@ -1004,21 +1007,20 @@ int nslookup_main(int argc UNUSED_PARAM, char **argv)
 	}
 
 	err = 0;
-	for (rc = 0; rc < G.query_count; rc++) {
-		if (G.query[rc].qlen) {
-			printf("*** Can't find %s: No answer\n", G.query[rc].name);
-			err = 1;
+	for (rc = 0; !G.answer && rc < G.query_count; rc++) {
+		if (G.query[rc].qlen || G.query[rc].rcode == 0x20) {
+			printf("*** Can't find %s: No answer\n\n", G.query[rc].name);
+		} else if (G.query[rc].rcode != 0) {
+			err = EXIT_FAILURE;
 		}
 	}
-	if (err) /* should this affect exicode too? */
-		bb_putchar('\n');
 
 	if (ENABLE_FEATURE_CLEAN_UP) {
 		free(G.server);
 		free(G.query);
 	}
 
-	return G.exitcode;
+	return err;
 }
 
 #endif
-- 
2.25.0



More information about the busybox mailing list