svn commit: r287790 - stable/10/bin/df

Xin LI delphij at FreeBSD.org
Mon Sep 14 17:40:58 UTC 2015


Author: delphij
Date: Mon Sep 14 17:40:57 2015
New Revision: 287790
URL: https://svnweb.freebsd.org/changeset/base/287790

Log:
  MFC r287236:
  
  Use exit() instead of return in main().  The difference in practice
  is subtle: C standard requires the language runtime to make return
  of int from main() behave like calling exit(), and in FreeBSD we do:
  
  	exit(main(argc, argv, env))
  
  In lib/csu/${ARCH}/crt1.c, so the real difference is using exit()
  explicitly would use an additional stack frame.
  
  Note however, if there is a on stack pointer is the last reference
  of an allocated memory block, returning from the function would,
  technically, result in a memory leak because we lost the last
  reference to the memory block, and calling exit() from C runtime
  could potentionally overwrite that stack frame that used to belong
  to the main() function.
  
  In practice, this is normally Okay because eventually the kernel
  would tear down the whole address space that belongs to the process
  in the _exit(2) system call, but the difference could confuse
  compilers (which may want to do stack overflow checks) and static
  analyzers.
  
  Replacing return with exit() in main() allows compilers/static
  analyzers to correctly omit or generate the right warnings when
  they do not treat main() specifically.  With the current version
  of clang on FreeBSD/amd64, use of exit() would result in slightly
  smaller code being generated and eliminated a false positive
  warning of memory leak.

Modified:
  stable/10/bin/df/df.c
Directory Properties:
  stable/10/   (props changed)

Modified: stable/10/bin/df/df.c
==============================================================================
--- stable/10/bin/df/df.c	Mon Sep 14 16:48:19 2015	(r287789)
+++ stable/10/bin/df/df.c	Mon Sep 14 17:40:57 2015	(r287790)
@@ -296,7 +296,7 @@ main(int argc, char *argv[])
 			prtstat(&mntbuf[i], &maxwidths);
 	if (cflag)
 		prtstat(&totalbuf, &maxwidths);
-	return (rv);
+	exit(rv);
 }
 
 static char *


More information about the svn-src-all mailing list