From d04702854bbc9cf09e9086b5123d4e7073d33e15 Mon Sep 17 00:00:00 2001 From: Jim Meyering Date: Tue, 12 May 2009 08:35:09 +0000 Subject: [PATCH] logsys.c: avoid redundant strlen in else-block * exec/logsys.c (strcpy_cutoff): Also, with a field width (aka cutoff), and a shorter-than-field-width string, don't write the same memory twice: once with strncpy using NUL bytes, then again with spaces via the memset. git-svn-id: http://svn.fedorahosted.org/svn/corosync/trunk@2177 fd59a12c-fef9-0310-b244-a6a79926bd2f --- exec/logsys.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/exec/logsys.c b/exec/logsys.c index c4dace56..5e977412 100644 --- a/exec/logsys.c +++ b/exec/logsys.c @@ -65,6 +65,8 @@ #define YIELD_AFTER_LOG_OPS 10 +#define MIN(x,y) ((x) < (y) ? (x) : (y)) + /* * similar to syslog facilities/priorities tables, * make a tag table for internal use @@ -381,18 +383,15 @@ do { \ */ static inline int strcpy_cutoff (char *dest, const char *src, int cutoff) { + size_t len = strlen (src); if (cutoff <= 0) { - size_t len = strlen (src); memcpy (dest, src, len + 1); return (len); } else { - size_t len; - strncpy (dest, src, cutoff); + len = MIN (len, cutoff); + memcpy (dest, src, len); + memset (dest + len, ' ', cutoff - len); dest[cutoff] = '\0'; - len = strlen (dest); - if (len != cutoff) { - memset (&dest[len], ' ', cutoff - len); - } } return (cutoff); }