1
Fork 0
mirror of git://git.sv.gnu.org/emacs.git synced 2026-04-27 16:51:06 -07:00

Avoid ridiculously high stack limit requests on macOS

* src/emacs.c (main): Avoid wraparound in subtraction of
rlim_t values, in case rlim_t is an unsigned type.  (Bug#32338)
This commit is contained in:
Eli Zaretskii 2018-10-04 19:13:17 +03:00
parent ac3622c81a
commit 86d2169ac3

View file

@ -875,7 +875,8 @@ main (int argc, char **argv)
newlim = rlim.rlim_max; newlim = rlim.rlim_max;
newlim -= newlim % pagesize; newlim -= newlim % pagesize;
if (pagesize <= newlim - lim) if (newlim > lim /* in case rlim_t is an unsigned type */
&& pagesize <= newlim - lim)
{ {
rlim.rlim_cur = newlim; rlim.rlim_cur = newlim;
if (setrlimit (RLIMIT_STACK, &rlim) == 0) if (setrlimit (RLIMIT_STACK, &rlim) == 0)
@ -884,9 +885,10 @@ main (int argc, char **argv)
} }
/* If the stack is big enough, let regex.c more of it before /* If the stack is big enough, let regex.c more of it before
falling back to heap allocation. */ falling back to heap allocation. */
emacs_re_safe_alloca = max if (lim < extra)
(min (lim - extra, SIZE_MAX) * (min_ratio / ratio), lim = extra; /* avoid wrap-around in unsigned subtraction */
MAX_ALLOCA); emacs_re_safe_alloca =
max (min (lim - extra, SIZE_MAX) * (min_ratio / ratio), MAX_ALLOCA);
} }
#endif /* HAVE_SETRLIMIT and RLIMIT_STACK and not CYGWIN */ #endif /* HAVE_SETRLIMIT and RLIMIT_STACK and not CYGWIN */