From owner-svn-src-all@freebsd.org Wed Dec 4 13:02:44 2019 Return-Path: Delivered-To: svn-src-all@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 2247F1B829E; Wed, 4 Dec 2019 13:02:44 +0000 (UTC) (envelope-from kostikbel@gmail.com) Received: from kib.kiev.ua (kib.kiev.ua [IPv6:2001:470:d5e7:1::1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) server-signature RSA-PSS (4096 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 47SfCR6J6Nz3KYL; Wed, 4 Dec 2019 13:02:43 +0000 (UTC) (envelope-from kostikbel@gmail.com) Received: from tom.home (kib@localhost [127.0.0.1]) by kib.kiev.ua (8.15.2/8.15.2) with ESMTPS id xB4D2YtU022246 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NO); Wed, 4 Dec 2019 15:02:38 +0200 (EET) (envelope-from kostikbel@gmail.com) DKIM-Filter: OpenDKIM Filter v2.10.3 kib.kiev.ua xB4D2YtU022246 Received: (from kostik@localhost) by tom.home (8.15.2/8.15.2/Submit) id xB4D2YlO022245; Wed, 4 Dec 2019 15:02:34 +0200 (EET) (envelope-from kostikbel@gmail.com) X-Authentication-Warning: tom.home: kostik set sender to kostikbel@gmail.com using -f Date: Wed, 4 Dec 2019 15:02:34 +0200 From: Konstantin Belousov To: Xin Li Cc: cem@freebsd.org, src-committers , svn-src-all , svn-src-head Subject: Re: svn commit: r355318 - head/sbin/newfs_msdos Message-ID: <20191204130234.GO10580@kib.kiev.ua> References: <201912030703.xB373P5N043316@repo.freebsd.org> <8d49e0b1-fdf1-6c75-b726-7b8f08a61c3a@FreeBSD.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <8d49e0b1-fdf1-6c75-b726-7b8f08a61c3a@FreeBSD.org> User-Agent: Mutt/1.12.2 (2019-09-21) X-Spam-Status: No, score=-1.0 required=5.0 tests=ALL_TRUSTED,BAYES_00, DKIM_ADSP_CUSTOM_MED,FORGED_GMAIL_RCVD,FREEMAIL_FROM, NML_ADSP_CUSTOM_MED autolearn=no autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on tom.home X-Rspamd-Queue-Id: 47SfCR6J6Nz3KYL X-Spamd-Bar: ----- Authentication-Results: mx1.freebsd.org; none X-Spamd-Result: default: False [-5.99 / 15.00]; NEURAL_HAM_MEDIUM(-0.99)[-0.995,0]; REPLY(-4.00)[]; NEURAL_HAM_LONG(-1.00)[-0.998,0] X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.29 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, 04 Dec 2019 13:02:44 -0000 On Wed, Dec 04, 2019 at 12:51:44AM -0800, Xin Li wrote: > On 12/3/19 14:02, Conrad Meyer wrote: > > Hi Xin Li, > > > > Is there a reason to prefer exit() over returning from main? I have > > No, this should be case-by-case (and also assumes you are using C and > not C++). > > The two are actually subtly different (return means teardown main's > stack first, then implicitly call exit from C runtime, while exit() > would terminate immediately). This is even more subtle. If libthr is loaded into the process, then exit() also causes stack unwinding. > > If the command is meant to be used as a built-in module of something > else, like the case of kill(1) builtin of sh(1), then one must not use > exit() and also need to pay special attention to not exit() implicitly, > because the caller will not fork() prior to calling the aliased main() > function for performance reasons. > > For other cases, using exit() might be a good idea, because it's easier > to find the exit points especially if one is following sysexits(3) > values. Another reason is that if one allocates memory in main but not > free them (these shouldn't be free'ed because the kernel would unmap all > pages upon exit), return would mean these memory would be leaked: these > are legitimate issues when main() would be called by someone else. With > an explicit exit(), these memory are never leaked because stack frame of > main() remains valid before the final _exit(2) call. > > > not surveyed the source tree, but I suspect most programs in base exit > > by returning from main rather than explicit exit(3).> Thanks, > > Conrad > > > > On Mon, Dec 2, 2019 at 11:03 PM Xin LI wrote: > >> > >> Author: delphij > >> Date: Tue Dec 3 07:03:25 2019 > >> New Revision: 355318 > >> URL: https://svnweb.freebsd.org/changeset/base/355318 > >> > >> Log: > >> Explicitly exit() instead of return in main(). > >> > >> MFC after: 2 weeks > >> > >> Modified: > >> head/sbin/newfs_msdos/newfs_msdos.c > >> > >> Modified: head/sbin/newfs_msdos/newfs_msdos.c > >> ============================================================================== > >> --- head/sbin/newfs_msdos/newfs_msdos.c Tue Dec 3 07:01:28 2019 (r355317) > >> +++ head/sbin/newfs_msdos/newfs_msdos.c Tue Dec 3 07:03:25 2019 (r355318) > >> @@ -189,7 +189,7 @@ main(int argc, char *argv[]) > >> err(1, NULL); > >> } > >> dtype = *argv; > >> - return !!mkfs_msdos(fname, dtype, &o); > >> + exit(!!mkfs_msdos(fname, dtype, &o)); > >> } > >> > >> /* > >