From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:60621) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1X37V6-0007NN-J7 for qemu-devel@nongnu.org; Fri, 04 Jul 2014 13:42:26 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1X37Uy-0008Fa-Rb for qemu-devel@nongnu.org; Fri, 04 Jul 2014 13:42:20 -0400 Received: from mx1.redhat.com ([209.132.183.28]:24894) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1X37Uy-0008FO-JM for qemu-devel@nongnu.org; Fri, 04 Jul 2014 13:42:12 -0400 From: "Dr. David Alan Gilbert (git)" Date: Fri, 4 Jul 2014 18:41:16 +0100 Message-Id: <1404495717-4239-6-git-send-email-dgilbert@redhat.com> In-Reply-To: <1404495717-4239-1-git-send-email-dgilbert@redhat.com> References: <1404495717-4239-1-git-send-email-dgilbert@redhat.com> Subject: [Qemu-devel] [PATCH 05/46] Add qemu_get_counted_string to read a string prefixed by a count byte List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: aarcange@redhat.com, yamahata@private.email.ne.jp, lilei@linux.vnet.ibm.com, quintela@redhat.com From: "Dr. David Alan Gilbert" and use it in loadvm_state. Signed-off-by: Dr. David Alan Gilbert --- include/migration/qemu-file.h | 2 ++ qemu-file.c | 15 +++++++++++++++ savevm.c | 18 ++++++++++-------- 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/include/migration/qemu-file.h b/include/migration/qemu-file.h index 1ce3702..e6d3a5c 100644 --- a/include/migration/qemu-file.h +++ b/include/migration/qemu-file.h @@ -322,4 +322,6 @@ static inline void qemu_get_sbe64s(QEMUFile *f, int64_t *pv) { qemu_get_be64s(f, (uint64_t *)pv); } + +int qemu_get_counted_string(QEMUFile *f, uint8_t *buf); #endif diff --git a/qemu-file.c b/qemu-file.c index 69479f1..88cacc7 100644 --- a/qemu-file.c +++ b/qemu-file.c @@ -857,6 +857,21 @@ uint64_t qemu_get_be64(QEMUFile *f) return v; } +/* + * Get a string whose length is determined by a single preceding byte + * A preallocated 256 byte buffer must be passed in. + * Returns: 0 on success and a 0 terminated string in the buffer + */ +int qemu_get_counted_string(QEMUFile *f, uint8_t *buf) +{ + unsigned int len = qemu_get_byte(f); + int res = qemu_get_buffer(f, buf, len); + + buf[len] = 0; + + return res != len; +} + #define QSB_CHUNK_SIZE (1 << 10) #define QSB_MAX_CHUNK_SIZE (10 * QSB_CHUNK_SIZE) diff --git a/savevm.c b/savevm.c index c3a1f68..cb6f0de 100644 --- a/savevm.c +++ b/savevm.c @@ -908,7 +908,7 @@ int qemu_loadvm_state(QEMUFile *f) v = qemu_get_be32(f); if (v == QEMU_VM_FILE_VERSION_COMPAT) { - fprintf(stderr, "SaveVM v2 format is obsolete and don't work anymore\n"); + error_report("SaveVM v2 format is obsolete and don't work anymore"); return -ENOTSUP; } if (v != QEMU_VM_FILE_VERSION) { @@ -918,31 +918,33 @@ int qemu_loadvm_state(QEMUFile *f) while ((section_type = qemu_get_byte(f)) != QEMU_VM_EOF) { uint32_t instance_id, version_id, section_id; SaveStateEntry *se; - char idstr[257]; - int len; + char idstr[256]; switch (section_type) { case QEMU_VM_SECTION_START: case QEMU_VM_SECTION_FULL: /* Read section start */ section_id = qemu_get_be32(f); - len = qemu_get_byte(f); - qemu_get_buffer(f, (uint8_t *)idstr, len); - idstr[len] = 0; + if (qemu_get_counted_string(f, (uint8_t *)idstr)) { + error_report("Unable to read ID string for section %u", + section_id); + return -EINVAL; + } instance_id = qemu_get_be32(f); version_id = qemu_get_be32(f); /* Find savevm section */ se = find_se(idstr, instance_id); if (se == NULL) { - fprintf(stderr, "Unknown savevm section or instance '%s' %d\n", idstr, instance_id); + error_report("Unknown savevm section or instance '%s' %d", + idstr, instance_id); ret = -EINVAL; goto out; } /* Validate version */ if (version_id > se->version_id) { - fprintf(stderr, "savevm: unsupported version %d for '%s' v%d\n", + error_report("savevm: unsupported version %d for '%s' v%d", version_id, idstr, se->version_id); ret = -EINVAL; goto out; -- 1.9.3