@@ -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;
}
@@ -244,6 +244,7 @@ struct Property {
int arrayoffset;
PropertyInfo *arrayinfo;
int arrayfieldsize;
+ int strlen;
};
struct PropertyInfo {
@@ -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) \
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(-)