From e5693d4ccda7f1ac184f671d2001d2426cd9f721 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Fri, 7 Mar 2014 09:46:03 +0000 Subject: [PATCH] avoid large alloca on Complex/Rational calls * complex.c (parse_comp): replace ALLOCA_N with ALLOCV_N/ALLOCV_END * rational.c (read_digits): ditto This fixes segfaults on: Rational("1" * 16 * 1024 * 1024) Complex("1" * 16 * 1024 * 1024) On my system where my stack size is only 8M. --- complex.c | 23 +++++++++++++++-------- rational.c | 4 +++- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/complex.c b/complex.c index 3e9d631..c36d890 100644 --- a/complex.c +++ b/complex.c @@ -1774,19 +1774,26 @@ parse_comp(const char *s, int strict, VALUE *num) { char *buf, *b; + VALUE tmp; + int ret = 1; - buf = ALLOCA_N(char, strlen(s) + 1); + buf = ALLOCV_N(char, tmp, strlen(s) + 1); b = buf; skip_ws(&s); - if (!read_comp(&s, strict, num, &b)) - return 0; - skip_ws(&s); + if (!read_comp(&s, strict, num, &b)) { + ret = 0; + } + else { + skip_ws(&s); - if (strict) - if (*s != '\0') - return 0; - return 1; + if (strict) + if (*s != '\0') + ret = 0; + } + ALLOCV_END(tmp); + + return ret; } static VALUE diff --git a/rational.c b/rational.c index fe2d11e..a03fcd9 100644 --- a/rational.c +++ b/rational.c @@ -2167,13 +2167,14 @@ read_digits(const char **s, int strict, { char *b, *bb; int us = 1, ret = 1; + VALUE tmp; if (!isdecimal(**s)) { *num = ZERO; return 0; } - bb = b = ALLOCA_N(char, strlen(*s) + 1); + bb = b = ALLOCV_N(char, tmp, strlen(*s) + 1); while (isdecimal(**s) || **s == '_') { if (**s == '_') { @@ -2200,6 +2201,7 @@ read_digits(const char **s, int strict, conv: *b = '\0'; *num = rb_cstr_to_inum(bb, 10, 0); + ALLOCV_END(tmp); return ret; } -- 1.9.rc1