From patchwork Mon Mar 24 14:38:01 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Michael S. Tsirkin" X-Patchwork-Id: 333069 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 6903D140095 for ; Tue, 25 Mar 2014 01:51:53 +1100 (EST) Received: from localhost ([::1]:36763 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WS63w-0006jY-By for incoming@patchwork.ozlabs.org; Mon, 24 Mar 2014 10:41:16 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:46963) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WS60q-00024H-JL for qemu-devel@nongnu.org; Mon, 24 Mar 2014 10:38:10 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1WS60k-00008P-JN for qemu-devel@nongnu.org; Mon, 24 Mar 2014 10:38:04 -0400 Received: from mx1.redhat.com ([209.132.183.28]:9036) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WS60k-000086-Ag for qemu-devel@nongnu.org; Mon, 24 Mar 2014 10:37:58 -0400 Received: from int-mx02.intmail.prod.int.phx2.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id s2OEbupI003250 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Mon, 24 Mar 2014 10:37:56 -0400 Received: from redhat.com (vpn1-6-85.ams2.redhat.com [10.36.6.85]) by int-mx02.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with SMTP id s2OEbskC003214; Mon, 24 Mar 2014 10:37:55 -0400 Date: Mon, 24 Mar 2014 16:38:01 +0200 From: "Michael S. Tsirkin" To: qemu-devel@nongnu.org Message-ID: <1395671853-2685-4-git-send-email-mst@redhat.com> References: <1395671853-2685-1-git-send-email-mst@redhat.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <1395671853-2685-1-git-send-email-mst@redhat.com> X-Mutt-Fcc: =sent X-Scanned-By: MIMEDefang 2.67 on 10.5.11.12 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: Peter Maydell Subject: [Qemu-devel] [RFC v2 3/5] vmstate: add VMS_MUST_EXIST 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 Can be used to verify a required field exists or validate state in some other way. Signed-off-by: Michael S. Tsirkin --- include/migration/vmstate.h | 1 + vmstate.c | 10 +++++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/include/migration/vmstate.h b/include/migration/vmstate.h index 3a1587e..eb90cef 100644 --- a/include/migration/vmstate.h +++ b/include/migration/vmstate.h @@ -101,6 +101,7 @@ enum VMStateFlags { VMS_MULTIPLY = 0x200, /* multiply "size" field by field_size */ VMS_VARRAY_UINT8 = 0x400, /* Array with size in uint8_t field*/ VMS_VARRAY_UINT32 = 0x800, /* Array with size in uint32_t field*/ + VMS_MUST_EXIST = 0x1000, /* Field must exist in input */ }; typedef struct { diff --git a/vmstate.c b/vmstate.c index fe53735..4943b83 100644 --- a/vmstate.c +++ b/vmstate.c @@ -13,7 +13,7 @@ static int vmstate_n_elems(void *opaque, VMStateField *field) { int n_elems = 1; - if (!(field->flags & ~VMS_NONE)) { + if (!(field->flags & ~(VMS_NONE | VMS_MUST_EXIST))) { n_elems = 0; } else if (field->flags & VMS_ARRAY) { n_elems = field->num; @@ -107,6 +107,9 @@ int vmstate_load_state(QEMUFile *f, const VMStateDescription *vmsd, } } field++; + } else if (field->flags & VMS_MUST_EXIST) { + fprintf(stderr, "Input validation failed: %s/%s\n", vmsd->name, field->name); + return -1; } ret = vmstate_subsection_load(f, vmsd, opaque); if (ret != 0) { @@ -148,6 +151,11 @@ void vmstate_save_state(QEMUFile *f, const VMStateDescription *vmsd, field->info->put(f, addr, size); } } + } else { + if (field->flags & VMS_MUST_EXIST) { + fprintf(stderr, "Input validation failed: %s/%s\n", vmsd->name, field->name); + assert(!(field->flags & VMS_MUST_EXIST)); + } } field++; }