From d82ca915875ac55ba291435f7eb4fe7bfcb2cecb Mon Sep 17 00:00:00 2001 From: Eduardo Habkost Date: Thu, 10 Nov 2011 10:41:43 -0200 Subject: [PATCH] qemu_fclose: return last_error if set (v3) This will make sure no error will be missed as long as callers always check for qemu_fclose() return value. For reference, this is the complete list of qemu_fclose() callers: - exec_close(): already fixed to check for negative values, not -1 - migrate_fd_cleanup(): already fixed to consider only negative values as error, not any non-zero value - exec_accept_incoming_migration(): no return value check (yet) - fd_accept_incoming_migration(): no return value check (yet) - tcp_accept_incoming_migration(): no return value check (yet) - unix_accept_incoming_migration(): no return value check (yet) - do_savevm(): no return value check (yet) - load_vmstate(): no return value check (yet) Changes v1 -> v2: - Add small comment about the need to return previously-spotted errors Changes v2 -> v3: - Add braces to "if" statements to match coding style Signed-off-by: Eduardo Habkost Signed-off-by: Anthony Liguori --- savevm.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 48 insertions(+), 3 deletions(-) diff --git a/savevm.c b/savevm.c index e12109602f..adff42140c 100644 --- a/savevm.c +++ b/savevm.c @@ -436,6 +436,22 @@ void qemu_file_set_error(QEMUFile *f, int ret) f->last_error = ret; } +/** Sets last_error conditionally + * + * Sets last_error only if ret is negative _and_ no error + * was set before. + */ +static void qemu_file_set_if_error(QEMUFile *f, int ret) +{ + if (ret < 0 && !f->last_error) { + qemu_file_set_error(f, ret); + } +} + +/** Flushes QEMUFile buffer + * + * In case of error, last_error is set. + */ void qemu_fflush(QEMUFile *f) { if (!f->put_buffer) @@ -482,12 +498,41 @@ static void qemu_fill_buffer(QEMUFile *f) qemu_file_set_error(f, len); } -int qemu_fclose(QEMUFile *f) +/** Calls close function and set last_error if needed + * + * Internal function. qemu_fflush() must be called before this. + * + * Returns f->close() return value, or 0 if close function is not set. + */ +static int qemu_close(QEMUFile *f) { int ret = 0; - qemu_fflush(f); - if (f->close) + if (f->close) { ret = f->close(f->opaque); + qemu_file_set_if_error(f, ret); + } + return ret; +} + +/** Closes the file + * + * Returns negative error value if any error happened on previous operations or + * while closing the file. Returns 0 or positive number on success. + * + * The meaning of return value on success depends on the specific backend + * being used. + */ +int qemu_fclose(QEMUFile *f) +{ + int ret; + qemu_fflush(f); + ret = qemu_close(f); + /* If any error was spotted before closing, we should report it + * instead of the close() return value. + */ + if (f->last_error) { + ret = f->last_error; + } g_free(f); return ret; }