From patchwork Mon Feb 5 13:27:29 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 126882 Delivered-To: patch@linaro.org Received: by 10.46.124.24 with SMTP id x24csp1971912ljc; Mon, 5 Feb 2018 05:29:04 -0800 (PST) X-Google-Smtp-Source: AH8x224zMFE8DoA5Fzhho/ElxJVSbqh0HjGKuUx8xbjQLEgARHrkcRiQfeXN6WSdUaE5RHKAbt+9 X-Received: by 2002:a17:902:1e5:: with SMTP id b92-v6mr2983383plb.144.1517837344170; Mon, 05 Feb 2018 05:29:04 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1517837344; cv=none; d=google.com; s=arc-20160816; b=EHHV5LF/JO+/pBCUIYXAPFaKPb4Y4+/LhSG+nHXIUPAL8FC7OAL90pMvDoKRJcQpGC lotn5RpXms6mD1oF/GH3u8ozH6wXfh5MznSSX5rrMgSH2i1Qr0GbscY5DsZ+2Z7FdgpG zsDZ/U/wYCzPRUe2ib2hr1udg24nRjlgPJ/IdaNUaBL/Bp36WLyjMPVZhx/TnlazZJXq SjZ85bVQ71zx1wgjXh/kDCsEgG23PjpB/Q4zcqnL9I/YSuJr+K4FuP2gZcndHanqaczt WzvH8P0Kp/hGIwKC1GXRIoq/DAqn1WJsF9ymm86ya1guKUA0T6RfOcSN9R6jbrwmfXQs kZ2w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:to:from:delivered-to :sender:list-help:list-post:list-archive:list-subscribe :list-unsubscribe:list-id:precedence:mailing-list:dkim-signature :domainkey-signature:arc-authentication-results; bh=5XzsduIWwk1ccqp3emkwuvZfL9iPTQxAUihFNwbyzPA=; b=NuZ6CILBjNLI04fl8C1yCPVPmNANoP0zPkMDgRXKTu3hf4I8rJx9/oVRZ+mwKyXjfB FqfVtgYT/NU/F8s1kk3J0KejoVuvGY/APsxEHpcziqG7UmUcqWM2ordCLKhlSlEPbjg1 JqG10HoZrEJso56dQc+9varRAwJJkLLBF6GtPQEwcJTwYI3HsbwgpWbK4QMvQ/YAnDQd vCMTqmWxXj6ylaz0LeucswGkBPsuDgvGpLOqKWifHj5zweFq8q1b4tkOPNNZosMzyx8a 16YhBTs0UqS+ayFhHfFV3n9r5U2l0tzLz711HRL9PpZVaTefdHVOgk0l2yTzjuW1JJWV nVjA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=UdT9KGFH; spf=pass (google.com: domain of libc-alpha-return-89971-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-89971-patch=linaro.org@sourceware.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id a3-v6si6822964pld.830.2018.02.05.05.29.03 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 05 Feb 2018 05:29:04 -0800 (PST) Received-SPF: pass (google.com: domain of libc-alpha-return-89971-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) client-ip=209.132.180.131; Authentication-Results: mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=UdT9KGFH; spf=pass (google.com: domain of libc-alpha-return-89971-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-89971-patch=linaro.org@sourceware.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:subject:date:message-id:in-reply-to :references; q=dns; s=default; b=TVK70tu8wkFcG3IiMnOBlXoVUZ4cVTn h4f1u+WOYVgCpomdG3tGlRkCduE+3KQPzJxR1UVpuA64mRR6TnDftziHKkWrrn1Z qsR769iwave1pr/BHPnpoqp7VUstLTZckP/fuCDv9M6OI2R28NK5M41zHdlbhlx+ kjDjgxxIkTYE= 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:from:to:subject:date:message-id:in-reply-to :references; s=default; bh=FALKffu/dfL2aQTX9ialG7HsT9I=; b=UdT9K GFHVLyg/tqaRrGeNkIJGbjChGfrB9y6N6QBkz3imAHSE8D4ebK3OSRBtdMOrMkPM s5upQmvKj7yAVihvwExvBPly502P38PJv5zNJScWRvweyExRd7OrS1H0mAEmWb8b egcK3vfM9RF2dVoRgg9xWY34jInWYrgf8pRBeg= Received: (qmail 33276 invoked by alias); 5 Feb 2018 13:27:55 -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 33164 invoked by uid 89); 5 Feb 2018 13:27:55 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-25.8 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.2 spammy= X-HELO: mail-qk0-f196.google.com X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=5XzsduIWwk1ccqp3emkwuvZfL9iPTQxAUihFNwbyzPA=; b=E5ot0+hMG+H4V7L5LAo96kvWfWYQ23ThW+aNMlVZz0NHEIJPGEASLizCH5PSGsKsGt 32MsQ62xd53tkrzT002EReNHMOJ70LkiMfYJO1dnHg0k7CjaMR04e8XIp2UpaYROtK6V 9Vq9Pc5PNxmNRGdRjlnkdJEQTz/9L+3baxL8hpIfw8OL0flwriH5FU3E7uQLc+QR0Sr3 5QP73VCMR7atmFpGtPzEApjdP0tfsKeut6rL62BDLOFQuPa3LKhbM9jlOd9WToijvRgO 2JfmhYL+YkrYtdBnB45tFmfS4v4+d6SHFB6kphQ7a/lo9aRlbZFtG/plsx/8Cl3brgKI hm1A== X-Gm-Message-State: AKwxytdBjNMjYWPWkxO+wXvBUX1ftuZd68XkFIKQxaXtav5qcQKQOSHM l0l7LxqGnIAqyXyOzTmY63WDQh2UMlY= X-Received: by 10.55.91.5 with SMTP id p5mr46354407qkb.32.1517837271392; Mon, 05 Feb 2018 05:27:51 -0800 (PST) From: Adhemerval Zanella To: libc-alpha@sourceware.org Subject: [PATCH v2 07/12] posix: Use char_array for home_dir in glob Date: Mon, 5 Feb 2018 11:27:29 -0200 Message-Id: <1517837254-19399-8-git-send-email-adhemerval.zanella@linaro.org> In-Reply-To: <1517837254-19399-1-git-send-email-adhemerval.zanella@linaro.org> References: <1517837254-19399-1-git-send-email-adhemerval.zanella@linaro.org> This patch uses dynarray at glob internal home directory ame manipulation for GLOB_TILDE. It simplifies it and removes all the boilerplate buffer managements required. Checked x86_64-linux-gnu. * posix/glob.c (__glob): Use char_array for home directory. Signed-off-by: Adhemerval Zanella --- ChangeLog | 2 ++ posix/glob.c | 66 ++++++++++++++++++++++++++++++++++++------------------------ 2 files changed, 42 insertions(+), 26 deletions(-) -- 2.7.4 diff --git a/posix/glob.c b/posix/glob.c index e914748..a78718d 100644 --- a/posix/glob.c +++ b/posix/glob.c @@ -596,9 +596,14 @@ __glob (const char *pattern, int flags, int (*errfunc) (const char *, int), || char_array_pos (&dirname, 2) == '/'))) { /* Look up home directory. */ - char *home_dir = getenv ("HOME"); - int malloc_home_dir = 0; - if (home_dir == NULL || home_dir[0] == '\0') + struct char_array home_dir; + + const char *home_env = getenv ("HOME"); + home_env = home_env == NULL ? "" : home_env; + if (!char_array_init_str (&home_dir, home_env)) + goto err_nospace; + + if (char_array_is_empty (&home_dir)) { #ifdef WINDOWS32 /* Windows NT defines HOMEDRIVE and HOMEPATH. But give @@ -608,16 +613,21 @@ __glob (const char *pattern, int flags, int (*errfunc) (const char *, int), if (home_drive != NULL && home_path != NULL) { - size_t home_drive_len = strlen (home_drive); - size_t home_path_len = strlen (home_path); - char *mem = alloca (home_drive_len + home_path_len + 1); - - memcpy (mem, home_drive, home_drive_len); - memcpy (mem + home_drive_len, home_path, home_path_len + 1); - home_dir = mem; + if (!char_array_set_str (&home_dir, home_drive) + || !char_array_append_str (&home_dir, home_path)) + { + char_array_free (&home_dir); + goto err_nospace; + } } else - home_dir = "c:/users/default"; /* poor default */ + { + if (!char_array_set_str (&home_dir, "c:/users/default")) + { + char_array_free (&home_dir); + goto err_nospace; + } + } #else int err; struct passwd *p; @@ -646,44 +656,48 @@ __glob (const char *pattern, int flags, int (*errfunc) (const char *, int), if (!scratch_buffer_grow (&s)) goto err_nospace; } - if (err == 0) - { - home_dir = strdup (p->pw_dir); - malloc_home_dir = 1; - } + bool e = (err == 0 && char_array_set_str (&home_dir, p->pw_dir)); scratch_buffer_free (&s); - if (err == 0 && home_dir == NULL) + if (e == false) goto err_nospace; #endif /* WINDOWS32 */ } - if (home_dir == NULL || home_dir[0] == '\0') + if (char_array_is_empty (&home_dir)) { - if (__glibc_unlikely (malloc_home_dir)) - free (home_dir); if (flags & GLOB_TILDE_CHECK) { + char_array_free (&home_dir); retval = GLOB_NOMATCH; goto out; } else { - home_dir = (char *) "~"; /* No luck. */ - malloc_home_dir = 0; + if (!char_array_set_str (&home_dir, "~")) + { + char_array_free (&home_dir); + goto err_nospace; + } } } /* Now construct the full directory. */ + bool e = true; if (char_array_pos (&dirname, 1) == '\0') { - if (!char_array_set_str (&dirname, home_dir)) - goto err_nospace; + e = char_array_set_str (&dirname, char_array_str (&home_dir)); dirlen = char_array_size (&dirname) - 1; } else { /* Replaces '~' by the obtained HOME dir. */ char_array_erase (&dirname, 0); - if (!char_array_prepend_str (&dirname, home_dir)) - goto err_nospace; + e = char_array_prepend_str (&dirname, + char_array_str (&home_dir)); + } + if (e == false) + { + char_array_free (&dirname); + char_array_free (&home_dir); + goto err_nospace; } dirname_modified = true; }