From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:34299) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YQfCF-0007Tp-D1 for qemu-devel@nongnu.org; Wed, 25 Feb 2015 11:52:37 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1YQfCE-0000yf-Bl for qemu-devel@nongnu.org; Wed, 25 Feb 2015 11:52:27 -0500 Received: from mx1.redhat.com ([209.132.183.28]:32834) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YQfCE-0000yU-4c for qemu-devel@nongnu.org; Wed, 25 Feb 2015 11:52:26 -0500 From: "Dr. David Alan Gilbert (git)" Date: Wed, 25 Feb 2015 16:51:27 +0000 Message-Id: <1424883128-9841-5-git-send-email-dgilbert@redhat.com> In-Reply-To: <1424883128-9841-1-git-send-email-dgilbert@redhat.com> References: <1424883128-9841-1-git-send-email-dgilbert@redhat.com> Subject: [Qemu-devel] [PATCH v5 04/45] 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, quintela@redhat.com, amit.shah@redhat.com, pbonzini@redhat.com, yanghy@cn.fujitsu.com, david@gibson.dropbear.id.au From: "Dr. David Alan Gilbert" and use it in loadvm_state and ram_load. Signed-off-by: Dr. David Alan Gilbert --- arch_init.c | 5 +---- include/migration/qemu-file.h | 3 +++ migration/qemu-file.c | 16 ++++++++++++++++ savevm.c | 11 ++++++----- 4 files changed, 26 insertions(+), 9 deletions(-) diff --git a/arch_init.c b/arch_init.c index 89c8fa4..91645cc 100644 --- a/arch_init.c +++ b/arch_init.c @@ -1077,13 +1077,10 @@ static int ram_load(QEMUFile *f, void *opaque, int version_id) total_ram_bytes = addr; while (!ret && total_ram_bytes) { RAMBlock *block; - uint8_t len; char id[256]; ram_addr_t length; - len = qemu_get_byte(f); - qemu_get_buffer(f, (uint8_t *)id, len); - id[len] = 0; + qemu_get_counted_string(f, id); length = qemu_get_be64(f); QTAILQ_FOREACH(block, &ram_list.blocks, next) { diff --git a/include/migration/qemu-file.h b/include/migration/qemu-file.h index a923cec..6ae0b03 100644 --- a/include/migration/qemu-file.h +++ b/include/migration/qemu-file.h @@ -310,4 +310,7 @@ 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, char buf[256]); + #endif diff --git a/migration/qemu-file.c b/migration/qemu-file.c index e66e557..57eb868 100644 --- a/migration/qemu-file.c +++ b/migration/qemu-file.c @@ -545,3 +545,19 @@ uint64_t qemu_get_be64(QEMUFile *f) v |= qemu_get_be32(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, char buf[256]) +{ + unsigned int len = qemu_get_byte(f); + int res = qemu_get_buffer(f, (uint8_t *)buf, len); + + buf[len] = 0; + + return res != len; +} + diff --git a/savevm.c b/savevm.c index 192110a..2f8ef45 100644 --- a/savevm.c +++ b/savevm.c @@ -960,8 +960,7 @@ 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]; trace_qemu_loadvm_state_section(section_type); switch (section_type) { @@ -969,9 +968,11 @@ int qemu_loadvm_state(QEMUFile *f) 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, 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); -- 2.1.0