qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Paolo Bonzini <pbonzini@redhat.com>
To: qemu-devel@nongnu.org
Cc: Mark Cave-Ayland <mark.cave-ayland@ilande.co.uk>
Subject: [PATCH 02/13] oslib: do not call g_strdup from qemu_get_exec_dir
Date: Tue,  1 Sep 2020 02:20:09 -0400	[thread overview]
Message-ID: <20200901062020.26660-3-pbonzini@redhat.com> (raw)
In-Reply-To: <20200901062020.26660-1-pbonzini@redhat.com>

Just return the directory without requiring the caller to free it.
This also removes a bogus check for NULL in os_find_datadir and
module_load_one; g_strdup of a static variable cannot return NULL.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 include/qemu/osdep.h    |  8 ++------
 os-posix.c              |  6 +-----
 os-win32.c              |  2 +-
 tests/qtest/fuzz/fuzz.c |  4 ++--
 util/module.c           |  7 +------
 util/oslib-posix.c      |  8 +++++---
 util/oslib-win32.c      | 12 ++++++++----
 7 files changed, 20 insertions(+), 27 deletions(-)

diff --git a/include/qemu/osdep.h b/include/qemu/osdep.h
index 412962d91a..db2cfffaff 100644
--- a/include/qemu/osdep.h
+++ b/include/qemu/osdep.h
@@ -590,12 +590,8 @@ char *qemu_get_local_state_pathname(const char *relative_pathname);
  * Try OS specific API first, if not working, parse from argv0. */
 void qemu_init_exec_dir(const char *argv0);
 
