From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-3.3 required=5.0 tests=MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 11572 invoked from network); 15 Jun 2022 08:14:49 -0000 Received: from mother.openwall.net (195.42.179.200) by inbox.vuxu.org with ESMTPUTF8; 15 Jun 2022 08:14:49 -0000 Received: (qmail 1809 invoked by uid 550); 15 Jun 2022 08:14:47 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Reply-To: musl@lists.openwall.com Received: (qmail 3913 invoked from network); 15 Jun 2022 06:30:21 -0000 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=qrHqAyUhN7rrRAGO/Sn8laQYxaYP+lEdrlLn+RWaAMQ=; b=Ic/nRZUCByrDegdmkvgHDcfdwV6bwweVcnuK6SbHhD87JIxH31+EroQQ1HDiOwc+lc FwWSVZOFyHqEVNkQbPsyzShLlylPoURxKKxOAPKhG7o2UCOtADw/2vV+x2JWq7mlEZ8G IpWwJ9PGAe5lHyfAXDPqJb3un+ckkzA6s8nuRVqI0YetsM3AHui4Pvo2ulZqBQWhrWQp f6RSa8oAGbD1hZwiwO29P06HfpSPePFAkSuuF++ldCb8PF8pPL3DNgLwm+dGDzJs0Ocp y+j5maIy7dfdtlFkIkm/FDetVb3aqatLQ07wSuM/njJLhrlkk4HElxlBxncwNMXwJQ6z dPCg== X-Gm-Message-State: AOAM5331hGpKGPMrOKhMVYA0BHC8uZwXSorUC0lXhthftLf/VTjpAC7L cpeJPZF7qimtiUXOwgS+AMUrXj3U7M65vfFxuPNtKt528oI= X-Google-Smtp-Source: ABdhPJzgZy1fUmDT9TgeAwKLjeP+h3gPbgbcAuKXGVzAYLKSVDqRbH/++K4lTUVsvYPTsRGeL8xUBYl9bNY/bmHGoWo= X-Received: by 2002:a17:906:7486:b0:6fe:ffd9:b14f with SMTP id e6-20020a170906748600b006feffd9b14fmr7286565ejl.573.1655274609529; Tue, 14 Jun 2022 23:30:09 -0700 (PDT) MIME-Version: 1.0 References: <20220609203426.GE7074@brightrain.aerifal.cx> In-Reply-To: <20220609203426.GE7074@brightrain.aerifal.cx> From: Hans Harder Date: Wed, 15 Jun 2022 08:30:02 +0200 Message-ID: To: Rich Felker Cc: musl@lists.openwall.com Content-Type: text/plain; charset="UTF-8" Subject: Re: [musl] hostname is using a case sensitive search in function name_from_hosts I don't have much experience in making patches, but you mean sometime like this patch. It uses strtok and only compares something case insensitive if the token is the same length of name. Also it makes it simpler in the remaining code. Hans diff -u a/src/network/lookup_name.c b/src/network/lookup_name.c --- a/src/network/lookup_name.c 2022-04-07 17:12:40.000000000 +0000 +++ b/src/network/lookup_name.c 2022-06-15 06:15:46.680000000 +0000 @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -49,6 +50,7 @@ static int name_from_hosts(struct address buf[static MAXADDRS], char canon[static 256], const char *name, int family) { char line[512]; + char sep[4] = " \t\n"; size_t l = strlen(name); int cnt = 0, badfam = 0, have_canon = 0; unsigned char _buf[1032]; @@ -62,17 +64,22 @@ return EAI_SYSTEM; } while (fgets(line, sizeof line, f) && cnt < MAXADDRS) { - char *p, *z; - + char *p; if ((p=strchr(line, '#'))) *p++='\n', *p=0; - for(p=line+1; (p=strstr(p, name)) && - (!isspace(p[-1]) || !isspace(p[l])); p++); - if (!p) continue; + if (line[0] == 0 || line[0]=='\n') continue; + p = strtok(line, sep); + while( p != NULL ) { + /* only compare case insensitive if length of both are the same */ + if (strlen(p) == l && strcasecmp(p,name)==0) { + p = strtok(line, sep); + break; + } + p = strtok(NULL, sep); + } + if (p == NULL) continue; /* Isolate IP address to parse */ - for (p=line; *p && !isspace(*p); p++); - *p++ = 0; - switch (name_from_numeric(buf+cnt, line, family)) { + switch (name_from_numeric(buf+cnt, p, family)) { case 1: cnt++; break; @@ -86,12 +93,10 @@ 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)) { + p = strtok(NULL, sep); + if (p != NULL && is_valid_hostname(p)) { have_canon = 1; - memcpy(canon, p, z-p+1); + strcpy(canon, p); } } __fclose_ca(f); On Thu, Jun 9, 2022 at 10:34 PM Rich Felker wrote: > > On Thu, Jun 09, 2022 at 08:42:28PM +0200, Hans Harder wrote: > > Hi, > > I discovered that the function name_from_hosts parses the /etc/hosts > > file and does a case sensitive search for a name. > > Sometimes I encounter mixed upper and lowercase hostnames in a /etc/hosts file. > > It would be easier if the function searches for the name in a case > > insensitive way.... > > > > By changing line 68 in src/network/lookup_name.c > > for(p=line+1; (p=strstr(p, name)) && > > to: > > for(p=line+1; (p=strcasestr(p, name)) && > > > > That would resolve the problem. > > strcasestr isn't a good match here, because it's quadratic time and > would be potentially quite slow (depending on file contents). It's > also not in a usable namespace, and is something of a junk function we > included for questionable reasons. > > The core problem here is that strstr isn't really the right operation > to be using, and was something of a lazy hack. Due to the linear-time > implementation it doesn't hurt, but it would make a lot more sense to > parse this right looking at separators. Even then though it's some > work to make it properly case-insensitive; strcasecmp is insufficient > and only handles single-byte characters. So the right thing to do is > really picking up review and merge of the draft IDN handling work, > which (if I'm remembering right) normalizes case as an inherent part > of the process. > > Rich