mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Wolf <wolf@wolfsden.cz>
To: musl@lists.openwall.com
Subject: Re: [musl] Proposal to match behaviour of gethostbyname to glibc
Date: Sun, 26 Apr 2020 02:15:22 +0200	[thread overview]
Message-ID: <20200426001522.rgwtcn66zquyrufq@wolfsden.cz> (raw)
In-Reply-To: <20200417184640.GM11469@brightrain.aerifal.cx>


[-- Attachment #1.1: Type: text/plain, Size: 841 bytes --]

Hello,

On 2020-04-17 14:46:40 -0400, Rich Felker wrote:
> Actually now that I'm doing this I'm not sure it's correct. The
> existing code reports all matches from the hosts file, not just the
> first one. This patch will prevent getting both ipv4 and ipv6 results,
> or multiple results for the same address family, by stopping after the
> first one.

Well, I've completely missed that. Sorry.

> If you want the canonical name to come from the first result, rather
> than suppressing all but the first result, the code instead needs to
> be changed to remember that it already found one name and not copy any
> others.

I've attached patch v2 doing exactly that. Hopefully this time it's
correct.

W.

-- 
There are only two hard things in Computer Science:
cache invalidation, naming things and off-by-one errors.

[-- Attachment #1.2: patch --]
[-- Type: text/plain, Size: 1009 bytes --]

diff --git a/src/network/lookup_name.c b/src/network/lookup_name.c
index c93263a9..00f056e6 100644
--- a/src/network/lookup_name.c
+++ b/src/network/lookup_name.c
@@ -50,7 +50,7 @@ static int name_from_hosts(struct address buf[static MAXADDRS], char canon[stati
 {
 	char line[512];
 	size_t l = strlen(name);
-	int cnt = 0, badfam = 0;
+	int cnt = 0, badfam = 0, have_canon = 0;
 	unsigned char _buf[1032];
 	FILE _f, *f = __fopen_rb_ca("/etc/hosts", &_f, _buf, sizeof _buf);
 	if (!f) switch (errno) {
@@ -83,11 +83,16 @@ static int name_from_hosts(struct address buf[static MAXADDRS], char canon[stati
 			continue;
 		}
 
+		if (have_canon) continue;
+
 		/* Extract first name as canonical name */
 		for (; *p && isspace(*p); p++);
 		for (z=p; *z && !isspace(*z); z++);
 		*z = 0;
-		if (is_valid_hostname(p)) memcpy(canon, p, z-p+1);
+		if (is_valid_hostname(p)) {
+			have_canon = 1;
+			memcpy(canon, p, z-p+1);
+		}
 	}
 	__fclose_ca(f);
 	return cnt ? cnt : badfam;

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

      reply	other threads:[~2020-04-26  0:15 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-03-13 21:46 Wolf
2020-03-13 22:16 ` Rich Felker
2020-03-13 23:43   ` Wolf
2020-04-13 23:18     ` Wolf
2020-04-15  8:31     ` Natanael Copa
2020-04-15 16:03       ` Rich Felker
2020-03-14  8:24 ` Florian Weimer
2020-03-14 14:54   ` Rich Felker
2020-03-14 16:31     ` Jeffrey Walton
2020-04-17 18:42 ` Rich Felker
2020-04-17 18:46   ` Rich Felker
2020-04-26  0:15     ` Wolf [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200426001522.rgwtcn66zquyrufq@wolfsden.cz \
    --to=wolf@wolfsden.cz \
    --cc=musl@lists.openwall.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).