From mboxrd@z Thu Jan 1 00:00:00 1970 From: Cyrill Gorcunov Subject: Re: [PATCH] kvm tool: Make kvm structure to carry name copy Date: Sat, 4 Feb 2012 17:08:04 +0400 Message-ID: <20120204130804.GB14818@moon> References: <20120203195715.GF3314@moon> <20120204122005.GA14818@moon> <20120204122658.GA15888@moon> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Cc: Sasha Levin , Asias He , Ingo Molnar , KVM-ML To: Pekka Enberg Return-path: Received: from mail-bk0-f46.google.com ([209.85.214.46]:41762 "EHLO mail-bk0-f46.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753278Ab2BDNII (ORCPT ); Sat, 4 Feb 2012 08:08:08 -0500 Received: by bkcjm19 with SMTP id jm19so3723917bkc.19 for ; Sat, 04 Feb 2012 05:08:07 -0800 (PST) Content-Disposition: inline In-Reply-To: Sender: kvm-owner@vger.kernel.org List-ID: On Sat, Feb 04, 2012 at 02:57:23PM +0200, Pekka Enberg wrote: > On Sat, 4 Feb 2012, Cyrill Gorcunov wrote: > >Index: linux-2.6.git/tools/kvm/kvm.c > >=================================================================== > >--- linux-2.6.git.orig/tools/kvm/kvm.c > >+++ linux-2.6.git/tools/kvm/kvm.c > >@@ -254,6 +254,7 @@ int kvm__exit(struct kvm *kvm) > > kvm__arch_delete_ram(kvm); > > kvm_ipc__stop(); > > kvm__remove_socket(kvm->name); > >+ free((void *)kvm->name); > > Please fix the struct definition and drop the cast. > I believe having it as const char * might save us some potential troubles, but sure, here we go ;) Cyrill --- kvm tool: Make kvm structure to carry name copy If default guest name is used (which is the default case) the kvm might end up carrying the pointer to a name which is allocated on stack. kvm_cmd_run_init (on stack) default_name kvm__init(..., default_name) kvm->name = default_name So make it to carry a copy of name. Signed-off-by: Cyrill Gorcunov --- tools/kvm/kvm.c | 9 +++++++-- tools/kvm/powerpc/include/kvm/kvm-arch.h | 2 +- tools/kvm/x86/include/kvm/kvm-arch.h | 2 +- 3 files changed, 9 insertions(+), 4 deletions(-) Index: linux-2.6.git/tools/kvm/kvm.c =================================================================== --- linux-2.6.git.orig/tools/kvm/kvm.c +++ linux-2.6.git/tools/kvm/kvm.c @@ -254,6 +254,7 @@ int kvm__exit(struct kvm *kvm) kvm__arch_delete_ram(kvm); kvm_ipc__stop(); kvm__remove_socket(kvm->name); + free(kvm->name); free(kvm); return 0; @@ -377,6 +378,12 @@ struct kvm *kvm__init(const char *kvm_de goto cleanup; } + kvm->name = strdup(name); + if (!kvm->name) { + ret = -ENOMEM; + goto cleanup; + } + if (kvm__check_extensions(kvm)) { pr_err("A required KVM extention is not supported by OS"); ret = -ENOSYS; @@ -384,8 +391,6 @@ struct kvm *kvm__init(const char *kvm_de kvm__arch_init(kvm, hugetlbfs_path, ram_size); - kvm->name = name; - kvm_ipc__start(kvm__create_socket(kvm)); kvm_ipc__register_handler(KVM_IPC_PID, kvm__pid); return kvm; Index: linux-2.6.git/tools/kvm/powerpc/include/kvm/kvm-arch.h =================================================================== --- linux-2.6.git.orig/tools/kvm/powerpc/include/kvm/kvm-arch.h +++ linux-2.6.git/tools/kvm/powerpc/include/kvm/kvm-arch.h @@ -64,7 +64,7 @@ struct kvm { unsigned long fdt_gra; unsigned long initrd_gra; unsigned long initrd_size; - const char *name; + char *name; int vm_state; }; Index: linux-2.6.git/tools/kvm/x86/include/kvm/kvm-arch.h =================================================================== --- linux-2.6.git.orig/tools/kvm/x86/include/kvm/kvm-arch.h +++ linux-2.6.git/tools/kvm/x86/include/kvm/kvm-arch.h @@ -48,7 +48,7 @@ struct kvm { struct disk_image **disks; int nr_disks; - const char *name; + char *name; int vm_state; };