Module: xenomai-forge
Branch: next
Commit: 303bc4b35613d36e059eead48f44ba45da71d25c
URL:    
http://git.xenomai.org/?p=xenomai-forge.git;a=commit;h=303bc4b35613d36e059eead48f44ba45da71d25c

Author: Philippe Gerum <r...@xenomai.org>
Date:   Thu Jul  3 20:41:56 2014 +0200

lib: introduce namecpy()

---

 include/boilerplate/ancillaries.h |   12 ++++++++++++
 lib/alchemy/pipe.c                |    3 +--
 lib/copperplate/heapobj-pshared.c |    3 +--
 lib/copperplate/regd/fs-common.c  |    5 ++---
 lib/copperplate/threadobj.c       |    7 +++----
 lib/psos/pt.c                     |    3 +--
 lib/psos/queue.c                  |    3 +--
 lib/psos/rn.c                     |    3 +--
 lib/psos/sem.c                    |    3 +--
 lib/psos/task.c                   |    3 +--
 lib/vxworks/taskInfo.c            |    2 +-
 lib/vxworks/taskLib.c             |    6 ++----
 12 files changed, 27 insertions(+), 26 deletions(-)

diff --git a/include/boilerplate/ancillaries.h 
b/include/boilerplate/ancillaries.h
index 2cb51d6..7c0b342 100644
--- a/include/boilerplate/ancillaries.h
+++ b/include/boilerplate/ancillaries.h
@@ -21,6 +21,7 @@
 #include <stdarg.h>
 #include <time.h>
 #include <pthread.h>
+#include <string.h>
 #include <boilerplate/signal.h>
 #include <boilerplate/compiler.h>
 
