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: AS31976 209.132.180.0/23 X-Spam-Status: No, score=-4.0 required=3.0 tests=AWL,BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_EF,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED,RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL,SPF_HELO_PASS,SPF_PASS shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (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 7C2181F45E for ; Fri, 14 Feb 2020 20:29:03 +0000 (UTC) DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:subject:to:cc:references:from:message-id:date :mime-version:in-reply-to:content-type :content-transfer-encoding; q=dns; s=default; b=Ve56uABj1nHS+9oZ BNBMgxO4wl6fZlnnNP6y5rNwn+dc/C8QHfaAaP4aZhMkwOxLqFzpeDbQeulzRz+W VSAoG1ZDMm19fvSwlrNcSMDKZcoLYvPu0nbO1dyg5kR7s8y+rHCWDJys0eZCx+o4 uUuodUMurpuf2atWriafu98/gQQ= 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:subject:to:cc:references:from:message-id:date :mime-version:in-reply-to:content-type :content-transfer-encoding; s=default; bh=RLb+MbUCbBvD0KJ6q0h3MH OB/3Y=; b=YKoE/f2t72TYSY+825LDX/lAKFbQNUrWr5vCi2wWurBfxZhO3FRtcF FVvDTMh6/OarIlExVycUEDJzdsVFsdIFAl8ROzX9SUHCf7ZQmlkfsuT/06wGD0Lv qhOqP90wctkwNhMFiNJQ4q3OxheqdIxWVgYajnmJRK+2/faMJOpao= Received: (qmail 89703 invoked by alias); 14 Feb 2020 20:29:01 -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 Received: (qmail 89660 invoked by uid 89); 14 Feb 2020 20:28:59 -0000 Authentication-Results: sourceware.org; auth=none X-HELO: zimbra.cs.ucla.edu Subject: Re: [PATCH 1/3] : Add type safety and port to Hurd To: Florian Weimer Cc: libc-alpha@sourceware.org, Samuel Thibault References: From: Paul Eggert Message-ID: <61b49643-9c7b-7060-6eb7-21060dd6e22f@cs.ucla.edu> Date: Fri, 14 Feb 2020 12:28:52 -0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.4.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit On 2/14/20 10:11 AM, Florian Weimer wrote: > + /* A positive int value has at most 10 decimal digits. */ > + char buffer[sizeof (FD_TO_FILENAME_PREFIX) + 10]; This should use 'INT_STRLEN_BOUND (int)' rather than '10', where INT_STRLEN_BOUND is taken from intprops.h. Then you don't need the comment (and the code won't break on future ILP64 platforms :-). > +char * > +__fd_to_filename (int descriptor, struct fd_to_filename *storage) > +{ > + char *p = mempcpy (storage->buffer, FD_TO_FILENAME_PREFIX, > + strlen (FD_TO_FILENAME_PREFIX)); > + if (__glibc_likely (descriptor >= 0)) > + { > + if (descriptor < 1000) > + p = digit123 (p, descriptor); > + else if (descriptor < 1000 * 1000) ... It's not clear what that "descriptor >= 0" test is doing. I assume that a precondition is that DESCRIPTOR is nonnegative; if so, this should be mentioned and the test removed. If it's not a precondition, the code should do the right thing if DESCRIPTOR is negative (I suppose, return a filename that cannot be opened, though it doesn't do that currently) -- which should also be documented but I think this'd be overkill. As Adhemerval writes, there's no need for special cases for powers of 1000 etc. Something like the following should do (this is a bit shorter than Adhemerval's version): /* Convert nonnegative DESCRIPTOR to a file name. */ char * __fd_to_filename (int descriptor, struct fd_to_filename *storage) { char *p = mempcpy (storage->buffer, FD_TO_FILENAME_PREFIX, strlen (FD_TO_FILENAME_PREFIX) - 1); for (int d = descriptor; p++, (d /= 10) != 0; ) continue; *p = '\0'; for (int d = descriptor; *--p = '0' + d % 10, (d /= 10) != 0; ) continue; return storage->buffer; } GCC turns those divisions and remainders into the usual multiply+shift+add and this should be good enough.