From owner-svn-src-all@FreeBSD.ORG Sun Feb 15 14:31:51 2015 Return-Path: Delivered-To: svn-src-all@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [8.8.178.115]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id 8AE8CA8E; Sun, 15 Feb 2015 14:31:51 +0000 (UTC) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:1900:2254:2068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 760AFE2D; Sun, 15 Feb 2015 14:31:51 +0000 (UTC) Received: from svn.freebsd.org ([127.0.1.70]) by svn.freebsd.org (8.14.9/8.14.9) with ESMTP id t1FEVpbP070433; Sun, 15 Feb 2015 14:31:51 GMT (envelope-from pfg@FreeBSD.org) Received: (from pfg@localhost) by svn.freebsd.org (8.14.9/8.14.9/Submit) id t1FEVpaD070432; Sun, 15 Feb 2015 14:31:51 GMT (envelope-from pfg@FreeBSD.org) Message-Id: <201502151431.t1FEVpaD070432@svn.freebsd.org> X-Authentication-Warning: svn.freebsd.org: pfg set sender to pfg@FreeBSD.org using -f From: "Pedro F. Giffuni" Date: Sun, 15 Feb 2015 14:31:51 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r278803 - head/lib/libc/gen X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.18-1 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: Sun, 15 Feb 2015 14:31:51 -0000 Author: pfg Date: Sun Feb 15 14:31:50 2015 New Revision: 278803 URL: https://svnweb.freebsd.org/changeset/base/278803 Log: 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 MFC after: 1 week Modified: head/lib/libc/gen/ulimit.c Modified: head/lib/libc/gen/ulimit.c ============================================================================== --- head/lib/libc/gen/ulimit.c Sun Feb 15 14:25:00 2015 (r278802) +++ head/lib/libc/gen/ulimit.c Sun Feb 15 14:31:50 2015 (r278803) @@ -33,6 +33,7 @@ #include #include #include +#include #include long @@ -40,6 +41,7 @@ ulimit(int cmd, ...) { struct rlimit limit; va_list ap; + volatile intmax_t targ; long arg; if (cmd == UL_GETFSIZE) { @@ -51,16 +53,18 @@ ulimit(int cmd, ...) return ((long)limit.rlim_cur); } else if (cmd == UL_SETFSIZE) { va_start(ap, cmd); - arg = va_arg(ap, long); + targ = arg = va_arg(ap, long); va_end(ap); - limit.rlim_max = limit.rlim_cur = (rlim_t)arg * 512; + if (targ < 0) + targ = LONG_MAX; + if (targ > RLIM_INFINITY / 512) + targ = RLIM_INFINITY / 512; + limit.rlim_max = limit.rlim_cur = targ * 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)targ); } else { errno = EINVAL; return (-1);