From nobody Mon Feb 9 18:19:26 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1598941384; cv=none; d=zohomail.com; s=zohoarc; b=XupOYHvt5ACrKxnpV9p6SWks+zKLY0Ju54KrZb4pq0iOuV93ROwSsD/sqq51Z6e5eELOQo1FoxeBHBBTl40KBLq0B5Aj6ZYqaVeSAZF+VolpTxvhvSdS0b8UyYeRqetLUVExTR7upypQprz+Qn1CtxdkF1DMenWdhAcLGV1Mrn0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1598941384; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=tSSh8Fj8KagTnesaHCQXywrAQbGBYsigzg8baFolzVs=; b=JJmFKadlpe5n/9fzZ6/CWDaZdbFaZz23OMRhisr+GmCyyMuYlktPsaty/wFo0f14S8c3abNUke9lICiTfOV49QIZALSMk6tEFYu+jCEOJWD1/7ED4I/c5TGowO0FYGk5v7aBEs0RwaQ3AwW+AoROqF4o34sW9YODdYntD7ORsTE= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1598941384973337.8214002864306; Mon, 31 Aug 2020 23:23:04 -0700 (PDT) Received: from localhost ([::1]:35960 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kCzhA-0002ez-5Y for importer@patchew.org; Tue, 01 Sep 2020 02:23:04 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:44298) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kCzed-0005zA-MR for qemu-devel@nongnu.org; Tue, 01 Sep 2020 02:20:27 -0400 Received: from us-smtp-2.mimecast.com ([207.211.31.81]:20613 helo=us-smtp-delivery-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1kCzeb-0000kY-FP for qemu-devel@nongnu.org; Tue, 01 Sep 2020 02:20:27 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-83-k3r2n5a5Ne-NNOkD9yrwZA-1; Tue, 01 Sep 2020 02:20:22 -0400 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id F09701DDF0; Tue, 1 Sep 2020 06:20:21 +0000 (UTC) Received: from virtlab701.virt.lab.eng.bos.redhat.com (virtlab701.virt.lab.eng.bos.redhat.com [10.19.152.228]) by smtp.corp.redhat.com (Postfix) with ESMTP id A8B5C10027A6; Tue, 1 Sep 2020 06:20:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1598941224; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=tSSh8Fj8KagTnesaHCQXywrAQbGBYsigzg8baFolzVs=; b=XJtlfep2RSMchKAu0YfXHOL5UDe6BRMkCn8uVBb1E9ikRORU+2t8As0XXDQfjnrnsYvEST ENeAn0YMPg1W+OBSZw0c0aDdd+KbbK7GptWzxEyZhlEg+lmWAuIoZj/1P6L7bDAh7WiiH4 7JU3fIn3mYwuJ3+lvcEStEUCpya6oFE= X-MC-Unique: k3r2n5a5Ne-NNOkD9yrwZA-1 From: Paolo Bonzini To: qemu-devel@nongnu.org Subject: [PATCH 02/13] oslib: do not call g_strdup from qemu_get_exec_dir Date: Tue, 1 Sep 2020 02:20:09 -0400 Message-Id: <20200901062020.26660-3-pbonzini@redhat.com> In-Reply-To: <20200901062020.26660-1-pbonzini@redhat.com> References: <20200901062020.26660-1-pbonzini@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=pbonzini@redhat.com X-Mimecast-Spam-Score: 0.001 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=207.211.31.81; envelope-from=pbonzini@redhat.com; helo=us-smtp-delivery-1.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/09/01 02:08:15 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Mark Cave-Ayland Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" 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 Reviewed-by: Thomas Huth --- 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 *relati= ve_pathname); * Try OS specific API first, if not working, parse from argv0. */ void qemu_init_exec_dir(const char *argv0); =20 -/* 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); =20 /** * 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 =3D NULL; g_autofree char *dir =3D NULL; =20 - exec_dir =3D qemu_get_exec_dir(); - g_return_val_if_fail(exec_dir !=3D NULL, NULL); - - dir =3D g_build_filename(exec_dir, "pc-bios", NULL); + dir =3D 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()); } =20 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) { =20 char *target_name; - char *bindir, *datadir; + const char *bindir; + char *datadir; bool serialize =3D false; =20 /* Initialize qgraph and modules */ @@ -167,7 +168,6 @@ int LLVMFuzzerInitialize(int *argc, char ***argv, char = ***envp) */ bindir =3D qemu_get_exec_dir(); datadir =3D 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 *li= b_name) =20 #ifdef CONFIG_MODULES char *fname =3D 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; } =20 - exec_dir =3D qemu_get_exec_dir(); search_dir =3D getenv("QEMU_MODULE_DIR"); if (search_dir !=3D NULL) { dirs[n_dirs++] =3D g_strdup_printf("%s", search_dir); } dirs[n_dirs++] =3D g_strdup_printf("%s", CONFIG_QEMU_MODDIR); - dirs[n_dirs++] =3D g_strdup_printf("%s", exec_dir ? : ""); + dirs[n_dirs++] =3D g_strdup(qemu_get_exec_dir()); =20 #ifdef CONFIG_MODULE_UPGRADES version_dir =3D g_strcanon(g_strdup(QEMU_PKGVERSION), @@ -216,9 +214,6 @@ bool module_load_one(const char *prefix, const char *li= b_name) =20 assert(n_dirs <=3D ARRAY_SIZE(dirs)); =20 - g_free(exec_dir); - exec_dir =3D NULL; - for (i =3D 0; i < n_dirs; i++) { fname =3D 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 =3D NULL; char buf[PATH_MAX]; =20 - assert(!exec_dir[0]); + if (exec_dir[0]) { + return; + } =20 #if defined(__linux__) { @@ -439,9 +441,9 @@ void qemu_init_exec_dir(const char *argv0) g_free(dir); } =20 -char *qemu_get_exec_dir(void) +const char *qemu_get_exec_dir(void) { - return g_strdup(exec_dir); + return exec_dir; } =20 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) } } =20 -static char exec_dir[PATH_MAX]; +static char *exec_dir; =20 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; =20 + if (exec_dir) { + return; + } + len =3D GetModuleFileName(NULL, buf, sizeof(buf) - 1); if (len =3D=3D 0) { return; @@ -336,13 +340,13 @@ void qemu_init_exec_dir(const char *argv0) } *p =3D 0; if (access(buf, R_OK) =3D=3D 0) { - pstrcpy(exec_dir, sizeof(exec_dir), buf); + exec_dir =3D g_strdup(buf); } } =20 -char *qemu_get_exec_dir(void) +const char *qemu_get_exec_dir(void) { - return g_strdup(exec_dir); + return exec_dir; } =20 #if !GLIB_CHECK_VERSION(2, 50, 0) --=20 2.26.2