[PATCH 01/16] util: virstring: Always copy string in virStrcpy

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



15 out of 72 invocations of virStrcpy(Static) ignore the return value as
it's either impossible to fail or in certain cases a truncated copy is
still good enough. Unfortunately virStrcpy doesn't copy anything in
such case as the checks are done first.

Fix this by using g_strlcpy for the implementation and removing
G_GNUC_WARN_UNUSED_RESULT from the function so that callers can decide
when it's okay.

Signed-off-by: Peter Krempa <pkrempa@xxxxxxxxxx>
---
 src/util/virstring.c | 12 +++++++-----
 src/util/virstring.h |  3 +--
 2 files changed, 8 insertions(+), 7 deletions(-)

diff --git a/src/util/virstring.c b/src/util/virstring.c
index c3e64007fe..a35cd8ba76 100644
--- a/src/util/virstring.c
+++ b/src/util/virstring.c
@@ -503,16 +503,18 @@ virStrncpy(char *dest, const char *src, size_t n, size_t destbytes)
  * @src: source buffer
  * @destbytes: number of bytes the destination can accommodate
  *
- * Copies @src to @dest.
+ * Copies @src to @dest. @dest is guaranteed to be 'nul' terminated if
+ * destbytes is 1 or more.
  *
- * See virStrncpy() for more information.
- *
- * Returns: 0 on success, <0 on failure.
+ * Returns: 0 on success, -1 if @src doesn't fit into @dest and was truncated.
  */
 int
 virStrcpy(char *dest, const char *src, size_t destbytes)
 {
-    return virStrncpy(dest, src, -1, destbytes);
+    if (g_strlcpy(dest, src, destbytes) >= destbytes)
+        return -1;
+
+    return 0;
 }

 /**
diff --git a/src/util/virstring.h b/src/util/virstring.h
index 45aead1838..da1fe86ffc 100644
--- a/src/util/virstring.h
+++ b/src/util/virstring.h
@@ -99,8 +99,7 @@ bool virStringIsEmpty(const char *str);

 int virStrncpy(char *dest, const char *src, size_t n, size_t destbytes)
     G_GNUC_WARN_UNUSED_RESULT;
-int virStrcpy(char *dest, const char *src, size_t destbytes)
-    G_GNUC_WARN_UNUSED_RESULT;
+int virStrcpy(char *dest, const char *src, size_t destbytes);
 #define virStrcpyStatic(dest, src) virStrcpy((dest), (src), sizeof(dest))

 int virStringSortCompare(const void *a, const void *b);
-- 
2.29.2




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]

  Powered by Linux