From 9a4d02cd8386e1b662cfee821509e50051887982 Mon Sep 17 00:00:00 2001 From: Rickard Green Date: Fri, 5 Jun 2020 17:48:28 +0200 Subject: Use destination buffer size for snprintf() --- erts/emulator/beam/erl_async.c | 2 +- erts/emulator/beam/erl_process.c | 12 ++++++------ erts/lib_src/pthread/ethread.c | 14 ++++++++++++-- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/erts/emulator/beam/erl_async.c b/erts/emulator/beam/erl_async.c index 536c50c283..cc42f68668 100644 --- a/erts/emulator/beam/erl_async.c +++ b/erts/emulator/beam/erl_async.c @@ -209,7 +209,7 @@ erts_init_async(void) for (i = 0; i < erts_async_max_threads; i++) { ErtsAsyncQ *aq = async_q(i); - erts_snprintf(thr_opts.name, 32, "async_%d", i+1); + erts_snprintf(thr_opts.name, sizeof(thr_name), "async_%d", i+1); erts_thr_create(&aq->thr_id, async_main, (void*) aq, &thr_opts); } diff --git a/erts/emulator/beam/erl_process.c b/erts/emulator/beam/erl_process.c index 12de96f4e9..4a69900d28 100644 --- a/erts/emulator/beam/erl_process.c +++ b/erts/emulator/beam/erl_process.c @@ -8622,7 +8622,7 @@ erts_start_schedulers(void) if (erts_runq_supervision_interval) { opts.suggested_stack_size = 16; - erts_snprintf(opts.name, 32, "runq_supervisor"); + erts_snprintf(opts.name, sizeof(name), "runq_supervisor"); erts_atomic_init_nob(&runq_supervisor_sleeping, 0); if (0 != ethr_event_init(&runq_supervision_event)) erts_exit(ERTS_ABORT_EXIT, "Failed to create run-queue supervision event\n"); @@ -8643,7 +8643,7 @@ erts_start_schedulers(void) for (ix = 0; ix < erts_no_schedulers; ix++) { ErtsSchedulerData *esdp = ERTS_SCHEDULER_IX(ix); ASSERT(ix == esdp->no - 1); - erts_snprintf(opts.name, 32, "%lu_scheduler", ix + 1); + erts_snprintf(opts.name, sizeof(name), "%lu_scheduler", ix + 1); res = ethr_thr_create(&esdp->tid, sched_thread_func, (void*)esdp, &opts); if (res != 0) { erts_exit(ERTS_ABORT_EXIT, "Failed to create scheduler thread %d, error = %d\n", ix, res); @@ -8657,7 +8657,7 @@ erts_start_schedulers(void) { for (ix = 0; ix < erts_no_dirty_cpu_schedulers; ix++) { ErtsSchedulerData *esdp = ERTS_DIRTY_CPU_SCHEDULER_IX(ix); - erts_snprintf(opts.name, 32, "%d_dirty_cpu_scheduler", ix + 1); + erts_snprintf(opts.name, sizeof(name), "%d_dirty_cpu_scheduler", ix + 1); opts.suggested_stack_size = erts_dcpu_sched_thread_suggested_stack_size; res = ethr_thr_create(&esdp->tid,sched_dirty_cpu_thread_func,(void*)esdp,&opts); if (res != 0) @@ -8665,7 +8665,7 @@ erts_start_schedulers(void) } for (ix = 0; ix < erts_no_dirty_io_schedulers; ix++) { ErtsSchedulerData *esdp = ERTS_DIRTY_IO_SCHEDULER_IX(ix); - erts_snprintf(opts.name, 32, "%d_dirty_io_scheduler", ix + 1); + erts_snprintf(opts.name, sizeof(name), "%d_dirty_io_scheduler", ix + 1); opts.suggested_stack_size = erts_dio_sched_thread_suggested_stack_size; res = ethr_thr_create(&esdp->tid,sched_dirty_io_thread_func,(void*)esdp,&opts); if (res != 0) @@ -8673,14 +8673,14 @@ erts_start_schedulers(void) } } - erts_snprintf(opts.name, 32, "aux"); + erts_snprintf(opts.name, sizeof(name), "aux"); res = ethr_thr_create(&tid, aux_thread, NULL, &opts); if (res != 0) erts_exit(ERTS_ABORT_EXIT, "Failed to create aux thread, error = %d\n", res); for (ix = 0; ix < erts_no_poll_threads; ix++) { - erts_snprintf(opts.name, 32, "%d_poller", ix); + erts_snprintf(opts.name, sizeof(name), "%d_poller", ix); res = ethr_thr_create(&tid, poll_thread, (void*)(UWord)ix, &opts); if (res != 0) diff --git a/erts/lib_src/pthread/ethread.c b/erts/lib_src/pthread/ethread.c index 9dcc2c505d..ba36b2f452 100644 --- a/erts/lib_src/pthread/ethread.c +++ b/erts/lib_src/pthread/ethread.c @@ -341,11 +341,21 @@ ethr_thr_create(ethr_tid *tid, void * (*func)(void *), void *arg, twd.stacksize = 0; if (opts && opts->name) { + size_t nlen = sizeof(twd.name_buff); #ifdef __HAIKU__ - snprintf(twd.name_buff, B_OS_NAME_LENGTH, "%s", opts->name); + if (nlen > B_OS_NAME_LENGTH) + nlen = B_OS_NAME_LENGTH; #else - snprintf(twd.name_buff, 16, "%s", opts->name); + /* + * Length of 16 is known to work. At least pthread_setname_np() + * is documented to fail on too long name string, but documentation + * does not say what the limit is. Do not have the time to dig + * further into that now... + */ + if (nlen > 16) + nlen = 16; #endif + snprintf(twd.name_buff, nlen, "%s", opts->name); twd.name = twd.name_buff; } else twd.name = NULL; -- cgit v1.2.1