Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 22 Feb 2015 01:31:29 +0000 (UTC)
From:      "Pedro F. Giffuni" <pfg@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-10@freebsd.org
Subject:   svn commit: r279129 - stable/10/lib/libc/gen
Message-ID:  <201502220131.t1M1VTTW002013@svn.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: pfg
Date: Sun Feb 22 01:31:28 2015
New Revision: 279129
URL: https://svnweb.freebsd.org/changeset/base/279129

Log:
  MFC	r278803, r278905:
  ulimit(3): Fix broken check.
  
  The existing implementation had a broken comparison that could
  overflow and return confusing values.  Replace this with a check
  that avoids the overflow before it happens.
  
  Consistently return a maximum value also on the case of negative
  arguments since negative is considered an overflow and means
  infinity for our current setrlimit().
  
  New revamped version is credited to Bruce Evans.
  
  CID:		1199295

Modified:
  stable/10/lib/libc/gen/ulimit.c
Directory Properties:
  stable/10/   (props changed)

Modified: stable/10/lib/libc/gen/ulimit.c
==============================================================================
--- stable/10/lib/libc/gen/ulimit.c	Sun Feb 22 01:20:49 2015	(r279128)
+++ stable/10/lib/libc/gen/ulimit.c	Sun Feb 22 01:31:28 2015	(r279129)
@@ -40,7 +40,7 @@ ulimit(int cmd, ...)
 {
 	struct rlimit limit;
 	va_list ap;
-	long arg;
+	rlim_t arg;
 
 	if (cmd == UL_GETFSIZE) {
 		if (getrlimit(RLIMIT_FSIZE, &limit) == -1)
@@ -53,14 +53,16 @@ ulimit(int cmd, ...)
 		va_start(ap, cmd);
 		arg = va_arg(ap, long);
 		va_end(ap);
-		limit.rlim_max = limit.rlim_cur = (rlim_t)arg * 512;
+		if (arg < 0)
+			arg = LONG_MAX;
+		if (arg > RLIM_INFINITY / 512)
+			arg = RLIM_INFINITY / 512;
+		limit.rlim_max = limit.rlim_cur = arg * 512;
 
 		/* The setrlimit() function sets errno to EPERM if needed. */
 		if (setrlimit(RLIMIT_FSIZE, &limit) == -1)
 			return (-1);
-		if (arg * 512 > LONG_MAX)
-			return (LONG_MAX);
-		return (arg);
+		return ((long)arg);
 	} else {
 		errno = EINVAL;
 		return (-1);



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201502220131.t1M1VTTW002013>