Patchwork [07/20,v2] target-i386: cpu_x86_register() consolidate freeing resources

login
register
mail settings
Submitter Igor Mammedov
Date Dec. 18, 2012, 4:30 p.m.
Message ID <1355848243-25704-1-git-send-email-imammedo@redhat.com>
Download mbox | patch
Permalink /patch/207154/
State New
Headers show

Comments

Igor Mammedov - Dec. 18, 2012, 4:30 p.m.
freeing resources in one place would require setting 'error'
to not NULL, so add some more error reporting before jumping to
exit branch.

Signed-off-by: Igor Mammedov <imammedo@redhat.com>
---
  v2:
   - add missing 'return -1' on exit if error is not NULL,
           Spotted-By: Eduardo Habkost <ehabkost@redhat.com>
---
 target-i386/cpu.c |   17 ++++++++---------
 1 files changed, 8 insertions(+), 9 deletions(-)
Eduardo Habkost - Dec. 19, 2012, 4:36 p.m.
On Tue, Dec 18, 2012 at 05:30:43PM +0100, Igor Mammedov wrote:
> freeing resources in one place would require setting 'error'
> to not NULL, so add some more error reporting before jumping to
> exit branch.
> 
> Signed-off-by: Igor Mammedov <imammedo@redhat.com>
> ---
>   v2:
>    - add missing 'return -1' on exit if error is not NULL,
>            Spotted-By: Eduardo Habkost <ehabkost@redhat.com>
> ---
>  target-i386/cpu.c |   17 ++++++++---------
>  1 files changed, 8 insertions(+), 9 deletions(-)
> 
> diff --git a/target-i386/cpu.c b/target-i386/cpu.c
> index 3b9bbfe..fe8b76c 100644
> --- a/target-i386/cpu.c
> +++ b/target-i386/cpu.c
> @@ -1550,13 +1550,14 @@ int cpu_x86_register(X86CPU *cpu, const char *cpu_model)
>  
>      model_pieces = g_strsplit(cpu_model, ",", 2);
>      if (!model_pieces[0]) {
> -        goto error;
> +        goto out;

Missing error_set*() call here.

The rest of the patch looks good, to me. I liked this style of handling
errors & freeing resources.


>      }
>      name = model_pieces[0];
>      features = model_pieces[1];
>  
>      if (cpu_x86_find_by_name(def, name) < 0) {
> -        goto error;
> +        error_setg(&error, "Unable to find CPU definition: %s", name);
> +        goto out;
>      }
>  
>      def->kvm_features |= kvm_default_features;
> @@ -1566,22 +1567,20 @@ int cpu_x86_register(X86CPU *cpu, const char *cpu_model)
>                              &def->svm_features, &def->cpuid_7_0_ebx_features);
>  
>      if (cpu_x86_parse_featurestr(def, features) < 0) {
> -        goto error;
> +        error_setg(&error, "Invalid cpu_model string format: %s", cpu_model);
> +        goto out;
>      }
>  
>      cpudef_2_x86_cpu(cpu, def, &error);
>  
> +out:
> +    g_strfreev(model_pieces);
>      if (error) {
>          fprintf(stderr, "%s\n", error_get_pretty(error));
>          error_free(error);
> -        goto error;
> +        return -1;
>      }
> -
> -    g_strfreev(model_pieces);
>      return 0;
> -error:
> -    g_strfreev(model_pieces);
> -    return -1;
>  }
>  
>  #if !defined(CONFIG_USER_ONLY)
> -- 
> 1.7.1
> 
>
Igor Mammedov - Dec. 19, 2012, 4:49 p.m.
On Wed, 19 Dec 2012 14:36:28 -0200
Eduardo Habkost <ehabkost@redhat.com> wrote:

