Explorar o código

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
Jim Meyering %!s(int64=17) %!d(string=hai) anos
pai
achega
d04702854b
Modificáronse 1 ficheiros con 6 adicións e 7 borrados
  1. 6 7
      exec/logsys.c

+ 6 - 7
exec/logsys.c

@@ -65,6 +65,8 @@
 
 
 #define YIELD_AFTER_LOG_OPS 10
 #define YIELD_AFTER_LOG_OPS 10
 
 
+#define MIN(x,y) ((x) < (y) ? (x) : (y))
+
 /*
 /*
  * similar to syslog facilities/priorities tables,
  * similar to syslog facilities/priorities tables,
  * make a tag table for internal use
  * make a tag table for internal use
@@ -381,18 +383,15 @@ do {									\
  */
  */
 static inline int strcpy_cutoff (char *dest, const char *src, int cutoff)
 static inline int strcpy_cutoff (char *dest, const char *src, int cutoff)
 {
 {
+	size_t len = strlen (src);
 	if (cutoff <= 0) {
 	if (cutoff <= 0) {
-		size_t len = strlen (src);
 		memcpy (dest, src, len + 1);
 		memcpy (dest, src, len + 1);
 		return (len);
 		return (len);
 	} else {
 	} else {
-		size_t len;
-		strncpy (dest, src, cutoff);
+		len = MIN (len, cutoff);
+		memcpy (dest, src, len);
+		memset (dest + len, ' ', cutoff - len);
 		dest[cutoff] = '\0';
 		dest[cutoff] = '\0';
-		len = strlen (dest);
-		if (len != cutoff) {
-			memset (&dest[len], ' ', cutoff - len);
-		}
 	}
 	}
 	return (cutoff);
 	return (cutoff);
 }
 }