diff --git a/include/nuttx/sched.h b/include/nuttx/sched.h index 563773ec17..fa08107116 100644 --- a/include/nuttx/sched.h +++ b/include/nuttx/sched.h @@ -536,7 +536,7 @@ struct tcb_s struct xcptcontext xcp; /* Interrupt register save area */ #if CONFIG_TASK_NAME_SIZE > 0 - char name[CONFIG_TASK_NAME_SIZE]; /* Task name */ + char name[CONFIG_TASK_NAME_SIZE+1]; /* Task name (with NUL terminator) */ #endif }; diff --git a/sched/Kconfig b/sched/Kconfig index 62bc6accb4..10b34fe01c 100644 --- a/sched/Kconfig +++ b/sched/Kconfig @@ -297,11 +297,13 @@ config RR_INTERVAL config TASK_NAME_SIZE int "Maximum task name size" - default 32 + default 31 ---help--- Spcifies that maximum size of a task name to save in the TCB. Useful if scheduler instrumentation is selected. Set to zero to - disable. + disable. Excludes the NUL terminator; the actual allocated size + willl be TASK_NAME_SIZE + 1. The default of 31 then results in + a align-able 32-byte allocation.:: config MAX_TASKS int "Max number of tasks" diff --git a/sched/init/os_start.c b/sched/init/os_start.c index 561e79b9b5..fcb305180b 100644 --- a/sched/init/os_start.c +++ b/sched/init/os_start.c @@ -307,7 +307,8 @@ void os_start(void) /* Set the IDLE task name */ #if CONFIG_TASK_NAME_SIZE > 0 - strncpy(g_idletcb.cmn.name, g_idlename, CONFIG_TASK_NAME_SIZE-1); + strncpy(g_idletcb.cmn.name, g_idlename, CONFIG_TASK_NAME_SIZE); + g_idletcb.cmn.name[CONFIG_TASK_NAME_SIZE] = '\0'; #endif /* CONFIG_TASK_NAME_SIZE */ /* Configure the task name in the argument list. The IDLE task does diff --git a/sched/pthread/pthread_create.c b/sched/pthread/pthread_create.c index 70c00a4478..8cfab5908f 100644 --- a/sched/pthread/pthread_create.c +++ b/sched/pthread/pthread_create.c @@ -115,6 +115,7 @@ static inline void pthread_argsetup(FAR struct pthread_tcb_s *tcb, pthread_addr_ /* Copy the pthread name into the TCB */ strncpy(tcb->cmn.name, g_pthreadname, CONFIG_TASK_NAME_SIZE); + tcb->cmn.name[CONFIG_TASK_NAME_SIZE] = '\0'; #endif /* CONFIG_TASK_NAME_SIZE */ /* For pthreads, args are strictly pass-by-value; that actual diff --git a/sched/task/task_prctl.c b/sched/task/task_prctl.c index b0e00f3163..5bbac6b9f9 100644 --- a/sched/task/task_prctl.c +++ b/sched/task/task_prctl.c @@ -133,9 +133,10 @@ int prctl(int option, ...) if (option == PR_SET_NAME) { - /* tcb->name may not be null-terminated */ + /* Ensure that tcb->name will be null-terminated, truncating if necessary */ strncpy(tcb->name, name, CONFIG_TASK_NAME_SIZE); + tcb->name[CONFIG_TASK_NAME_SIZE] = '\0'; } else { diff --git a/sched/task/task_setup.c b/sched/task/task_setup.c index 1b838eea34..5474c91265 100644 --- a/sched/task/task_setup.c +++ b/sched/task/task_setup.c @@ -426,6 +426,7 @@ static void task_namesetup(FAR struct task_tcb_s *tcb, FAR const char *name) /* Copy the name into the TCB */ strncpy(tcb->cmn.name, name, CONFIG_TASK_NAME_SIZE); + tcb->cmn.name[CONFIG_TASK_NAME_SIZE] = '\0'; } #else # define task_namesetup(t,n)