@@ -46,6 +47,17 @@ struct name_generator {
 
 #define ONE_BILLION  1000000000
 
+void __namecpy_requires_character_array_as_destination(void);
+
+#define namecpy(__dst, __src)                                          \
+       ({                                                              \
+               if (!__builtin_types_compatible_p(typeof(__dst), char[])) \
+                       __namecpy_requires_character_array_as_destination();    
\
+               strncpy((__dst), __src, sizeof(__dst) - 1);             \
+               __dst[sizeof(__dst) - 1] = '\0';                        \
+               __dst;                                                  \
+        })
+       
 #ifdef __cplusplus
 extern "C" {
 #endif
diff --git a/lib/alchemy/pipe.c b/lib/alchemy/pipe.c
index 0e8f984..6d22a6b 100644
--- a/lib/alchemy/pipe.c
+++ b/lib/alchemy/pipe.c
@@ -161,8 +161,7 @@ int rt_pipe_create(RT_PIPE *pipe,
        }
 
        if (name && *name) {
-               strncpy(plabel.label, name, sizeof(plabel.label)-1);
-               plabel.label[sizeof(plabel.label)-1] = '\0';
+               namecpy(plabel.label, name);
                ret = __RT(setsockopt(sock, SOL_XDDP, XDDP_LABEL,
                                      &plabel, sizeof(plabel)));
                if (ret)
diff --git a/lib/copperplate/heapobj-pshared.c 
b/lib/copperplate/heapobj-pshared.c
index c4d4045..f7cc3b0 100644
--- a/lib/copperplate/heapobj-pshared.c
+++ b/lib/copperplate/heapobj-pshared.c
@@ -157,8 +157,7 @@ static int init_heap(struct shared_heap *heap, const char 
*name,
        pthread_mutexattr_t mattr;
        int ret;
 
-       strncpy(heap->name, name, sizeof(heap->name) - 1);
-       heap->name[sizeof(heap->name) - 1] = '\0';
+       namecpy(heap->name, name);
        heap->extentsize = size;
        heap->hdrsize = internal_overhead(size);
        heap->npages = (size - heap->hdrsize) >> HOBJ_PAGE_SHIFT;
diff --git a/lib/copperplate/regd/fs-common.c b/lib/copperplate/regd/fs-common.c
index 790e877..5f8b439 100644
--- a/lib/copperplate/regd/fs-common.c
+++ b/lib/copperplate/regd/fs-common.c
@@ -112,7 +112,7 @@ int open_threads(struct fsobj *fsobj, void *priv)
                ret = threadobj_lock(thobj);
                if (ret)
                        continue;
-               strncpy(p->name, thobj->name, sizeof(p->name) - 1);
+               namecpy(p->name, thobj->name);
                p->name[sizeof(p->name) - 1] = '\0';
                p->pid = thobj->pid;
                p->priority = threadobj_get_priority(thobj);
@@ -234,8 +234,7 @@ int open_heaps(struct fsobj *fsobj, void *priv)
                if (p - heap_data >= count)
                        break;
                heap = container_of(obj, struct shared_heap, memspec);
-               strncpy(p->name, heap->name, sizeof(p->name) - 1);
-               p->name[sizeof(p->name) - 1] = '\0';
+               namecpy(p->name, heap->name);
                p->used = heap->ubytes;
                p->total = heap->total;
                p++;
diff --git a/lib/copperplate/threadobj.c b/lib/copperplate/threadobj.c
index 96bd6ac..44b7cd9 100644
--- a/lib/copperplate/threadobj.c
+++ b/lib/copperplate/threadobj.c
@@ -1162,10 +1162,9 @@ int threadobj_prologue(struct threadobj *thobj, const 
char *name)
        } else
                pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL);
 
-       if (name) {
-               strncpy(thobj->name, name, sizeof(thobj->name) - 1);
-               thobj->name[sizeof(thobj->name) - 1] = '\0';
-       } else
+       if (name)
+               namecpy(thobj->name, name);
+       else
                *thobj->name = '\0';
 
        thobj->ptid = pthread_self();
diff --git a/lib/psos/pt.c b/lib/psos/pt.c
index e11da03..aac253b 100644
--- a/lib/psos/pt.c
+++ b/lib/psos/pt.c
@@ -147,8 +147,7 @@ u_long pt_create(const char *name,
                sprintf(pt->name, "pt%lu", ++anon_ptids);
        else {
                name = __psos_maybe_short_name(short_name, name);
-               strncpy(pt->name, name, sizeof(pt->name));
-               pt->name[sizeof(pt->name) - 1] = '\0';
+               namecpy(pt->name, name);
        }
 
        CANCEL_DEFER(svc);
diff --git a/lib/psos/queue.c b/lib/psos/queue.c
index 2b74191..e9cd782 100644
--- a/lib/psos/queue.c
+++ b/lib/psos/queue.c
@@ -99,8 +99,7 @@ static u_long __q_create(const char *name, u_long count,
                sprintf(q->name, "q%lu", ++anon_qids);
        else {
                name = __psos_maybe_short_name(short_name, name);
-               strncpy(q->name, name, sizeof(q->name));
-               q->name[sizeof(q->name) - 1] = '\0';
+               namecpy(q->name, name);
        }
 
        if (flags & Q_PRIOR)
diff --git a/lib/psos/rn.c b/lib/psos/rn.c
index 952ddfd..fec88ed 100644
--- a/lib/psos/rn.c
+++ b/lib/psos/rn.c
@@ -115,8 +115,7 @@ u_long rn_create(const char *name, void *saddr, u_long 
length,
                sprintf(rn->name, "rn%lu", ++anon_rnids);
        else {
                name = __psos_maybe_short_name(short_name, name);
-               strncpy(rn->name, name, sizeof(rn->name));
-               rn->name[sizeof(rn->name) - 1] = '\0';
+               namecpy(rn->name, name);
        }
 
        if (pvcluster_addobj_dup(&psos_rn_table, rn->name, &rn->cobj)) {
diff --git a/lib/psos/sem.c b/lib/psos/sem.c
index 7290f5d..8f20be0 100644
--- a/lib/psos/sem.c
+++ b/lib/psos/sem.c
@@ -93,8 +93,7 @@ u_long sm_create(const char *name,
                sprintf(sem->name, "sm%lu", ++anon_smids);
        else {
                name = __psos_maybe_short_name(short_name, name);
-               strncpy(sem->name, name, sizeof(sem->name));
-               sem->name[sizeof(sem->name) - 1] = '\0';
+               namecpy(sem->name, name);
        }
 
        if (cluster_addobj_dup(&psos_sem_table, sem->name, &sem->cobj)) {
diff --git a/lib/psos/task.c b/lib/psos/task.c
index 780d954..26db2b9 100644
--- a/lib/psos/task.c
+++ b/lib/psos/task.c
@@ -306,8 +306,7 @@ u_long t_create(const char *name, u_long prio,
                sprintf(task->name, "t%lu", ++anon_tids);
        else {
                name = __psos_maybe_short_name(short_name, name);
-               strncpy(task->name, name, sizeof(task->name));
-               task->name[sizeof(task->name) - 1] = '\0';
+               namecpy(task->name, name);
        }
 
        task->flags = flags;    /* We don't do much with those. */
diff --git a/lib/vxworks/taskInfo.c b/lib/vxworks/taskInfo.c
index e5fa90b..8e33644 100644
--- a/lib/vxworks/taskInfo.c
+++ b/lib/vxworks/taskInfo.c
@@ -120,7 +120,7 @@ STATUS taskGetInfo(TASK_ID task_id, TASK_DESC *desc)
        desc->td_priority = wind_task_get_priority(task);
        desc->td_status = get_task_status(task);
        desc->td_flags = tcb->flags;
-       strncpy(desc->td_name, task->name, sizeof(desc->td_name));
+       namecpy(desc->td_name, task->name);
        desc->td_entry = tcb->entry;
        desc->td_errorStatus = *task->thobj.errno_pointer;
        ret = pthread_getattr_np(task->thobj.ptid, &attr);
diff --git a/lib/vxworks/taskLib.c b/lib/vxworks/taskLib.c
index be50e49..fa6b4f5 100644
--- a/lib/vxworks/taskLib.c
+++ b/lib/vxworks/taskLib.c
@@ -354,10 +354,8 @@ static STATUS __taskInit(struct wind_task *task,
 
        if (name == NULL || *name == '\0')
                sprintf(task->name, "t%lu", ++anon_tids);
-       else {
-               strncpy(task->name, name, sizeof(task->name));
-               task->name[sizeof(task->name) - 1] = '\0';
-       }
+       else
+               namecpy(task->name, name);
 
        idata.magic = task_magic;
        idata.finalizer = task_finalizer;


_______________________________________________
Xenomai-git mailing list
Xenomai-git@xenomai.org
http://www.xenomai.org/mailman/listinfo/xenomai-git

Reply via email to