From 1efd6e072cb13b7a7050acc9c673eb4ff25ddfc9 Mon Sep 17 00:00:00 2001 From: John Snow Date: Tue, 29 Jul 2014 19:28:57 -0400 Subject: [PATCH 1/2] virtio-rng: Move error-checking forward to prevent memory leak This patch pushes the error-checking forward and the virtio initialization backward in the device realization function in order to prevent memory leaks for hot plug scenarios. Signed-off-by: John Snow Reviewed-by: Markus Armbruster Reviewed-by: Stefan Hajnoczi Signed-off-by: Amit Shah --- hw/virtio/virtio-rng.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/hw/virtio/virtio-rng.c b/hw/virtio/virtio-rng.c index 7c5a675674..ff589172bd 100644 --- a/hw/virtio/virtio-rng.c +++ b/hw/virtio/virtio-rng.c @@ -147,6 +147,14 @@ static void virtio_rng_device_realize(DeviceState *dev, Error **errp) return; } + /* Workaround: Property parsing does not enforce unsigned integers, + * So this is a hack to reject such numbers. */ + if (vrng->conf.max_bytes > INT64_MAX) { + error_set(errp, QERR_INVALID_PARAMETER_VALUE, "max-bytes", + "a non-negative integer below 2^63"); + return; + } + if (vrng->conf.rng == NULL) { vrng->conf.default_backend = RNG_RANDOM(object_new(TYPE_RNG_RANDOM)); @@ -171,23 +179,15 @@ static void virtio_rng_device_realize(DeviceState *dev, Error **errp) "rng", NULL); } - virtio_init(vdev, "virtio-rng", VIRTIO_ID_RNG, 0); - vrng->rng = vrng->conf.rng; if (vrng->rng == NULL) { error_set(errp, QERR_INVALID_PARAMETER_VALUE, "rng", "a valid object"); return; } - vrng->vq = virtio_add_queue(vdev, 8, handle_input); + virtio_init(vdev, "virtio-rng", VIRTIO_ID_RNG, 0); - /* Workaround: Property parsing does not enforce unsigned integers, - * So this is a hack to reject such numbers. */ - if (vrng->conf.max_bytes > INT64_MAX) { - error_set(errp, QERR_INVALID_PARAMETER_VALUE, "max-bytes", - "a non-negative integer below 2^63"); - return; - } + vrng->vq = virtio_add_queue(vdev, 8, handle_input); vrng->quota_remaining = vrng->conf.max_bytes; vrng->rate_limit_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL, From c617dd3b7e8e82511060b8f7a9c51e46c5c1e87a Mon Sep 17 00:00:00 2001 From: John Snow Date: Tue, 29 Jul 2014 19:28:58 -0400 Subject: [PATCH 2/2] virtio-rng: replace error_set calls with error_setg Under recommendation from Luiz Capitulino, we are changing the error_set calls to error_setg while we are fixing up the error handling pathways of virtio-rng. Signed-off-by: John Snow Reviewed-by: Markus Armbruster Reviewed-by: Stefan Hajnoczi Signed-off-by: Amit Shah --- hw/virtio/virtio-rng.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/hw/virtio/virtio-rng.c b/hw/virtio/virtio-rng.c index ff589172bd..03fd04a1e5 100644 --- a/hw/virtio/virtio-rng.c +++ b/hw/virtio/virtio-rng.c @@ -142,16 +142,15 @@ static void virtio_rng_device_realize(DeviceState *dev, Error **errp) Error *local_err = NULL; if (!vrng->conf.period_ms > 0) { - error_set(errp, QERR_INVALID_PARAMETER_VALUE, "period", - "a positive number"); + error_setg(errp, "'period' parameter expects a positive integer"); return; } /* Workaround: Property parsing does not enforce unsigned integers, * So this is a hack to reject such numbers. */ if (vrng->conf.max_bytes > INT64_MAX) { - error_set(errp, QERR_INVALID_PARAMETER_VALUE, "max-bytes", - "a non-negative integer below 2^63"); + error_setg(errp, "'max-bytes' parameter must be non-negative, " + "and less than 2^63"); return; } @@ -181,7 +180,7 @@ static void virtio_rng_device_realize(DeviceState *dev, Error **errp) vrng->rng = vrng->conf.rng; if (vrng->rng == NULL) { - error_set(errp, QERR_INVALID_PARAMETER_VALUE, "rng", "a valid object"); + error_setg(errp, "'rng' parameter expects a valid object"); return; }