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 <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
Peter Krempa 2021-03-02 11:00:23 +01:00
parent bfbed3c718
commit f6280b0397
2 changed files with 8 additions and 7 deletions

View File

@ -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;
}
/**

View File

@ -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);