From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS22989 209.51.188.0/24 X-Spam-Status: No, score=-3.7 required=3.0 tests=AWL,BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dcvr.yhbt.net (Postfix) with ESMTPS id 890D61F461 for ; Thu, 4 Jul 2019 11:24:39 +0000 (UTC) Received: from localhost ([::1]:44634 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hizqu-0003G4-H9 for normalperson@yhbt.net; Thu, 04 Jul 2019 07:24:38 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:57326) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hizql-0003Dq-UQ for bug-gnulib@gnu.org; Thu, 04 Jul 2019 07:24:29 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hizqk-0002Aj-8n for bug-gnulib@gnu.org; Thu, 04 Jul 2019 07:24:27 -0400 Received: from mail.magicbluesmoke.com ([82.195.144.49]:44042) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1hizqj-00029z-U2 for bug-gnulib@gnu.org; Thu, 04 Jul 2019 07:24:26 -0400 Received: from localhost.localdomain (unknown [109.77.223.150]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.magicbluesmoke.com (Postfix) with ESMTPSA id F3A84AD5A for ; Thu, 4 Jul 2019 12:24:23 +0100 (IST) Subject: [PATCH] areadlink-with-size: guess a lower bound with 0 size References: <7AC7AA2A-EDEA-4A80-9A5A-02FAA2D823EF@whamcloud.com> <5a1b87fe-e265-6b0f-66eb-20ad1ba9c6e5@draigBrady.com> To: bug-gnulib From: =?UTF-8?Q?P=c3=a1draig_Brady?= Message-ID: Date: Thu, 4 Jul 2019 12:24:23 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.8.0 MIME-Version: 1.0 In-Reply-To: <5a1b87fe-e265-6b0f-66eb-20ad1ba9c6e5@draigBrady.com> Content-Type: multipart/mixed; boundary="------------6018699C03CA9E93C850419E" X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 82.195.144.49 X-BeenThere: bug-gnulib@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Gnulib discussion list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnulib-bounces+normalperson=yhbt.net@gnu.org Sender: "bug-gnulib" This is a multi-part message in MIME format. --------------6018699C03CA9E93C850419E Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: quoted-printable BTW this would allocate more for empty symlinks, but they're rare: https://lwn.net/Articles/551224/ cheers, P=E1draig --------------6018699C03CA9E93C850419E Content-Type: text/x-patch; name="areadlink-zero-size.patch" Content-Disposition: attachment; filename="areadlink-zero-size.patch" Content-Transfer-Encoding: quoted-printable >From 6be031b4c9d6cb742a010dbe3fe38f77fe515fec Mon Sep 17 00:00:00 2001 From: =3D?UTF-8?q?P=3DC3=3DA1draig=3D20Brady?=3D Date: Thu, 4 Jul 2019 11:50:16 +0100 Subject: [PATCH] areadlink-with-size: guess a lower bound with 0 size * lib/areadlink-with-size.c (areadlink_with_size): The size is usually taken from st_size, which can be zero, resulting in inefficient operation as seen with: $ strace -e readlink stat -c %N /proc/$$/cwd readlink("/proc/9036/cwd", "/", 1) =3D 1 readlink("/proc/9036/cwd", "/h", 2) =3D 2 readlink("/proc/9036/cwd", "/hom", 4) =3D 4 readlink("/proc/9036/cwd", "/home/pa", 8) =3D 8 readlink("/proc/9036/cwd", "/home/padraig", 16) =3D 13 Instead let zero select an appropriate lower bound, as was already done for sizes more than 8Ki. We also change SYMLINK_MAX to 1023 so that the initial allocation is a power of two. --- ChangeLog | 10 ++++++++++ lib/areadlink-with-size.c | 7 ++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/ChangeLog b/ChangeLog index ea2e86a..bbd91f0 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,13 @@ +2019-07-04 P=C3=A1draig Brady + + areadlink-with-size: guess a lower bound with 0 size + * lib/areadlink-with-size.c (areadlink_with_size): + SIZE is usually taken from st_size, which can be zero. + Instead let zero select an appropriate lower bound, + as was already done for sizes more than 8Ki. + We also change SYMLINK_MAX to 1023 so that the initial + allocation is a power of two. + 2019-07-03 Bruno Haible =20 mbrtowc: Fix invalid use of mbtowc() on MSVC. diff --git a/lib/areadlink-with-size.c b/lib/areadlink-with-size.c index eacad3f..2fbe51c 100644 --- a/lib/areadlink-with-size.c +++ b/lib/areadlink-with-size.c @@ -36,14 +36,15 @@ check, so it's OK to guess too small on hosts where there is no arbitrary limit to symbolic link length. */ #ifndef SYMLINK_MAX -# define SYMLINK_MAX 1024 +# define SYMLINK_MAX 1023 #endif =20 #define MAXSIZE (SIZE_MAX < SSIZE_MAX ? SIZE_MAX : SSIZE_MAX) =20 /* Call readlink to get the symbolic link value of FILE. SIZE is a hint as to how long the link is expected to be; - typically it is taken from st_size. It need not be correct. + typically it is taken from st_size. It need not be correct, + and a value of 0 (or more than 8Ki) will select an appropriate lower = bound. Return a pointer to that NUL-terminated string in malloc'd storage. If readlink fails, malloc fails, or if the link value is longer than SSIZE_MAX, return NULL (caller may use errno to diagnose). */ @@ -61,7 +62,7 @@ areadlink_with_size (char const *file, size_t size) : INITIAL_LIMIT_BOUND); =20 /* The initial buffer size for the link value. */ - size_t buf_size =3D size < initial_limit ? size + 1 : initial_limit; + size_t buf_size =3D size && size < initial_limit ? size + 1 : initial_= limit; =20 while (1) { --=20 2.9.3 --------------6018699C03CA9E93C850419E--