diff mbox

[v3,1/2] qapi: Create DEFINE_PROP_STRING_LEN

Message ID 20151016185405.1248.70564.stgit@gimli.home
State New
Headers show

Commit Message

Alex Williamson Oct. 16, 2015, 6:54 p.m. UTC
A slight addition to set_string(), which optionally tests that the
string length is within the bounds specified.

Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
---
 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 mbox

Patch

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)             \