From patchwork Mon Sep 4 17:31:23 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 809773 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=sourceware.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=libc-alpha-return-84173-incoming=patchwork.ozlabs.org@sourceware.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.b="HuWpxpsi"; dkim-atps=neutral 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 3xmH2g3mQMz9t32 for ; Tue, 5 Sep 2017 03:31:39 +1000 (AEST) 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= LIakNDftGac6rXcxrsRKjNkS3zplemkvRgZvG3EeLjNpMPpa4kelCUCl3RbhGAPm f2b16UrhLNCopA8qvzXRO9U7lt0mvFiRC9KXiQufq5gaPm+4YM5lqv4myxQ25Zb1 KPYPnrPsWBte17RhbavLQfkPzZFPyxgl+qyM+OtoIGo= 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=60MAQl 0ILsreO8qL4elI93T2Moc=; b=HuWpxpsiS1Ab5Br2WCX+Fi7yy27dYAxFnVCbsB jJkdmD9pVWuEPLKNkz3Kp3DKAdP8AzvuNKUEC0xre4APXFR8cC7uOu9RnoI8VqJ8 ZEH/xLQGZET12INdvlzNAUEzck04gKZOyu74hP/1iRUFUoaT6AArLcYi/5oPa7rj D0Kl0= Received: (qmail 124742 invoked by alias); 4 Sep 2017 17:31:30 -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 124295 invoked by uid 89); 4 Sep 2017 17:31:30 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-24.1 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RP_MATCHES_RCVD, SPF_HELO_PASS, UNWANTED_LANGUAGE_BODY autolearn=ham version=3.3.2 spammy= X-HELO: mx1.redhat.com DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 7F11E2C8A 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=fail smtp.mailfrom=fweimer@redhat.com Date: Mon, 04 Sep 2017 19:31:23 +0200 To: libc-alpha@sourceware.org Subject: [PATCH] nss_files: Use struct scratch_buffer for gethostbyname [BZ #18023] User-Agent: Heirloom mailx 12.5 7/5/10 MIME-Version: 1.0 Message-Id: <20170904173123.9C550439942E3@oldenburg.str.redhat.com> From: fweimer@redhat.com (Florian Weimer) 2017-09-04 Florian Weimer [BZ #18023] * nss/nss_files/files-hosts.c (gethostbyname3_multi): Use struct scratch_buffer. diff --git a/nss/nss_files/files-hosts.c b/nss/nss_files/files-hosts.c index 867c10c2ef..c2cd07584c 100644 --- a/nss/nss_files/files-hosts.c +++ b/nss/nss_files/files-hosts.c @@ -22,6 +22,7 @@ #include #include #include +#include /* Get implementation for some internal functions. */ @@ -121,15 +122,12 @@ gethostbyname3_multi (FILE * stream, const char *name, int af, int *errnop, int *herrnop, int flags) { /* We have to get all host entries from the file. */ - size_t tmp_buflen = MIN (buflen, 4096); - char tmp_buffer_stack[tmp_buflen] - __attribute__ ((__aligned__ (__alignof__ (struct hostent_data)))); - char *tmp_buffer = tmp_buffer_stack; + struct scratch_buffer tmp_buffer; + scratch_buffer_init (&tmp_buffer); struct hostent tmp_result_buf; int naddrs = 1; int naliases = 0; char *bufferend; - bool tmp_buffer_malloced = false; enum nss_status status; while (result->h_aliases[naliases] != NULL) @@ -138,8 +136,8 @@ gethostbyname3_multi (FILE * stream, const char *name, int af, bufferend = (char *) &result->h_aliases[naliases + 1]; again: - while ((status = internal_getent (stream, &tmp_result_buf, tmp_buffer, - tmp_buflen, errnop, herrnop, af, + while ((status = internal_getent (stream, &tmp_result_buf, tmp_buffer.data, + tmp_buffer.length, errnop, herrnop, af, flags)) == NSS_STATUS_SUCCESS) { @@ -266,52 +264,18 @@ gethostbyname3_multi (FILE * stream, const char *name, int af, if (status == NSS_STATUS_TRYAGAIN) { - size_t newsize = 2 * tmp_buflen; - if (tmp_buffer_malloced) + if (!scratch_buffer_grow (&tmp_buffer)) { - char *newp = realloc (tmp_buffer, newsize); - if (newp != NULL) - { - assert ((((uintptr_t) newp) - & (__alignof__ (struct hostent_data) - 1)) - == 0); - tmp_buffer = newp; - tmp_buflen = newsize; - goto again; - } - } - else if (!__libc_use_alloca (buflen + newsize)) - { - tmp_buffer = malloc (newsize); - if (tmp_buffer != NULL) - { - assert ((((uintptr_t) tmp_buffer) - & (__alignof__ (struct hostent_data) - 1)) - == 0); - tmp_buffer_malloced = true; - tmp_buflen = newsize; - goto again; - } + *herrnop = NETDB_INTERNAL; + status = NSS_STATUS_TRYAGAIN; } else - { - tmp_buffer - = extend_alloca (tmp_buffer, tmp_buflen, - newsize - + __alignof__ (struct hostent_data)); - tmp_buffer = (char *) (((uintptr_t) tmp_buffer - + __alignof__ (struct hostent_data) - - 1) - & ~(__alignof__ (struct hostent_data) - - 1)); - goto again; - } + goto again; } else status = NSS_STATUS_SUCCESS; out: - if (tmp_buffer_malloced) - free (tmp_buffer); + scratch_buffer_free (&tmp_buffer); return status; }