Skip to contentSkip to conversation

cutils: replace strdup with g_strdup

PATCH In review Paolo Bonzini 2 participants 2 messages Nov 2, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 cutils: replace strdup with g_strdup

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.

---
 include/qemu/cutils.h | 1 +
 util/cutils.c         | 2 +-
 2 files changed, 2 insertions(+), 1 deletion(-)
include/qemu/cutils.h +1 -0
index 4bbf4834ea..986ed8e15f 100644
@@ -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);
 
util/cutils.c +1 -1
index c395974fab..9498e28e1a 100644
@@ -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);
Signature · 1 line
2.26.2
Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · Paolo Bonzini

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.

Quoted 6 lines
---
 include/qemu/cutils.h | 1 +
 util/cutils.c         | 2 +-
 2 files changed, 2 insertions(+), 1 deletion(-)