[PATCH 16/16] util: virstring: Remove virStrncpy

Peter Krempa pkrempa at redhat.com
Tue Mar 2 14:49:33 UTC 2021


The function is now unused and motivated users to write crazy parsers
which were hard to understand, had pointless error paths just to avoid
few memory allocations.

Remove the function as we're fine with g_strndup and virStrcpy.

Signed-off-by: Peter Krempa <pkrempa at redhat.com>
---
 docs/coding-style.rst    | 13 ------------
 src/libvirt_private.syms |  1 -
 src/util/virstring.c     | 44 ----------------------------------------
 src/util/virstring.h     |  2 --
 4 files changed, 60 deletions(-)

diff --git a/docs/coding-style.rst b/docs/coding-style.rst
index 55dfa196a2..c43d20c7b2 100644
--- a/docs/coding-style.rst
+++ b/docs/coding-style.rst
@@ -714,19 +714,6 @@ does **not** guarantee a NULL-terminated buffer, which makes it
 extremely dangerous to use. Instead, use one of the replacement
 functions provided by libvirt:

-::
-
-  virStrncpy(char *dest, const char *src, size_t n, size_t destbytes)
-
-The first two arguments have the same meaning as for strncpy,
-namely the destination and source of the copy operation. Unlike
-strncpy, the function will always copy exactly the number of bytes
-requested and make sure the destination is NULL-terminated, as the
-source is required to be; sanity checks are performed to ensure
-the size of the destination, as specified by the last argument, is
-sufficient for the operation to succeed. On success, 0 is
-returned; on failure, a value <0 is returned instead.
-
 ::

   virStrcpy(char *dest, const char *src, size_t destbytes)
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 2a3bbdc577..eb121032d5 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -3264,7 +3264,6 @@ virStringStripIPv6Brackets;
 virStringStripSuffix;
 virStringToUpper;
 virStringTrimOptionalNewline;
-virStrncpy;
 virStrToDouble;
 virStrToLong_i;
 virStrToLong_l;
diff --git a/src/util/virstring.c b/src/util/virstring.c
index a35cd8ba76..1d6141b5c1 100644
--- a/src/util/virstring.c
+++ b/src/util/virstring.c
@@ -452,50 +452,6 @@ virDoubleToStr(char **strp, double number)
 }


-/**
- * virStrncpy:
- *
- * @dest: destination buffer
- * @src: source buffer
- * @n: number of bytes to copy
- * @destbytes: number of bytes the destination can accommodate
- *
- * Copies the first @n bytes of @src to @dest.
- *
- * @src must be NULL-terminated; if successful, @dest is guaranteed to
- * be NULL-terminated as well.
- *
- * @n must be a reasonable value, that is, it must not exceed either
- * the length of @src or the size of @dest. For the latter constraint,
- * the fact that @dest needs to accommodate a NULL byte in addition to
- * the bytes copied from @src must be taken into account.
- *
- * If you want to copy *all* of @src to @dest, use virStrcpy() or
- * virStrcpyStatic() instead.
- *
- * Returns: 0 on success, <0 on failure.
- */
-int
-virStrncpy(char *dest, const char *src, size_t n, size_t destbytes)
-{
-    size_t src_len = strlen(src);
-
-    /* As a special case, -1 means "copy the entire string".
-     *
-     * This is to avoid calling strlen() twice, once in the virStrcpy()
-     * wrapper and once here for bound checking purposes. */
-    if (n == -1)
-        n = src_len;
-
-    if (n > src_len || n > (destbytes - 1))
-        return -1;
-
-    memcpy(dest, src, n);
-    dest[n] = '\0';
-
-    return 0;
-}
-
 /**
  * virStrcpy:
  *
diff --git a/src/util/virstring.h b/src/util/virstring.h
index da1fe86ffc..e688495574 100644
--- a/src/util/virstring.h
+++ b/src/util/virstring.h
@@ -97,8 +97,6 @@ void virSkipSpacesBackwards(const char *str, char **endp)

 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);
 #define virStrcpyStatic(dest, src) virStrcpy((dest), (src), sizeof(dest))

-- 
2.29.2




More information about the libvir-list mailing list