From patchwork Thu Nov 25 16:38:05 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 48157 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A8A4A385841F for ; Thu, 25 Nov 2021 16:38:32 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A8A4A385841F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1637858312; bh=tnwzPFlRq84Np93+7iWlJZB6LWvvKpwJPA9gUOtMm7g=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=JrR0paOh1cKnhcJ6bSW/nZ2LXsNofSiCLjdgEYhy/ehlxxPXnJsSS4x8NQL408Yi3 yJl42C3Kal2fArmEepA4lAHUhoOqt1zj7BRvy5mJw4L3gD1p2L6SB2uDASfZw57Mzk 8pK7EjFeDr6L8Aq18zNbnW7sPXB8Zfzn3Hn8yCjE= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-vk1-xa29.google.com (mail-vk1-xa29.google.com [IPv6:2607:f8b0:4864:20::a29]) by sourceware.org (Postfix) with ESMTPS id 6E7923858D35 for ; Thu, 25 Nov 2021 16:38:09 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 6E7923858D35 Received: by mail-vk1-xa29.google.com with SMTP id 84so4282181vkc.6 for ; Thu, 25 Nov 2021 08:38:09 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=tnwzPFlRq84Np93+7iWlJZB6LWvvKpwJPA9gUOtMm7g=; b=oWvG9WqO6HGBNdGnkVcyLczKYvq9pAWOEIKjKv4I/hW+mc+wX9UKb3bOSiJKRbEcr8 6eNlEdEiZiz/rPAmwy+AbitOFE78dvzbYE0PcwJjMIMMDJSim27apO2/fiKxp/Krs1+H QqXAZV64dul3E9ohnG6ozTB0BPsgh3eQAoHSvkmZkm9fnH3ulbMJqme1onQFcsg1retJ zgai37hdsE7gC92Ab/3Qpsf33qT5P5S5wgrhHAH19RVNqNKABzKhN3l84fdv0mQXjzvi 2PxPFG7suEZWmVb26mHSXeGib0Pp46OtLfWJ8a2OGK6J2TTbOFPR1wyaMohTtxYazvGu arqw== X-Gm-Message-State: AOAM530lzQ5z3FxKbGHpapnP5eEmjrDZOL8xumivmB6dPm/fFLAoEMfb 48Ea7rXoixr+wj7PpeT62tZnnOii44zoVw== X-Google-Smtp-Source: ABdhPJxv/gIhTjJidAswJWtC/80flxsiVV70OnXl2rSjFvIxczp4ASa7WyfuLWSMIkOfOSGsavOhRw== X-Received: by 2002:a05:6122:791:: with SMTP id k17mr13154102vkr.13.1637858288638; Thu, 25 Nov 2021 08:38:08 -0800 (PST) Received: from birita.. ([2804:431:c7cb:e054:2b7b:edb0:2264:6dae]) by smtp.gmail.com with ESMTPSA id o188sm1951325vko.48.2021.11.25.08.38.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Nov 2021 08:38:08 -0800 (PST) To: libc-alpha@sourceware.org, Andreas Schwab Subject: [PATCH v3] inet: Return EAI_MEMORY when nrl_domainname() fails to allocate memory Date: Thu, 25 Nov 2021 13:38:05 -0300 Message-Id: <20211125163805.3744478-1-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <87ee7ltm34.fsf@igel.home> References: <87ee7ltm34.fsf@igel.home> MIME-Version: 1.0 X-Spam-Status: No, score=-13.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Adhemerval Zanella via Libc-alpha From: Adhemerval Zanella Reply-To: Adhemerval Zanella Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" It aligns NI_NOFQDN with default behavior for getnameinfo(). Checked on x86_64-linux-gnu. --- Changes from v2: * Simplify nrl_domainname_core. --- inet/getnameinfo.c | 140 +++++++++++++++++++++++---------------------- 1 file changed, 72 insertions(+), 68 deletions(-) diff --git a/inet/getnameinfo.c b/inet/getnameinfo.c index 2d2397e7dc..acf1538a37 100644 --- a/inet/getnameinfo.c +++ b/inet/getnameinfo.c @@ -83,94 +83,95 @@ libc_freeres_ptr (static char *domain); now ignored. */ #define DEPRECATED_NI_IDN 192 -static void +static bool nrl_domainname_core (struct scratch_buffer *tmpbuf) { char *c; struct hostent *h, th; int herror; - while (__gethostbyname_r ("localhost", &th, - tmpbuf->data, tmpbuf->length, + while (__gethostbyname_r ("localhost", &th, tmpbuf->data, tmpbuf->length, &h, &herror)) { if (herror == NETDB_INTERNAL && errno == ERANGE) { if (!scratch_buffer_grow (tmpbuf)) - return; + return false; } else break; } if (h != NULL && (c = strchr (h->h_name, '.')) != NULL) - domain = __strdup (++c); - else { - /* The name contains no domain information. Use the name - now to get more information. */ - while (__gethostname (tmpbuf->data, tmpbuf->length)) - if (!scratch_buffer_grow (tmpbuf)) - return; - - if ((c = strchr (tmpbuf->data, '.')) != NULL) - domain = __strdup (++c); - else - { - /* We need to preserve the hostname. */ - size_t hstnamelen = strlen (tmpbuf->data) + 1; - while (__gethostbyname_r (tmpbuf->data, &th, - tmpbuf->data + hstnamelen, - tmpbuf->length - hstnamelen, - &h, &herror)) - { - if (herror == NETDB_INTERNAL && errno == ERANGE) - { - if (!scratch_buffer_grow_preserve (tmpbuf)) - return; - } - else - break; - } + domain = __strdup (++c); + return domain != NULL; + } - if (h != NULL && (c = strchr(h->h_name, '.')) != NULL) - domain = __strdup (++c); - else - { - struct in_addr in_addr; + /* The name contains no domain information. Use the name + now to get more information. */ + while (__gethostname (tmpbuf->data, tmpbuf->length)) + if (!scratch_buffer_grow (tmpbuf)) + return false; - in_addr.s_addr = htonl (INADDR_LOOPBACK); + if ((c = strchr (tmpbuf->data, '.')) != NULL) + { + domain = __strdup (++c); + return domain != NULL; + } - while (__gethostbyaddr_r ((const char *) &in_addr, - sizeof (struct in_addr), - AF_INET, &th, - tmpbuf->data, - tmpbuf->length, - &h, &herror)) - { - if (herror == NETDB_INTERNAL && errno == ERANGE) - { - if (!scratch_buffer_grow (tmpbuf)) - return; - } - else - break; - } + /* We need to preserve the hostname. */ + size_t hstnamelen = strlen (tmpbuf->data) + 1; + while (__gethostbyname_r (tmpbuf->data, &th, tmpbuf->data + hstnamelen, + tmpbuf->length - hstnamelen, &h, &herror)) + { + if (herror == NETDB_INTERNAL && errno == ERANGE) + { + if (!scratch_buffer_grow_preserve (tmpbuf)) + return false; + } + else + break; + } - if (h != NULL && (c = strchr (h->h_name, '.')) != NULL) - domain = __strdup (++c); - } + if (h != NULL && (c = strchr(h->h_name, '.')) != NULL) + { + domain = __strdup (++c); + return domain != NULL; + } + + struct in_addr in_addr = { .s_addr = htonl (INADDR_LOOPBACK) }; + + while (__gethostbyaddr_r ((const char *) &in_addr, sizeof (struct in_addr), + AF_INET, &th, tmpbuf->data, tmpbuf->length, &h, + &herror)) + { + if (herror == NETDB_INTERNAL && errno == ERANGE) + { + if (!scratch_buffer_grow (tmpbuf)) + return false; } + else + break; + } + + if (h != NULL && (c = strchr (h->h_name, '.')) != NULL) + { + domain = __strdup (++c); + return domain != NULL; } + return true; } -static char * +static bool nrl_domainname (void) { static int not_first; if (__glibc_likely (atomic_load_acquire (¬_first) != 0)) - return domain; + return true; + + int r = true; __libc_lock_define_initialized (static, lock); __libc_lock_lock (lock); @@ -180,16 +181,15 @@ nrl_domainname (void) struct scratch_buffer tmpbuf; scratch_buffer_init (&tmpbuf); - nrl_domainname_core (&tmpbuf); + if ((r = nrl_domainname_core (&tmpbuf))) + atomic_store_release (¬_first, 1); scratch_buffer_free (&tmpbuf); - - atomic_store_release (¬_first, 1); } __libc_lock_unlock (lock); - return domain; + return r; }; /* Copy a string to a destination buffer with length checking. Return @@ -285,13 +285,17 @@ gni_host_inet_name (struct scratch_buffer *tmpbuf, if (h) { - char *c; - if ((flags & NI_NOFQDN) - && (c = nrl_domainname ()) - && (c = strstr (h->h_name, c)) - && (c != h->h_name) && (*(--c) == '.')) - /* Terminate the string after the prefix. */ - *c = '\0'; + if (flags & NI_NOFQDN) + { + if (!nrl_domainname ()) + return EAI_MEMORY; + + char *c = domain; + if (c != NULL && (c = strstr (h->h_name, c)) + && (c != h->h_name) && (*(--c) == '.')) + /* Terminate the string after the prefix. */ + *c = '\0'; + } /* If requested, convert from the IDN format. */ bool do_idn = flags & NI_IDN;