[libvirt] [PATCH] esx: Simplify goto usage (part 1/2)

Eric Blake eblake at redhat.com
Wed May 26 19:49:11 UTC 2010


On 05/26/2010 10:30 AM, Matthias Bolte wrote:
> Eliminate almost all backward jumps by replacing this common pattern:
> 
> int
> some_random_function(void)
> {
>     int result = 0;
>     ...
> 
>   cleanup:
>     <unconditional cleanup code>
>     return result;
> 
>   failure:
>     <cleanup code in case of an error>
>     result = -1;
>     goto cleanup
> }
> 
> with this simpler pattern:
> 
> int
> some_random_function(void)
> {
>     int result = -1;
>     ...
>     result = 0;
> 
>   cleanup:
>     if (result < 0) {
>         <cleanup code in case of an error>
>     }
> 
>     <unconditional cleanup code>
>     return result;
> }

Mostly mechanical, according to that pattern.

> @@ -1786,21 +1733,17 @@ esxDomainGetInfo(virDomainPtr domain, virDomainInfoPtr info)
>          esxVI_LookupVirtualMachineByUuid(priv->host, domain->uuid,
>                                           propertyNameList, &virtualMachine,
>                                           esxVI_Occurrence_RequiredItem) < 0) {
> -        goto failure;
> +        goto cleanup;
>      }
>  
>      info->state = VIR_DOMAIN_NOSTATE;
> -    info->maxMem = 0;
> -    info->memory = 0;
> -    info->nrVirtCpu = 0;
> -    info->cpuTime = 0; /* FIXME */

I didn't quite follow this deletion just from the patch itself; are
these values not valid if info->state is VIR_DOMAIN_NOSTATE?

> @@ -2219,6 +2158,10 @@ esxDomainDumpXML(virDomainPtr domain, int flags)
>      }
>  
>    cleanup:
> +    if (url == NULL) {
> +        virBufferFreeAndReset(&buffer);
> +    }
> +
>      esxVI_String_Free(&propertyNameList);
>      esxVI_ObjectContent_Free(&virtualMachine);
>      VIR_FREE(datastoreName);
> @@ -2229,12 +2172,6 @@ esxDomainDumpXML(virDomainPtr domain, int flags)
>      virDomainDefFree(def);
>  
>      return xml;
> -
> -  failure:
> -    virBufferFreeAndReset(&buffer);
> -    VIR_FREE(xml);

Did we lose a VIR_FREE(xml) on this conversion?  Or can we guarantee
that xml is NULL if url is NULL?

ACK, assuming you can either answer those questions or tweak the code to
fix them.

-- 
Eric Blake   eblake at redhat.com    +1-801-349-2682
Libvirt virtualization library http://libvirt.org

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 619 bytes
Desc: OpenPGP digital signature
URL: <http://listman.redhat.com/archives/libvir-list/attachments/20100526/12d465d7/attachment-0001.sig>


More information about the libvir-list mailing list