From: Peter Krempa Date: Tue, 2 Mar 2021 10:00:23 +0000 (+0100) Subject: util: virstring: Always copy string in virStrcpy X-Git-Tag: v7.2.0-rc1~235 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f6280b03975a92f56c0fc3cb80faa2a4f5ad23c0;p=thirdparty%2Flibvirt.git util: virstring: Always copy string in virStrcpy 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 Reviewed-by: Ján Tomko --- 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);