diff options
author | Nobuyoshi Nakada <[email protected]> | 2020-04-23 20:17:24 +0900 |
---|---|---|
committer | Nobuyoshi Nakada <[email protected]> | 2020-04-23 21:42:21 +0900 |
commit | a52a459b16ce1b5cf32cb6393960ff59e35c48d0 (patch) | |
tree | 7636f610b3a24ea9a68566f42b174a88939c3d45 /thread_pthread.c | |
parent | 03e4eeabbf3bbb5a467956920705bf346d790efa (diff) |
Truncate too long thread name before setting [Bug #16808]
Notes
Notes:
Merged: https://github.com/ruby/ruby/pull/3053
Diffstat (limited to 'thread_pthread.c')
-rw-r--r-- | thread_pthread.c | 36 |
1 files changed, 30 insertions, 6 deletions
diff --git a/thread_pthread.c b/thread_pthread.c index b9ac63f298..6bcb18d89b 100644 --- a/thread_pthread.c +++ b/thread_pthread.c @@ -1572,6 +1572,15 @@ setup_communication_pipe_internal(int pipes[2]) # define SET_CURRENT_THREAD_NAME(name) prctl(PR_SET_NAME, name) #endif +#if defined(__linux__) +static const size_t thread_name_max = 16; +#elif defined(__APPLE__) +/* Undocumented, and main thread seems unlimited */ +static const size_t thread_name_max = 64; +#else +static const size_t thread_name_max = 16; +#endif + static VALUE threadptr_invoke_proc_location(rb_thread_t *th); static void @@ -1584,14 +1593,14 @@ native_set_thread_name(rb_thread_t *th) } else if ((loc = threadptr_invoke_proc_location(th)) != Qnil) { char *name, *p; - char buf[16]; + char buf[thread_name_max]; size_t len; int n; name = RSTRING_PTR(RARRAY_AREF(loc, 0)); p = strrchr(name, '/'); /* show only the basename of the path. */ if (p && p[1]) - name = p + 1; + name = p + 1; n = snprintf(buf, sizeof(buf), "%s:%d", name, NUM2INT(RARRAY_AREF(loc, 1))); rb_gc_force_recycle(loc); /* acts as a GC guard, too */ @@ -1606,15 +1615,30 @@ native_set_thread_name(rb_thread_t *th) #endif } -static VALUE +static void native_set_another_thread_name(rb_nativethread_id_t thread_id, VALUE name) { -#ifdef SET_ANOTHER_THREAD_NAME +#if defined SET_ANOTHER_THREAD_NAME || defined SET_CURRENT_THREAD_NAME + char buf[thread_name_max]; const char *s = ""; - if (!NIL_P(name)) s = RSTRING_PTR(name); +# if !defined SET_ANOTHER_THREAD_NAME + if (pthread_equal(pthread_self(), thread_id)) return; +# endif + if (!NIL_P(name)) { + long n; + RSTRING_GETMEM(name, s, n); + if (n >= (int)sizeof(buf)) { + memcpy(buf, s, sizeof(buf)-1); + buf[sizeof(buf)-1] = '\0'; + s = buf; + } + } +# if defined SET_ANOTHER_THREAD_NAME SET_ANOTHER_THREAD_NAME(thread_id, s); +# elif defined SET_CURRENT_THREAD_NAME + SET_CURRENT_THREAD_NAME(s); +# endif #endif - return name; } static void |