-/* Get the saved exec dir.
- *
- * The caller is responsible for releasing the value returned with g_free()
- * after use.
- */
-char *qemu_get_exec_dir(void);
+/* Get the saved exec dir.  */
+const char *qemu_get_exec_dir(void);
 
 /**
  * qemu_getauxval:
diff --git a/os-posix.c b/os-posix.c
index bf98508b6d..8d8e7fc15c 100644
--- a/os-posix.c
+++ b/os-posix.c
@@ -90,13 +90,9 @@ void os_setup_signal_handling(void)
  */
 char *os_find_datadir(void)
 {
-    g_autofree char *exec_dir = NULL;
     g_autofree char *dir = NULL;
 
-    exec_dir = qemu_get_exec_dir();
-    g_return_val_if_fail(exec_dir != NULL, NULL);
-
-    dir = g_build_filename(exec_dir, "pc-bios", NULL);
+    dir = g_build_filename(qemu_get_exec_dir(), "pc-bios", NULL);
     if (g_file_test(dir, G_FILE_TEST_IS_DIR)) {
         return g_steal_pointer(&dir);
     }
diff --git a/os-win32.c b/os-win32.c
index c9c3afe648..eb8501b9e5 100644
--- a/os-win32.c
+++ b/os-win32.c
@@ -65,7 +65,7 @@ void os_setup_early_signal_handling(void)
  */
 char *os_find_datadir(void)
 {
-    return qemu_get_exec_dir();
+    return g_strdup(qemu_get_exec_dir());
 }
 
 void os_set_line_buffering(void)
diff --git a/tests/qtest/fuzz/fuzz.c b/tests/qtest/fuzz/fuzz.c
index 391223219d..1811cb1d88 100644
--- a/tests/qtest/fuzz/fuzz.c
+++ b/tests/qtest/fuzz/fuzz.c
@@ -143,7 +143,8 @@ int LLVMFuzzerInitialize(int *argc, char ***argv, char ***envp)
 {
 
     char *target_name;
-    char *bindir, *datadir;
+    const char *bindir;
+    char *datadir;
     bool serialize = false;
 
     /* Initialize qgraph and modules */
@@ -167,7 +168,6 @@ int LLVMFuzzerInitialize(int *argc, char ***argv, char ***envp)
          */
         bindir = qemu_get_exec_dir();
         datadir = g_build_filename(bindir, "pc-bios", NULL);
-        g_free(bindir);
         if (g_file_test(datadir, G_FILE_TEST_IS_DIR)) {
             qemu_add_data_dir(datadir);
         }
diff --git a/util/module.c b/util/module.c
index 6e63006a8f..aed04b578a 100644
--- a/util/module.c
+++ b/util/module.c
@@ -172,7 +172,6 @@ bool module_load_one(const char *prefix, const char *lib_name)
 
 #ifdef CONFIG_MODULES
     char *fname = NULL;
-    char *exec_dir;
 #ifdef CONFIG_MODULE_UPGRADES
     char *version_dir;
 #endif
@@ -199,13 +198,12 @@ bool module_load_one(const char *prefix, const char *lib_name)
         return true;
     }
 
-    exec_dir = qemu_get_exec_dir();
     search_dir = getenv("QEMU_MODULE_DIR");
     if (search_dir != NULL) {
         dirs[n_dirs++] = g_strdup_printf("%s", search_dir);
     }
     dirs[n_dirs++] = g_strdup_printf("%s", CONFIG_QEMU_MODDIR);
-    dirs[n_dirs++] = g_strdup_printf("%s", exec_dir ? : "");
+    dirs[n_dirs++] = g_strdup(qemu_get_exec_dir());
 
 #ifdef CONFIG_MODULE_UPGRADES
     version_dir = g_strcanon(g_strdup(QEMU_PKGVERSION),
@@ -216,9 +214,6 @@ bool module_load_one(const char *prefix, const char *lib_name)
 
     assert(n_dirs <= ARRAY_SIZE(dirs));
 
-    g_free(exec_dir);
-    exec_dir = NULL;
-
     for (i = 0; i < n_dirs; i++) {
         fname = g_strdup_printf("%s/%s%s",
                 dirs[i], module_name, CONFIG_HOST_DSOSUF);
diff --git a/util/oslib-posix.c b/util/oslib-posix.c
index ad8001a4ad..0dd8d24076 100644
--- a/util/oslib-posix.c
+++ b/util/oslib-posix.c
@@ -366,7 +366,9 @@ void qemu_init_exec_dir(const char *argv0)
     char *p = NULL;
     char buf[PATH_MAX];
 
-    assert(!exec_dir[0]);
+    if (exec_dir[0]) {
+        return;
+    }
 
 #if defined(__linux__)
     {
@@ -439,9 +441,9 @@ void qemu_init_exec_dir(const char *argv0)
     g_free(dir);
 }
 
-char *qemu_get_exec_dir(void)
+const char *qemu_get_exec_dir(void)
 {
-    return g_strdup(exec_dir);
+    return exec_dir;
 }
 
 static void sigbus_handler(int signal)
diff --git a/util/oslib-win32.c b/util/oslib-win32.c
index c654dafd93..1a33912944 100644
--- a/util/oslib-win32.c
+++ b/util/oslib-win32.c
@@ -315,7 +315,7 @@ void qemu_set_tty_echo(int fd, bool echo)
     }
 }
 
-static char exec_dir[PATH_MAX];
+static char *exec_dir;
 
 void qemu_init_exec_dir(const char *argv0)
 {
@@ -324,6 +324,10 @@ void qemu_init_exec_dir(const char *argv0)
     char buf[MAX_PATH];
     DWORD len;
 
+    if (exec_dir) {
+        return;
+    }
+
     len = GetModuleFileName(NULL, buf, sizeof(buf) - 1);
     if (len == 0) {
         return;
@@ -336,13 +340,13 @@ void qemu_init_exec_dir(const char *argv0)
     }
     *p = 0;
     if (access(buf, R_OK) == 0) {
-        pstrcpy(exec_dir, sizeof(exec_dir), buf);
+        exec_dir = g_strdup(buf);
     }
 }
 
-char *qemu_get_exec_dir(void)
+const char *qemu_get_exec_dir(void)
 {
-    return g_strdup(exec_dir);
+    return exec_dir;
 }
 
 #if !GLIB_CHECK_VERSION(2, 50, 0)
-- 
2.26.2




  parent reply	other threads:[~2020-09-01  6:23 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-09-01  6:20 [PATCH 00/13] Make QEMU installation relocatable Paolo Bonzini
2020-09-01  6:20 ` [PATCH 01/13] fuzz: use qemu_get_exec_dir Paolo Bonzini
2020-09-01  9:18   ` Thomas Huth
2020-09-01 14:36   ` Alexander Bulekov
2020-09-01  6:20 ` Paolo Bonzini [this message]
2020-09-02 10:30   ` [PATCH 02/13] oslib: do not call g_strdup from qemu_get_exec_dir Thomas Huth
2020-09-01  6:20 ` [PATCH 03/13] oslib-posix: default exec_dir to bindir Paolo Bonzini
2020-09-01 18:04   ` Richard Henderson
2020-09-01 18:40     ` Paolo Bonzini
2020-09-01  6:20 ` [PATCH 04/13] cutils: introduce get_relocated_path Paolo Bonzini
2020-09-01  6:20 ` [PATCH 05/13] oslib-posix: relocate path to /var Paolo Bonzini
2020-09-02  8:20   ` Philippe Mathieu-Daudé
2020-09-01  6:20 ` [PATCH 06/13] module: relocate path to modules Paolo Bonzini
2020-09-01  6:20 ` [PATCH 07/13] net: relocate paths to helpers and scripts Paolo Bonzini
2020-09-02  8:24   ` Philippe Mathieu-Daudé
2020-09-02  8:40     ` Paolo Bonzini
2020-09-01  6:20 ` [PATCH 08/13] vl: relocate paths to data directories Paolo Bonzini
2020-09-02  8:28   ` Philippe Mathieu-Daudé
2020-09-02  8:35     ` Paolo Bonzini
2020-09-02  8:41       ` Philippe Mathieu-Daudé
2020-09-01  6:20 ` [PATCH 09/13] vl: relocate path to configuration file Paolo Bonzini
2020-09-02  8:28   ` Philippe Mathieu-Daudé
2020-09-01  6:20 ` [PATCH 10/13] qemu-bridge-helper: relocate path to default ACL Paolo Bonzini
2020-09-01  6:20 ` [PATCH 11/13] qga: relocate path to default configuration and hook Paolo Bonzini
2020-09-01  6:20 ` [PATCH 12/13] ui: relocate paths to icons and translations Paolo Bonzini
2020-09-02  8:29   ` Philippe Mathieu-Daudé
2020-09-01  6:20 ` [PATCH 13/13] configure: use a platform-neutral prefix Paolo Bonzini
2020-09-01 21:14 ` [PATCH 00/13] Make QEMU installation relocatable Mark Cave-Ayland
2020-09-01 21:22   ` Paolo Bonzini
2020-09-02  6:09     ` Mark Cave-Ayland
2020-09-02  6:42       ` Paolo Bonzini
2020-09-02 11:42         ` Mark Cave-Ayland
2020-09-02 11:45           ` Mark Cave-Ayland
2020-09-02 12:16           ` Paolo Bonzini
2020-09-02 18:45             ` Mark Cave-Ayland
2020-09-02 20:20               ` Paolo Bonzini

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200901062020.26660-3-pbonzini@redhat.com \
    --to=pbonzini@redhat.com \
    --cc=mark.cave-ayland@ilande.co.uk \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).