cutils: replace strdup with g_strdup
authorPaolo Bonzini <pbonzini@redhat.com>
Mon, 2 Nov 2020 18:19:53 +0000 (13:19 -0500)
committerPaolo Bonzini <pbonzini@redhat.com>
Tue, 3 Nov 2020 14:42:52 +0000 (09:42 -0500)
Memory returned by get_relocated_path must be freed with
free or g_free depending on the path that the function
took; Coverity takes exception to this practice.  The
fix lets caller use g_free as is standard in QEMU.

While at it, mention the requirements on the caller in
the doc comment.

Suggested-by: Peter Maydell <peter.maydell@linaro.org>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
include/qemu/cutils.h
util/cutils.c

index 4bbf4834ea5c98440fa9322ce889ec03c9cadeb0..986ed8e15f41248e3802b53cab08c8a939b957d6 100644 (file)
@@ -205,6 +205,7 @@ int qemu_pstrcmp0(const char **str1, const char **str2);
  * as the prefix.  For example, if `bindir` is `/usr/bin` and @dir is
  * `/usr/share/qemu`, the function will append `../share/qemu` to the
  * directory that contains the running executable and return the result.
+ * The returned string should be freed by the caller.
  */
 char *get_relocated_path(const char *dir);
 
index c395974fab432e28c4089c4d095781ea5e70dacb..9498e28e1a259546e01e9e60d459915213a16011 100644 (file)
@@ -937,7 +937,7 @@ char *get_relocated_path(const char *dir)
     /* Fail if qemu_init_exec_dir was not called.  */
     assert(exec_dir[0]);
     if (!starts_with_prefix(dir) || !starts_with_prefix(bindir)) {
-        return strdup(dir);
+        return g_strdup(dir);
     }
 
     result = g_string_new(exec_dir);