From 1bff5bbe25eb7a7e7a4e0067c4ca7cbc1cb34999 Mon Sep 17 00:00:00 2001 From: Nikolay Shirokovskiy Date: Mon, 2 Jul 2018 14:16:52 +0300 Subject: [PATCH] util: set OOM in virCopyLastError if error is not set virCopyLastError is intended to be used after last error is set. However due to virLastErrorObject failures (very unlikely though as thread local error is allocated on first use) we can have zero fields in a copy as a result. In particular code field can be set to VIR_ERR_OK. In some places (qemu monitor, qemu agent and qemu migaration code for example) we use copy result as a flag and this leads to bugs. Let's set OOM-like error in copy in case of virLastErrorObject failures. Signed-off-by: Nikolay Shirokovskiy --- .gnulib | 2 +- src/util/virerror.c | 15 +++++++++++---- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/.gnulib b/.gnulib index cdbf3d385a..d6397dde2e 160000 --- a/.gnulib +++ b/.gnulib @@ -1 +1 @@ -Subproject commit cdbf3d385a32ff904c96f20c26f3470bd8345248 +Subproject commit d6397dde2e127e246e3eeb5254a21f42cac783c8 diff --git a/src/util/virerror.c b/src/util/virerror.c index f198f27957..5f26d59777 100644 --- a/src/util/virerror.c +++ b/src/util/virerror.c @@ -366,19 +366,26 @@ virSetError(virErrorPtr newerr) * * One will need to free the result with virResetError() * - * Returns 0 if no error was found and the error code otherwise and -1 in case - * of parameter error. + * Returns error code or -1 in case of parameter error. */ int virCopyLastError(virErrorPtr to) { virErrorPtr err = virLastErrorObject(); + + if (!to) + return -1; + /* We can't guarantee caller has initialized it to zero */ memset(to, 0, sizeof(*to)); - if (err) + if (err) { virCopyError(err, to); - else + } else { virResetError(to); + to->code = VIR_ERR_NO_MEMORY; + to->domain = VIR_FROM_NONE; + to->level = VIR_ERR_ERROR; + } return to->code; }