diff options
author | Costa Tsaousis <costa@netdata.cloud> | 2023-04-20 23:50:45 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-04-20 23:50:45 +0300 |
commit | e476bf35b8957ab9e81c98191728d5d29946b6ea (patch) | |
tree | 4a5e1fb290ac9f1493f5631bc62df53f517af659 /libnetdata/threads/threads.c | |
parent | c3d70ffcb43b62c95d71334ed49ad345ddf4360d (diff) |
fix 32bit segv (#14940)
Diffstat (limited to 'libnetdata/threads/threads.c')
-rw-r--r-- | libnetdata/threads/threads.c | 25 |
1 files changed, 12 insertions, 13 deletions
diff --git a/libnetdata/threads/threads.c b/libnetdata/threads/threads.c index c4026a7331..a4591d5ac3 100644 --- a/libnetdata/threads/threads.c +++ b/libnetdata/threads/threads.c @@ -10,7 +10,7 @@ static pthread_attr_t *netdata_threads_attr = NULL; typedef struct { void *arg; pthread_t *thread; - const char *tag; + char tag[NETDATA_THREAD_NAME_MAX + 1]; void *(*start_routine) (void *); NETDATA_THREAD_OPTIONS options; } NETDATA_THREAD; @@ -18,14 +18,14 @@ typedef struct { static __thread NETDATA_THREAD *netdata_thread = NULL; inline int netdata_thread_tag_exists(void) { - return (netdata_thread && netdata_thread->tag && *netdata_thread->tag); + return (netdata_thread && *netdata_thread->tag); } static const char *thread_name_get(bool recheck) { static __thread char threadname[NETDATA_THREAD_NAME_MAX + 1] = ""; if(netdata_thread_tag_exists()) - strncpyz(threadname, netdata_thread->tag, NETDATA_THREAD_NAME_MAX + 1); + strncpyz(threadname, netdata_thread->tag, NETDATA_THREAD_NAME_MAX); else { if(!recheck && threadname[0]) return threadname; @@ -33,15 +33,15 @@ static const char *thread_name_get(bool recheck) { #if defined(__FreeBSD__) pthread_get_name_np(pthread_self(), threadname, NETDATA_THREAD_NAME_MAX + 1); if(strcmp(threadname, "netdata") == 0) - strncpyz(threadname, "MAIN", NETDATA_THREAD_NAME_MAX + 1); + strncpyz(threadname, "MAIN", NETDATA_THREAD_NAME_MAX); #elif defined(__APPLE__) - strncpyz(threadname, "MAIN", NETDATA_THREAD_NAME_MAX + 1); + strncpyz(threadname, "MAIN", NETDATA_THREAD_NAME_MAX); #elif defined(HAVE_PTHREAD_GETNAME_NP) pthread_getname_np(pthread_self(), threadname, NETDATA_THREAD_NAME_MAX + 1); if(strcmp(threadname, "netdata") == 0) - strncpyz(threadname, "MAIN", NETDATA_THREAD_NAME_MAX + 1); + strncpyz(threadname, "MAIN", NETDATA_THREAD_NAME_MAX); #else - strncpyz(threadname, "MAIN", NETDATA_THREAD_NAME_MAX + 1); + strncpyz(threadname, "MAIN", NETDATA_THREAD_NAME_MAX); #endif } @@ -182,8 +182,7 @@ static void thread_cleanup(void *ptr) { service_exits(); worker_unregister(); - freez((void *)netdata_thread->tag); - netdata_thread->tag = NULL; + netdata_thread->tag[0] = '\0'; freez(netdata_thread); netdata_thread = NULL; @@ -191,7 +190,7 @@ static void thread_cleanup(void *ptr) { static void thread_set_name_np(NETDATA_THREAD *nt) { - if (nt->tag) { + if (nt && nt->tag[0]) { int ret = 0; char threadname[NETDATA_THREAD_NAME_MAX+1]; @@ -244,7 +243,7 @@ void os_thread_get_current_name_np(char threadname[NETDATA_THREAD_NAME_MAX + 1]) #endif } -static void *thread_start(void *ptr) { +static void *netdata_thread_init(void *ptr) { netdata_thread = (NETDATA_THREAD *)ptr; if(!(netdata_thread->options & NETDATA_THREAD_OPTION_DONT_LOG_STARTUP)) @@ -270,11 +269,11 @@ int netdata_thread_create(netdata_thread_t *thread, const char *tag, NETDATA_THR NETDATA_THREAD *info = mallocz(sizeof(NETDATA_THREAD)); info->arg = arg; info->thread = thread; - info->tag = strdupz(tag); info->start_routine = start_routine; info->options = options; + strncpyz(info->tag, tag, NETDATA_THREAD_NAME_MAX); - int ret = pthread_create(thread, netdata_threads_attr, thread_start, info); + int ret = pthread_create(thread, netdata_threads_attr, netdata_thread_init, info); if(ret != 0) error("failed to create new thread for %s. pthread_create() failed with code %d", tag, ret); |