From patchwork Fri Oct 16 18:54:05 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alex Williamson X-Patchwork-Id: 531537 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 0F40114029E for ; Sat, 17 Oct 2015 05:56:06 +1100 (AEDT) Received: from localhost ([::1]:55469 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZnAAd-0007NI-RP for incoming@patchwork.ozlabs.org; Fri, 16 Oct 2015 14:56:03 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:43473) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZnA8m-00057B-91 for qemu-devel@nongnu.org; Fri, 16 Oct 2015 14:54:09 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZnA8l-00053n-AY for qemu-devel@nongnu.org; Fri, 16 Oct 2015 14:54:08 -0400 Received: from mx1.redhat.com ([209.132.183.28]:42685) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZnA8l-00053b-3j for qemu-devel@nongnu.org; Fri, 16 Oct 2015 14:54:07 -0400 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by mx1.redhat.com (Postfix) with ESMTPS id 9C85BA2C07; Fri, 16 Oct 2015 18:54:06 +0000 (UTC) Received: from gimli.home (ovpn-113-42.phx2.redhat.com [10.3.113.42]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id t9GIs68E030914; Fri, 16 Oct 2015 14:54:06 -0400 From: Alex Williamson To: qemu-devel@nongnu.org, kvm@vger.kernel.org Date: Fri, 16 Oct 2015 12:54:05 -0600 Message-ID: <20151016185405.1248.70564.stgit@gimli.home> In-Reply-To: <20151016184627.1248.63936.stgit@gimli.home> References: <20151016184627.1248.63936.stgit@gimli.home> User-Agent: StGit/0.17.1-dirty MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.68 on 10.5.11.22 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: pbonzini@redhat.com, imammedo@redhat.com Subject: [Qemu-devel] [PATCH v3 1/2] qapi: Create DEFINE_PROP_STRING_LEN 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 A slight addition to set_string(), which optionally tests that the string length is within the bounds specified. Signed-off-by: Alex Williamson --- hw/core/qdev-properties.c | 7 +++++++ include/hw/qdev-core.h | 1 + include/hw/qdev-properties.h | 16 ++++++++++++++-- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/hw/core/qdev-properties.c b/hw/core/qdev-properties.c index 33e245e..51a05c7 100644 --- a/hw/core/qdev-properties.c +++ b/hw/core/qdev-properties.c @@ -422,6 +422,13 @@ static void set_string(Object *obj, Visitor *v, void *opaque, error_propagate(errp, local_err); return; } + + if (prop->strlen >= 0 && strlen(str) > prop->strlen) { + error_set_from_qdev_prop_error(errp, EINVAL, dev, prop, str); + g_free(str); + return; + } + g_free(*ptr); *ptr = str; } diff --git a/include/hw/qdev-core.h b/include/hw/qdev-core.h index 8057aed..12e3031 100644 --- a/include/hw/qdev-core.h +++ b/include/hw/qdev-core.h @@ -244,6 +244,7 @@ struct Property { int arrayoffset; PropertyInfo *arrayinfo; int arrayfieldsize; + int strlen; }; struct PropertyInfo { diff --git a/include/hw/qdev-properties.h b/include/hw/qdev-properties.h index 77538a8..2a46640 100644 --- a/include/hw/qdev-properties.h +++ b/include/hw/qdev-properties.h @@ -69,6 +69,20 @@ extern PropertyInfo qdev_prop_arraylen; .qtype = QTYPE_QBOOL, \ .defval = (bool)_defval, \ } +#define DEFINE_PROP_STRING(_name, _state, _field) { \ + .name = (_name), \ + .info = &(qdev_prop_string), \ + .strlen = -1, \ + .offset = offsetof(_state, _field) \ + + type_check(char*, typeof_field(_state, _field)), \ + } +#define DEFINE_PROP_STRING_LEN(_name, _state, _field, _strlen) { \ + .name = (_name), \ + .info = &(qdev_prop_string), \ + .strlen = (_strlen), \ + .offset = offsetof(_state, _field) \ + + type_check(char*, typeof_field(_state, _field)), \ + } #define PROP_ARRAY_LEN_PREFIX "len-" @@ -144,8 +158,6 @@ extern PropertyInfo qdev_prop_arraylen; #define DEFINE_PROP_CHR(_n, _s, _f) \ DEFINE_PROP(_n, _s, _f, qdev_prop_chr, CharDriverState*) -#define DEFINE_PROP_STRING(_n, _s, _f) \ - DEFINE_PROP(_n, _s, _f, qdev_prop_string, char*) #define DEFINE_PROP_NETDEV(_n, _s, _f) \ DEFINE_PROP(_n, _s, _f, qdev_prop_netdev, NICPeers) #define DEFINE_PROP_VLAN(_n, _s, _f) \