From patchwork Fri Jun 12 05:03:39 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Juan Quintela X-Patchwork-Id: 483373 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 10E7B14028F for ; Fri, 12 Jun 2015 15:10:19 +1000 (AEST) Received: from localhost ([::1]:49490 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Z3HEO-0007FE-Qo for incoming@patchwork.ozlabs.org; Fri, 12 Jun 2015 01:10:16 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:39219) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Z3H8W-000547-Cp for qemu-devel@nongnu.org; Fri, 12 Jun 2015 01:04:13 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Z3H8V-0001RG-Ap for qemu-devel@nongnu.org; Fri, 12 Jun 2015 01:04:12 -0400 Received: from mx1.redhat.com ([209.132.183.28]:57704) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Z3H8V-0001RA-1K for qemu-devel@nongnu.org; Fri, 12 Jun 2015 01:04:11 -0400 Received: from int-mx14.intmail.prod.int.phx2.redhat.com (int-mx14.intmail.prod.int.phx2.redhat.com [10.5.11.27]) by mx1.redhat.com (Postfix) with ESMTPS id AEDBC2EBCE8 for ; Fri, 12 Jun 2015 05:04:10 +0000 (UTC) Received: from trasno.mitica (ovpn-116-127.ams2.redhat.com [10.36.116.127]) by int-mx14.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id t5C53paB008114; Fri, 12 Jun 2015 01:04:09 -0400 From: Juan Quintela To: qemu-devel@nongnu.org Date: Fri, 12 Jun 2015 07:03:39 +0200 Message-Id: <1434085430-29219-11-git-send-email-quintela@redhat.com> In-Reply-To: <1434085430-29219-1-git-send-email-quintela@redhat.com> References: <1434085430-29219-1-git-send-email-quintela@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.27 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: amit.shah@redhat.com, "Dr. David Alan Gilbert" Subject: [Qemu-devel] [PULL 10/21] Add qemu_get_counted_string to read a string prefixed by a count byte X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org From: "Dr. David Alan Gilbert" and use it in loadvm_state and ram_load. Where ever it's used, check the return and error if it failed. Minor: ram_load was using a 257 byte array for its string, the maximum length is 255 bytes + 0 terminator, so fix to 256 Signed-off-by: Dr. David Alan Gilbert Reviewed-by: Amit Shah Reviewed-by: David Gibson Reviewed-by: Juan Quintela Signed-off-by: Juan Quintela --- include/migration/qemu-file.h | 3 +++ migration/qemu-file.c | 17 +++++++++++++++++ migration/savevm.c | 11 ++++++----- 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/include/migration/qemu-file.h b/include/migration/qemu-file.h index a01c5b8..318aa1e 100644 --- a/include/migration/qemu-file.h +++ b/include/migration/qemu-file.h @@ -312,4 +312,7 @@ static inline void qemu_get_sbe64s(QEMUFile *f, int64_t *pv) { qemu_get_be64s(f, (uint64_t *)pv); } + +size_t qemu_get_counted_string(QEMUFile *f, char buf[256]); + #endif diff --git a/migration/qemu-file.c b/migration/qemu-file.c index 2750365..0ef543a 100644 --- a/migration/qemu-file.c +++ b/migration/qemu-file.c @@ -585,3 +585,20 @@ int qemu_put_qemu_file(QEMUFile *f_des, QEMUFile *f_src) } return len; } + +/* + * Get a string whose length is determined by a single preceding byte + * A preallocated 256 byte buffer must be passed in. + * Returns: len on success and a 0 terminated string in the buffer + * else 0 + * (Note a 0 length string will return 0 either way) + */ +size_t qemu_get_counted_string(QEMUFile *f, char buf[256]) +{ + size_t len = qemu_get_byte(f); + size_t res = qemu_get_buffer(f, (uint8_t *)buf, len); + + buf[res] = 0; + + return res == len ? res : 0; +} diff --git a/migration/savevm.c b/migration/savevm.c index 002f9b8..2b0aa65 100644 --- a/migration/savevm.c +++ b/migration/savevm.c @@ -973,8 +973,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) { @@ -982,9 +981,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);