From patchwork Tue Nov 21 13:55:29 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 119359 Delivered-To: patch@linaro.org Received: by 10.80.225.132 with SMTP id k4csp5125306edl; Tue, 21 Nov 2017 05:57:03 -0800 (PST) X-Google-Smtp-Source: AGs4zMYh6CGZyEW/g96/ftVlZN20Q8L+zwmoCmDK+ygtLWSsaLVAC1nV9PsdYiYut9Z22AeDmNsA X-Received: by 10.84.240.72 with SMTP id h8mr6727247plt.327.1511272623706; Tue, 21 Nov 2017 05:57:03 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1511272623; cv=none; d=google.com; s=arc-20160816; b=cd8NmJSZ1J61aAoWwyvv7skwEqlbGe1hmTyPyrId+G+TfwP3m3CdarmVU3I1cExYur jB7KbdvFm7VFrM0dfYDD+CdhZRdAeIDSMgGnpB+plXLq/Ts0EWGIkFdnE5BPKo1um4R/ FTWonbOOwhhzi3sfxJL9ensPfg7+Lkw4Tiiw/hWHfViVODv2uOyK2PzQmJ/3BGNXQSYE VOcxLRmXAxIzwuiOTtRdqJnVBLvxAsm2XPjLXE22+jMyC5BAEqp2kEpXsrJZ4xB0Q456 hgIj36SEnoSAS7d8zJrFj7o+ALT7CLM5/uMZhXaZAm4MZ7WYLzyO2ntD2XmTq/2L718N LEKA== 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=RuvzQikIghan8u4dt7nEw5V9TNVl4vzshHc7seNtJKQ=; b=qs+ln37SFFV17GLF9yvWf06E3PMwzSEzEwt7vQ8wngylKjx/Qfbq7FUZp3XbXKLekw 8qHPLhTdYe/cYFzIfvd24Ae+VBz6vBccRxpuGVaM7MnCaTFOWdh65k8Vw0/ONK7hDJcs 4eSmEB6FTGISjtFbEJhW4UjT41Nc8HS4ziWW61EgNTi/2N8KmV0QbgStqcvgXPk1ZX4U +YSyzPsMHFM4kcZCCVgYuurzRJunyNztZl1bPp1xbgp0RoyNxApw/nzNhskEglzx503G /Nmwu025bJ/YJ3BAeLMsjGWiFhZeasM/h9SZAeBvV4HmSHcUoHrGhg03i77F6X7IRTnk hpCg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=VpmacF5+; spf=pass (google.com: domain of libc-alpha-return-87363-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-87363-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 c11si10612629pll.792.2017.11.21.05.57.03 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 21 Nov 2017 05:57:03 -0800 (PST) Received-SPF: pass (google.com: domain of libc-alpha-return-87363-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=VpmacF5+; spf=pass (google.com: domain of libc-alpha-return-87363-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-87363-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=dGZV5O6bUxVqq4U3cm+4hAfzt9cbIjI 8sjUUroUCiHbB8CZupkh4RlvnPbMZyzTsdvnNbuBrE55V5RZjgNyEakh3t4Ur2ha PsFHSswRJLHUklwxZw56d5MuvdD9uNSPZYvsQrwRqVSylteK7KMxDjLTBSaS6Kz1 KKRV/3r4cJSs= 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=sCozfV+vSGjPzWX/qz2HhtqW5c4=; b=Vpmac F5+hnsvh0Cpi7ei8jchBRNRBAqQ0XPpJ+sSdVbVn7GqDuYusiyU0VH1VZ6u3WCya zKkU2GZcaiK9jQgfQvevyjDGuCpRQy+sPvaeaVXVc4G2ItlQgixxifnhX7ro/ktr tFi7J3NcI7VawMPBOAiAC6slSXqTjn5LmOWoEo= Received: (qmail 45027 invoked by alias); 21 Nov 2017 13:55:51 -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 45002 invoked by uid 89); 21 Nov 2017 13:55:50 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-25.7 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KB_WAM_FROM_NAME_SINGLEWORD, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.2 spammy= X-HELO: mail-qt0-f195.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=RuvzQikIghan8u4dt7nEw5V9TNVl4vzshHc7seNtJKQ=; b=DjHsiOCXjTE8J++BGt3T9dF6z9bMeHrIxLUQAR59HM2Y3/zV4xMp5k82luMhFH2UA4 ja3096LjZ5KhJWAKeiwCfoEJNH9BIUc61PA2iTUcAaI/ZkY1fkWpKkaZGkgNPt/6lzUf RlvW2mmjpNORa95qWR8aS0wC44g6GJArCZ7fBf1Zu+MmMm/fVjjE+PmVOI8rfih3Friw MKB/s0ptLSjYNmlm2amUbtXPsJ2fX3Da/yoeRC0EDql35qNARYwNH8P5CSUA1R6mOSo/ g6JSng9/pQrgJM2SixCb+UhMlK6/0qBSDH4CIIip/0ypY8YZaLk/rx4aE18ytr6H2+d3 pKkA== X-Gm-Message-State: AJaThX4aviUzC3vq3oCnPGYbIVAub5cwTJO6O/dS2do7vVvnyDQLA0yH L+OYk6p+IppoNxZmGkPs6wiDbeVKPFM= X-Received: by 10.200.40.157 with SMTP id i29mr26896380qti.86.1511272547269; Tue, 21 Nov 2017 05:55:47 -0800 (PST) From: Adhemerval Zanella To: libc-alpha@sourceware.org Subject: [PATCH 7/8] posix: Use char_array for home_dir in glob Date: Tue, 21 Nov 2017 11:55:29 -0200 Message-Id: <1511272530-10936-8-git-send-email-adhemerval.zanella@linaro.org> In-Reply-To: <1511272530-10936-1-git-send-email-adhemerval.zanella@linaro.org> References: <1511272530-10936-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 aefdb28..71807d6 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; @@ -645,44 +655,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; }