From owner-svn-src-all@freebsd.org Wed Jun 13 21:14:52 2018 Return-Path: Delivered-To: svn-src-all@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id D1D751017E96; Wed, 13 Jun 2018 21:14:52 +0000 (UTC) (envelope-from jilles@stack.nl) Received: from mailout.stack.nl (mailout05.stack.nl [IPv6:2001:610:1108:5010::202]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client CN "*.stack.nl", Issuer "COMODO RSA Domain Validation Secure Server CA" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 5CC857CC74; Wed, 13 Jun 2018 21:14:52 +0000 (UTC) (envelope-from jilles@stack.nl) Received: from toad2.stack.nl (toad2.stack.nl [IPv6:2001:610:1108:5010::161]) by mailout.stack.nl (Postfix) with ESMTP id BDDC038; Wed, 13 Jun 2018 23:14:43 +0200 (CEST) Received: by toad2.stack.nl (Postfix, from userid 1677) id B429F892AA; Wed, 13 Jun 2018 23:14:43 +0200 (CEST) Date: Wed, 13 Jun 2018 23:14:43 +0200 From: Jilles Tjoelker To: Bruce Evans Cc: Eitan Adler , src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: Re: svn commit: r335041 - head/lib/libc/stdlib Message-ID: <20180613211443.GA57326@stack.nl> References: <201806130852.w5D8qH9a093758@repo.freebsd.org> <20180613194008.W2003@besplex.bde.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20180613194008.W2003@besplex.bde.org> User-Agent: Mutt/1.9.4 (2018-02-28) X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.26 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 13 Jun 2018 21:14:53 -0000 On Wed, Jun 13, 2018 at 08:03:13PM +1000, Bruce Evans wrote: > On Wed, 13 Jun 2018, Eitan Adler wrote: > > Log: > > libc: remove explicit cast NULL in atoi > > There isn't any reason to cast NULL so just remove it. Noticed when > > cleaning up top. > There are many reasons to cast NULL for all members of the ato*() family: > - it is required if no prototype is in scope > - C99 specifies ato*() in terms of strtol*() and uses the cast to NULL, > probably because this is simplest. Omitting the cast is just wrong > if no prototype is in scope. Writing the explicit cast is simpler than > writing caveats that the stated equivalence is only valid if a prototype > is in scope. > - POSIX specifies ato*() in terms of strtol*() and uses the cast to NULL, > exactly as in C99, probably because it defers to the C standard and > doesn't and doesn't risk breaking it by changing its wording except when > extending it. These reasons can be summarized to a single reason: the cast is required if no prototype is in scope. I think it is unwise to call any function without a prototype in scope, since this runs a risk of undefined behaviour if you get the types wrong. For the code in libc, we ensure a prototype is in scope and no cast is required. For the code in the man page, I doubt we should allow for programmers that play tricks by declaring system functions manually using K&R-style declarations or (even worse) call functions without declaring them at all. Note that NULL may still need a cast when passed to a function with variable number of parameters. Ideally these types are also checked using attributes. > FreeBSD used to do the same here, and should do the same here and > elsewhere by copying better wording from POSIX whenever possible. For some reason, the FreeBSD text does not have the exception about error handling. This exception permits an implementation like musl's which calculates using int and hard-codes base 10, even if the compiler documents a cast from long to int as truncating bits. I don't think we should take advantage of this, though, since making atoi() faster than strtol() may encourage people to use atoi(). -- Jilles Tjoelker