[libvirt] [PATCH] Prohibit verbose strcat

Ján Tomko jtomko at redhat.com
Wed Feb 10 16:26:59 UTC 2016


Using strcat directly is more readable than passing strlen
of the copied string to strncat.
---
 cfg.mk                                | 5 +++++
 src/storage/storage_backend_logical.c | 4 ++--
 2 files changed, 7 insertions(+), 2 deletions(-)

diff --git a/cfg.mk b/cfg.mk
index 71b0866..be9e475 100644
--- a/cfg.mk
+++ b/cfg.mk
@@ -1031,6 +1031,11 @@ sc_prohibit_not_strneq:
 	halt='Use STREQ instead of !STRNEQ'	\
 	  $(_sc_search_regexp)
 
+sc_prohibit_verbose_strcat:
+	@prohibit='strncat\([^,]*,\s+([^,]*),\s+strlen\(\1\)\)' \
+	halt='Use strcat(a, b) instead of strncat(a, b, strlen())' \
+	  $(_sc_search_regexp)
+
 # We don't use this feature of maint.mk.
 prev_version_file = /dev/null
 
diff --git a/src/storage/storage_backend_logical.c b/src/storage/storage_backend_logical.c
index ba26223..f0d6f80 100644
--- a/src/storage/storage_backend_logical.c
+++ b/src/storage/storage_backend_logical.c
@@ -123,11 +123,11 @@ virStorageBackendLogicalParseVolExtents(virStorageVolDefPtr vol,
     /* Allocate space for 'nextents' regex_unit strings plus a comma for each */
     if (VIR_ALLOC_N(regex, nextents * (strlen(regex_unit) + 1) + 1) < 0)
         goto cleanup;
-    strncat(regex, regex_unit, strlen(regex_unit));
+    strcat(regex, regex_unit);
     for (i = 1; i < nextents; i++) {
         /* "," is the separator of "devices" field */
         strcat(regex, ",");
-        strncat(regex, regex_unit, strlen(regex_unit));
+        strcat(regex, regex_unit);
     }
 
     if (VIR_ALLOC(reg) < 0)
-- 
2.4.10




More information about the libvir-list mailing list