From patchwork Fri Nov 12 14:21:14 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 47529 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 DC2D53858024 for ; Fri, 12 Nov 2021 14:23:52 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org DC2D53858024 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1636727032; bh=nAjnyyrqf77jbojovTBL3O1Dv+ZrTWW11/yJjE2OdEU=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=E5XbnnsPBZHtzLa0Ug4eX0CvqnaYL4HSiGxJSD9ag9YzkcYas4GVadcoxsVCgpHda 1w+W32MQOHaiy9YldUae67AqdnbAxgJmZuYF0f3BZWxBk5xK9xqz/rVdwdQYjtt9PD TrigULnpVTiTEO5pD+6mUA67aqr/suoISHrVlMa4= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-vk1-xa30.google.com (mail-vk1-xa30.google.com [IPv6:2607:f8b0:4864:20::a30]) by sourceware.org (Postfix) with ESMTPS id A0A6C385800F for ; Fri, 12 Nov 2021 14:21:23 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org A0A6C385800F Received: by mail-vk1-xa30.google.com with SMTP id 84so5196023vkc.6 for ; Fri, 12 Nov 2021 06:21:23 -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:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=nAjnyyrqf77jbojovTBL3O1Dv+ZrTWW11/yJjE2OdEU=; b=OylImLL2hV+kmJqQ90Sym9SIw4U/VnyYO/zLsWBYJ6OcW2eUonZlA7aZnK7XR7nVCS CbDz+svgdrArew3vqX7LHtFthMezC7TrDheWkgTks7PmPBeQoIBdDmVwJM20z8577WbW CXQw7HBmzQnmfhaCz2s4WVJhD/kTExOAsk+V+31KNRgGfnDvDbpSu3PN5s/O/bNfhOza SMNKl9SSjRG5Mey4tmYryNTeidU7cW0oJ96hInRiTBxPb7gR3C0HUReL6HzCRYjwMucD IDt1vcbMF7q67P9TMQO+bUbxxdhAWEzDWZ4TVbAIXBinFCGt9mgNd/iaK2/YaybJZa8K 3jrA== X-Gm-Message-State: AOAM533tLIq68N0VQg00phye0nPxh9vOtg9UGcuUCLFER+oI6S1vFu8U sM6GCJY3FHLHKRgfQ4MPnqV0NZKrlof2/w== X-Google-Smtp-Source: ABdhPJycWK19gEaJWkEAKmYh26CLPqgkpUiy0LuJoAVp8JaGIrBntqeZka3L7u6ynpyMxoKxEwzreg== X-Received: by 2002:a1f:a046:: with SMTP id j67mr23869982vke.17.1636726882378; Fri, 12 Nov 2021 06:21:22 -0800 (PST) Received: from birita.. ([2804:431:c7cb:55a:ec9e:6037:1db6:e51]) by smtp.gmail.com with ESMTPSA id z188sm3902946vsz.12.2021.11.12.06.21.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 12 Nov 2021 06:21:22 -0800 (PST) To: libc-alpha@sourceware.org Subject: [PATCH v2 3/3] inet: Return EAI_MEMORY when nrl_domainname() fails to allocate memory Date: Fri, 12 Nov 2021 11:21:14 -0300 Message-Id: <20211112142114.239913-4-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20211112142114.239913-1-adhemerval.zanella@linaro.org> References: <20211112142114.239913-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-2.5 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, UNWANTED_LANGUAGE_BODY 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 Netto Reply-To: Adhemerval Zanella Cc: leonardo.macchia@gmail.com 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. --- inet/getnameinfo.c | 55 +++++++++++++++++++++++++++++----------------- 1 file changed, 35 insertions(+), 20 deletions(-) diff --git a/inet/getnameinfo.c b/inet/getnameinfo.c index 2d2397e7dc..2bec3263fc 100644 --- a/inet/getnameinfo.c +++ b/inet/getnameinfo.c @@ -83,7 +83,7 @@ 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; @@ -97,7 +97,7 @@ nrl_domainname_core (struct scratch_buffer *tmpbuf) if (herror == NETDB_INTERNAL && errno == ERANGE) { if (!scratch_buffer_grow (tmpbuf)) - return; + return false; } else break; @@ -111,10 +111,13 @@ nrl_domainname_core (struct scratch_buffer *tmpbuf) now to get more information. */ while (__gethostname (tmpbuf->data, tmpbuf->length)) if (!scratch_buffer_grow (tmpbuf)) - return; + return false; if ((c = strchr (tmpbuf->data, '.')) != NULL) - domain = __strdup (++c); + { + domain = __strdup (++c); + return domain != NULL; + } else { /* We need to preserve the hostname. */ @@ -127,14 +130,17 @@ nrl_domainname_core (struct scratch_buffer *tmpbuf) if (herror == NETDB_INTERNAL && errno == ERANGE) { if (!scratch_buffer_grow_preserve (tmpbuf)) - return; + return false; } else break; } if (h != NULL && (c = strchr(h->h_name, '.')) != NULL) - domain = __strdup (++c); + { + domain = __strdup (++c); + return domain != NULL; + } else { struct in_addr in_addr; @@ -151,20 +157,24 @@ nrl_domainname_core (struct scratch_buffer *tmpbuf) 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); + { + domain = __strdup (++c); + return domain != NULL; + } } } } + return true; } -static char * +static bool nrl_domainname (void) { static int not_first; @@ -172,6 +182,8 @@ nrl_domainname (void) if (__glibc_likely (atomic_load_acquire (¬_first) != 0)) return domain; + int r = true; + __libc_lock_define_initialized (static, lock); __libc_lock_lock (lock); @@ -180,16 +192,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 +296,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;