strncpy will not copy the terminating \0 into the destination if the source is larger than the size of the destination. Ensure that the last byte is always zero and let strncpy only copy CONFIG_TASK_NAME_SIZE bytes. The issue of unterminated names can be observed in ps when creating a pthread while CONFIG_TASK_NAME_SIZE is set to 8.

This commit is contained in:
Gregory Nutt 2014-12-17 12:24:02 -06:00
parent 8e4f7230f9
commit 937f9f23f1
6 changed files with 11 additions and 5 deletions

View File

@ -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
};

View File

@ -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"

View File

@ -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

View File

@ -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

View File

@ -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
{

View File

@ -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)