[前][次][番号順一覧][スレッド一覧]

ruby-changes:60884

From: Nobuyoshi <ko1@a...>
Date: Thu, 23 Apr 2020 21:42:43 +0900 (JST)
Subject: [ruby-changes:60884] a52a459b16 (master): Truncate too long thread name before setting [Bug #16808]

https://git.ruby-lang.org/ruby.git/commit/?id=a52a459b16

From a52a459b16ce1b5cf32cb6393960ff59e35c48d0 Mon Sep 17 00:00:00 2001
From: Nobuyoshi Nakada <nobu@r...>
Date: Thu, 23 Apr 2020 20:17:24 +0900
Subject: Truncate too long thread name before setting [Bug #16808]


diff --git a/thread_pthread.c b/thread_pthread.c
index b9ac63f..6bcb18d 100644
--- a/thread_pthread.c
+++ b/thread_pthread.c
@@ -1572,6 +1572,15 @@ setup_communication_pipe_internal(int pipes[2]) https://github.com/ruby/ruby/blob/trunk/thread_pthread.c#L1572
 # 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) https://github.com/ruby/ruby/blob/trunk/thread_pthread.c#L1593
     }
     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) https://github.com/ruby/ruby/blob/trunk/thread_pthread.c#L1615
 #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
-- 
cgit v0.10.2


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]