Fix writes past the allocated array bounds in execvpe (BZ#20847)
[glibc.git] / resource / vlimit.c
blobd817eeabbefcda5bb7dabaef0597f0022916701e
1 /* Copyright (C) 1991-2016 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <http://www.gnu.org/licenses/>. */
18 /* This is generic in the sense that it will work with the BSD, SYSV,
19 or stub versions of getrlimit. Separate versions could be written
20 for efficiency, but it's probably not worth it. */
22 #include <sys/vlimit.h>
23 #include <sys/resource.h>
24 #include <errno.h>
26 /* Set the soft limit for RESOURCE to be VALUE.
27 Returns 0 for success, -1 for failure. */
28 int
29 vlimit (enum __vlimit_resource resource, int value)
31 if (resource >= LIM_CPU && resource <= LIM_MAXRSS)
33 /* The rlimit codes happen to each be one less
34 than the corresponding vlimit codes. */
35 enum __rlimit_resource rlimit_res =
36 (enum __rlimit_resource) ((int) resource - 1);
37 struct rlimit lims;
39 if (__getrlimit (rlimit_res, &lims) < 0)
40 return -1;
42 lims.rlim_cur = value;
43 return __setrlimit (rlimit_res, &lims);
46 __set_errno (EINVAL);
47 return -1;