Skip to content

Commit ebd9752

Browse files
committed
Truncate to 15 bytes; add error handling
1 parent d79e7af commit ebd9752

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

Modules/_threadmodule.c

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2378,7 +2378,12 @@ _thread__get_name_impl(PyObject *module)
23782378
char name[17];
23792379
size_t size = Py_ARRAY_LENGTH(name) - 1;
23802380
pthread_t thread = pthread_self();
2381-
pthread_getname_np(thread, name, size);
2381+
int rc = pthread_getname_np(thread, name, size);
2382+
if (rc) {
2383+
errno = rc;
2384+
return PyErr_SetFromErrno(PyExc_OSError);
2385+
}
2386+
23822387
name[size] = 0;
23832388
return PyUnicode_DecodeFSDefault(name);
23842389
}
@@ -2400,11 +2405,27 @@ _thread_set_name_impl(PyObject *module, PyObject *name_obj)
24002405
{
24012406
const char *name = PyBytes_AS_STRING(name_obj);
24022407
#ifdef __APPLE__
2403-
pthread_setname_np(name);
2408+
int rc = pthread_setname_np(name);
24042409
#else
2410+
2411+
#if defined(__linux__)
2412+
// Truncate to 16 bytes including the NUL byte
2413+
char buffer[16];
2414+
size_t len = strlen(name);
2415+
if (len > 15) {
2416+
memcpy(buffer, name, 15);
2417+
buffer[15] = 0;
2418+
name = buffer;
2419+
}
2420+
#endif
2421+
24052422
pthread_t thread = pthread_self();
2406-
pthread_setname_np(thread, name);
2423+
int rc = pthread_setname_np(thread, name);
24072424
#endif
2425+
if (rc) {
2426+
errno = rc;
2427+
return PyErr_SetFromErrno(PyExc_OSError);
2428+
}
24082429
Py_RETURN_NONE;
24092430
}
24102431
#endif // HAVE_PTHREAD_SETNAME_NP

0 commit comments

Comments
 (0)