> On Tue, Dec 18, 2012 at 05:30:43PM +0100, Igor Mammedov wrote:
> > freeing resources in one place would require setting 'error'
> > to not NULL, so add some more error reporting before jumping to
> > exit branch.
> > 
> > Signed-off-by: Igor Mammedov <imammedo@redhat.com>
> > ---
> >   v2:
> >    - add missing 'return -1' on exit if error is not NULL,
> >            Spotted-By: Eduardo Habkost <ehabkost@redhat.com>
> > ---
> >  target-i386/cpu.c |   17 ++++++++---------
> >  1 files changed, 8 insertions(+), 9 deletions(-)
> > 
> > diff --git a/target-i386/cpu.c b/target-i386/cpu.c
> > index 3b9bbfe..fe8b76c 100644
> > --- a/target-i386/cpu.c
> > +++ b/target-i386/cpu.c
> > @@ -1550,13 +1550,14 @@ int cpu_x86_register(X86CPU *cpu, const char
> > *cpu_model) 
> >      model_pieces = g_strsplit(cpu_model, ",", 2);
> >      if (!model_pieces[0]) {
> > -        goto error;
> > +        goto out;
> 
> Missing error_set*() call here.
Thinking about this error path, it looks like it's unreachable, because of
every caller of cpu_init() passes in not empty cpu_model.
Perhaps it would make sense to just assert(cpu_model) at the beginning of
function and delete this error check.

> 
> The rest of the patch looks good, to me. I liked this style of handling
> errors & freeing resources.
> 
> 
[snip]
Eduardo Habkost - Dec. 19, 2012, 5:04 p.m.
On Wed, Dec 19, 2012 at 05:49:22PM +0100, Igor Mammedov wrote:
> On Wed, 19 Dec 2012 14:36:28 -0200
> Eduardo Habkost <ehabkost@redhat.com> wrote:
> 
> > On Tue, Dec 18, 2012 at 05:30:43PM +0100, Igor Mammedov wrote:
> > > freeing resources in one place would require setting 'error'
> > > to not NULL, so add some more error reporting before jumping to
> > > exit branch.
> > > 
> > > Signed-off-by: Igor Mammedov <imammedo@redhat.com>
> > > ---
> > >   v2:
> > >    - add missing 'return -1' on exit if error is not NULL,
> > >            Spotted-By: Eduardo Habkost <ehabkost@redhat.com>
> > > ---
> > >  target-i386/cpu.c |   17 ++++++++---------
> > >  1 files changed, 8 insertions(+), 9 deletions(-)
> > > 
> > > diff --git a/target-i386/cpu.c b/target-i386/cpu.c
> > > index 3b9bbfe..fe8b76c 100644
> > > --- a/target-i386/cpu.c
> > > +++ b/target-i386/cpu.c
> > > @@ -1550,13 +1550,14 @@ int cpu_x86_register(X86CPU *cpu, const char
> > > *cpu_model) 
> > >      model_pieces = g_strsplit(cpu_model, ",", 2);
> > >      if (!model_pieces[0]) {
> > > -        goto error;
> > > +        goto out;
> > 
> > Missing error_set*() call here.
> Thinking about this error path, it looks like it's unreachable, because of
> every caller of cpu_init() passes in not empty cpu_model.

Not empty, or just non-NULL? I just hit the check above using -cpu "".

Note that the above check catches both NULL and empty cpu_model strings,
because g_strsplit("", ...) returns an empty array.


> Perhaps it would make sense to just assert(cpu_model) at the beginning of
> function and delete this error check.
> 
> > 
> > The rest of the patch looks good, to me. I liked this style of handling
> > errors & freeing resources.
> > 
> > 
> [snip]
> 
>
Igor Mammedov - Dec. 19, 2012, 5:18 p.m.
On Wed, 19 Dec 2012 15:04:37 -0200
Eduardo Habkost <ehabkost@redhat.com> wrote:

> On Wed, Dec 19, 2012 at 05:49:22PM +0100, Igor Mammedov wrote:
> > On Wed, 19 Dec 2012 14:36:28 -0200
> > Eduardo Habkost <ehabkost@redhat.com> wrote:
> > 
> > > On Tue, Dec 18, 2012 at 05:30:43PM +0100, Igor Mammedov wrote:
> > > > freeing resources in one place would require setting 'error'
> > > > to not NULL, so add some more error reporting before jumping to
> > > > exit branch.
> > > > 
> > > > Signed-off-by: Igor Mammedov <imammedo@redhat.com>
> > > > ---
> > > >   v2:
> > > >    - add missing 'return -1' on exit if error is not NULL,
> > > >            Spotted-By: Eduardo Habkost <ehabkost@redhat.com>
> > > > ---
> > > >  target-i386/cpu.c |   17 ++++++++---------
> > > >  1 files changed, 8 insertions(+), 9 deletions(-)
> > > > 
> > > > diff --git a/target-i386/cpu.c b/target-i386/cpu.c
> > > > index 3b9bbfe..fe8b76c 100644
> > > > --- a/target-i386/cpu.c
> > > > +++ b/target-i386/cpu.c
> > > > @@ -1550,13 +1550,14 @@ int cpu_x86_register(X86CPU *cpu, const char
> > > > *cpu_model) 
> > > >      model_pieces = g_strsplit(cpu_model, ",", 2);
> > > >      if (!model_pieces[0]) {
> > > > -        goto error;
> > > > +        goto out;
> > > 
> > > Missing error_set*() call here.
> > Thinking about this error path, it looks like it's unreachable, because of
> > every caller of cpu_init() passes in not empty cpu_model.
> 
> Not empty, or just non-NULL? I just hit the check above using -cpu "".
> 
> Note that the above check catches both NULL and empty cpu_model strings,
> because g_strsplit("", ...) returns an empty array.
I see, I'll add error message here then.


Thanks!

> 
> 
> > Perhaps it would make sense to just assert(cpu_model) at the beginning of
> > function and delete this error check.
> > 
> > > 
> > > The rest of the patch looks good, to me. I liked this style of handling
> > > errors & freeing resources.
> > > 
> > > 
> > [snip]
> > 
> > 
>

Patch

diff --git a/target-i386/cpu.c b/target-i386/cpu.c
index 3b9bbfe..fe8b76c 100644
--- a/target-i386/cpu.c
+++ b/target-i386/cpu.c
@@ -1550,13 +1550,14 @@  int cpu_x86_register(X86CPU *cpu, const char *cpu_model)
 
     model_pieces = g_strsplit(cpu_model, ",", 2);
     if (!model_pieces[0]) {
-        goto error;
+        goto out;
     }
     name = model_pieces[0];
     features = model_pieces[1];
 
     if (cpu_x86_find_by_name(def, name) < 0) {
-        goto error;
+        error_setg(&error, "Unable to find CPU definition: %s", name);
+        goto out;
     }
 
     def->kvm_features |= kvm_default_features;
@@ -1566,22 +1567,20 @@  int cpu_x86_register(X86CPU *cpu, const char *cpu_model)
                             &def->svm_features, &def->cpuid_7_0_ebx_features);
 
     if (cpu_x86_parse_featurestr(def, features) < 0) {
-        goto error;
+        error_setg(&error, "Invalid cpu_model string format: %s", cpu_model);
+        goto out;
     }
 
     cpudef_2_x86_cpu(cpu, def, &error);
 
+out:
+    g_strfreev(model_pieces);
     if (error) {
         fprintf(stderr, "%s\n", error_get_pretty(error));
         error_free(error);
-        goto error;
+        return -1;
     }
-
-    g_strfreev(model_pieces);
     return 0;
-error:
-    g_strfreev(model_pieces);
-    return -1;
 }
 
 #if !defined(CONFIG_USER_ONLY)