[libvirt] [RFC PATCH 1/5] storage: encryption: Add deep copy function for storage encryption

Eric Blake eblake at redhat.com
Thu Jun 12 21:19:58 UTC 2014


On 06/12/2014 09:02 AM, Peter Krempa wrote:
> ---
>  src/util/virstorageencryption.c | 39 +++++++++++++++++++++++++++++++++++++++
>  src/util/virstorageencryption.h |  2 ++
>  2 files changed, 41 insertions(+)

I've definitely been wishing for this; as our struct gets more
complicated, piece-wise transfers into a temporary will not be robust,
while copying into a temporary can be.

Did you need to export the new symbol in libvirt_private.syms?

> +virStorageEncryptionPtr
> +virStorageEncryptionCopy(const virStorageEncryption *src)
> +{
> +    virStorageEncryptionPtr ret;
> +    size_t i;
> +
> +    if (VIR_ALLOC(ret) < 0)
> +        return NULL;
> +
> +    ret->nsecrets = src->nsecrets;

Swap this line...

> +
> +    if (VIR_ALLOC_N(ret->secrets, ret->nsecrets) < 0)
> +        goto error;

...here, and use src->nsecrets instead of ret->nsecrets in the
VIR_ALLOC_N.  Why?  Because if VIR_ALLOC_N fails, the error label calls
virStorageEncryptionFree(ret), but that function blindly assumes that
ret->nsecrets is valid and tries to dereference memory.

You forgot:

ret->format = src->format;

> +++ b/src/util/virstorageencryption.h
> @@ -61,6 +61,8 @@ struct _virStorageEncryption {
>      virStorageEncryptionSecretPtr *secrets;
>  };
> 
> +virStorageEncryptionPtr virStorageEncryptionCopy(const virStorageEncryption *src);

add ATTRIBUTE_NONNULL(1), since we blindly dereference src.

ACK with problems fixed.

-- 
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org

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


More information about the libvir-list mailing list