From patchwork Mon Jun 19 12:26:54 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 777723 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3wrqyl1Lvwz9s89 for ; Mon, 19 Jun 2017 22:28:46 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.b="lWRTvH1Y"; dkim-atps=neutral DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:date:to:subject:mime-version:content-type :content-transfer-encoding:message-id:from; q=dns; s=default; b= DXOFbN+eg1QkWGkNEwESnOXNnpnxsMTmkku8sJjniWfcmfg4KHgZK/OU4u7uFFB5 DuH054n2LAY5mzL/dKbiNjdirkJwIUDhzc/dRizgEeqO8NJrk0wLOWBothNw8jlF waXcNiYNGPdSxXCMqbK6bK4oA2QXOJIdN/n4Zdizcfk= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:date:to:subject:mime-version:content-type :content-transfer-encoding:message-id:from; s=default; bh=8YQZi9 J4ZiawL0iQOJVaOCK/Ta4=; b=lWRTvH1YkxMlkNKW06KscU+WQY09TbyZUx+Ljv 6c//n8fyaF+HQbyqcw3iaBB6jPqtousiu77/MZFdFjHm6b6KUZa4zMOA60XsGbdo l8sp3wKMyJkzUO0yqlfCSpbSc0iN8N5hC7NnON8ArPeSIvOj56yjPwNsPGxiX64f +n6T0= Received: (qmail 108473 invoked by alias); 19 Jun 2017 12:27:00 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 108216 invoked by uid 89); 19 Jun 2017 12:26:59 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, SPF_HELO_PASS, T_RP_MATCHES_RCVD autolearn=ham version=3.3.2 spammy= X-HELO: mx1.redhat.com DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 2D8CF811A9 Authentication-Results: ext-mx03.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx03.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=fweimer@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 2D8CF811A9 Date: Mon, 19 Jun 2017 14:26:54 +0200 To: libc-alpha@sourceware.org Subject: [PATCH COMMITTED] resolv: Use getline for configuration file reading in res_vinit_1 User-Agent: Heirloom mailx 12.5 7/5/10 MIME-Version: 1.0 Message-Id: <20170619122654.488EA40138EE9@oldenburg.str.redhat.com> From: fweimer@redhat.com (Florian Weimer) 2017-06-16 Florian Weimer * resolv/res_init.c (res_vinit_1): Use getline to read from the configuration file. (__res_vinit): Adjust. diff --git a/resolv/res_init.c b/resolv/res_init.c index e604a02..ed5a4d4 100644 --- a/resolv/res_init.c +++ b/resolv/res_init.c @@ -126,10 +126,10 @@ is_sort_mask (char ch) deallocation and error handling. Return true on success, false on failure. */ static bool -res_vinit_1 (res_state statp, bool preinit, FILE *fp) +res_vinit_1 (res_state statp, bool preinit, FILE *fp, char **buffer) { char *cp, **pp; - char buf[BUFSIZ]; + size_t buffer_size = 0; int nserv = 0; /* Number of nameservers read from file. */ bool have_serv6 = false; bool haveenv = false; @@ -197,27 +197,38 @@ res_vinit_1 (res_state statp, bool preinit, FILE *fp) } #define MATCH(line, name) \ - (!strncmp (line, name, sizeof (name) - 1) \ - && (line[sizeof (name) - 1] == ' ' \ - || line[sizeof (name) - 1] == '\t')) + (!strncmp ((line), name, sizeof (name) - 1) \ + && ((line)[sizeof (name) - 1] == ' ' \ + || (line)[sizeof (name) - 1] == '\t')) if (fp != NULL) { /* No threads use this stream. */ __fsetlocking (fp, FSETLOCKING_BYCALLER); /* Read the config file. */ - while (__fgets_unlocked (buf, sizeof (buf), fp) != NULL) + while (true) { + { + ssize_t ret = __getline (buffer, &buffer_size, fp); + if (ret <= 0) + { + if (_IO_ferror_unlocked (fp)) + return false; + else + break; + } + } + /* Skip comments. */ - if (*buf == ';' || *buf == '#') + if (**buffer == ';' || **buffer == '#') continue; /* Read default domain name. */ - if (MATCH (buf, "domain")) + if (MATCH (*buffer, "domain")) { if (haveenv) /* LOCALDOMAIN overrides the configuration file. */ continue; - cp = buf + sizeof ("domain") - 1; + cp = *buffer + sizeof ("domain") - 1; while (*cp == ' ' || *cp == '\t') cp++; if ((*cp == '\0') || (*cp == '\n')) @@ -230,12 +241,12 @@ res_vinit_1 (res_state statp, bool preinit, FILE *fp) continue; } /* Set search list. */ - if (MATCH (buf, "search")) + if (MATCH (*buffer, "search")) { if (haveenv) /* LOCALDOMAIN overrides the configuration file. */ continue; - cp = buf + sizeof ("search") - 1; + cp = *buffer + sizeof ("search") - 1; while (*cp == ' ' || *cp == '\t') cp++; if ((*cp == '\0') || (*cp == '\n')) @@ -271,11 +282,11 @@ res_vinit_1 (res_state statp, bool preinit, FILE *fp) continue; } /* Read nameservers to query. */ - if (MATCH (buf, "nameserver") && nserv < MAXNS) + if (MATCH (*buffer, "nameserver") && nserv < MAXNS) { struct in_addr a; - cp = buf + sizeof ("nameserver") - 1; + cp = *buffer + sizeof ("nameserver") - 1; while (*cp == ' ' || *cp == '\t') cp++; if ((*cp != '\0') && (*cp != '\n') && __inet_aton (cp, &a)) @@ -300,7 +311,7 @@ res_vinit_1 (res_state statp, bool preinit, FILE *fp) sa6 = malloc (sizeof (*sa6)); if (sa6 == NULL) - return -1; + return false; sa6->sin6_family = AF_INET6; sa6->sin6_port = htons (NAMESERVER_PORT); @@ -323,11 +334,11 @@ res_vinit_1 (res_state statp, bool preinit, FILE *fp) } continue; } - if (MATCH (buf, "sortlist")) + if (MATCH (*buffer, "sortlist")) { struct in_addr a; - cp = buf + sizeof ("sortlist") - 1; + cp = *buffer + sizeof ("sortlist") - 1; while (nsort < MAXRESOLVSORT) { while (*cp == ' ' || *cp == '\t') @@ -367,9 +378,9 @@ res_vinit_1 (res_state statp, bool preinit, FILE *fp) } continue; } - if (MATCH (buf, "options")) + if (MATCH (*buffer, "options")) { - res_setoptions (statp, buf + sizeof ("options") - 1, "conf"); + res_setoptions (statp, *buffer + sizeof ("options") - 1, "conf"); continue; } } @@ -387,10 +398,13 @@ res_vinit_1 (res_state statp, bool preinit, FILE *fp) statp->nsaddr.sin_port = htons (NAMESERVER_PORT); statp->nscount = 1; } - if (statp->defdname[0] == 0 - && __gethostname (buf, sizeof (statp->defdname) - 1) == 0 - && (cp = strchr (buf, '.')) != NULL) - strcpy (statp->defdname, cp + 1); + if (statp->defdname[0] == 0) + { + char buf[sizeof (statp->defdname)]; + if (__gethostname (buf, sizeof (statp->defdname) - 1) == 0 + && (cp = strchr (buf, '.')) != NULL) + strcpy (statp->defdname, cp + 1); + } /* Find components of local domain that might be searched. */ if (!havesearch) @@ -404,7 +418,7 @@ res_vinit_1 (res_state statp, bool preinit, FILE *fp) if ((cp = getenv ("RES_OPTIONS")) != NULL) res_setoptions (statp, cp, "env"); statp->options |= RES_INIT; - return 0; + return true; } /* Set up default settings. If the /etc/resolv.conf configuration @@ -434,7 +448,12 @@ __res_vinit (res_state statp, int preinit) need to be handled by the application. */ return -1; } - if (!res_vinit_1 (statp, preinit, fp)) + + char *buffer = NULL; + bool ok = res_vinit_1 (statp, preinit, fp, &buffer); + free (buffer); + + if (!ok) { /* Deallocate the name server addresses which have been allocated. */