From 360edb1c04dfc7552dc053d837a8320c46910147 Mon Sep 17 00:00:00 2001 From: yugui Date: Thu, 4 Apr 2013 08:01:23 +0000 Subject: [PATCH] * thread_pthread.c (ruby_init_stack): Avoid using uninitialized value. stackaddr and size are not set if get_stack() fails. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40102 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 5 +++++ thread_pthread.c | 50 ++++++++++++++++++++++++++++-------------------- 2 files changed, 34 insertions(+), 21 deletions(-) diff --git a/ChangeLog b/ChangeLog index ca6c1f7b8b..8e53e96796 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Wed Apr 3 17:25:31 2013 Yuki Yugui Sonoda + + * thread_pthread.c (ruby_init_stack): Avoid using uninitialized value. + stackaddr and size are not set if get_stack() fails. + Thu Apr 4 16:55:08 2013 Nobuyoshi Nakada * struct.c (make_struct): avoid inadvertent symbol creation. diff --git a/thread_pthread.c b/thread_pthread.c index f2e18d24e4..7937cc9834 100644 --- a/thread_pthread.c +++ b/thread_pthread.c @@ -588,6 +588,23 @@ static struct { extern void *STACK_END_ADDRESS; #endif +enum { + RUBY_STACK_SPACE_LIMIT = 1024 * 1024, /* 1024KB */ + RUBY_STACK_SPACE_RATIO = 5 +}; + +static size_t +space_size(size_t stack_size) +{ + size_t space_size = stack_size / RUBY_STACK_SPACE_RATIO; + if (space_size > RUBY_STACK_SPACE_LIMIT) { + return RUBY_STACK_SPACE_LIMIT; + } + else { + return space_size; + } +} + #undef ruby_init_stack /* Set stack bottom of Ruby implementation. * @@ -618,13 +635,21 @@ ruby_init_stack(volatile VALUE *addr } #endif { - size_t size = 0; - size_t space = 0; +#if defined(PTHREAD_STACK_DEFAULT) +# if PTHREAD_STACK_DEFAULT < RUBY_STACK_SPACE*5 +# error "PTHREAD_STACK_DEFAULT is too small" +# endif + size_t size = PTHREAD_STACK_DEFAULT; +#else + size_t size = RUBY_VM_THREAD_VM_STACK_SIZE; +#endif + size_t space = space_size(size); #if MAINSTACKADDR_AVAILABLE void* stackaddr; STACK_GROW_DIR_DETECTION; - get_stack(&stackaddr, &size); - space = STACK_DIR_UPPER((char *)addr - (char *)stackaddr, (char *)stackaddr - (char *)addr); + if (get_stack(&stackaddr, &size) == 0) { + space = STACK_DIR_UPPER((char *)addr - (char *)stackaddr, (char *)stackaddr - (char *)addr); + } native_main_thread.stack_maxsize = size - space; #elif defined(HAVE_GETRLIMIT) int pagesize = getpagesize(); @@ -831,23 +856,6 @@ use_cached_thread(rb_thread_t *th) return result; } -enum { - RUBY_STACK_SPACE_LIMIT = 1024 * 1024, /* 1024KB */ - RUBY_STACK_SPACE_RATIO = 5 -}; - -static size_t -space_size(size_t stack_size) -{ - size_t space_size = stack_size / RUBY_STACK_SPACE_RATIO; - if (space_size > RUBY_STACK_SPACE_LIMIT) { - return RUBY_STACK_SPACE_LIMIT; - } - else { - return space_size; - } -} - static int native_thread_create(rb_thread_t *th) {