From b65ab77b3afadd7bb3051b341a5258ff7fb9d246 Mon Sep 17 00:00:00 2001 From: Markus Armbruster Date: Fri, 27 Jul 2018 08:22:03 +0200 Subject: qstring: Assert size calculations don't overflow Signed-off-by: Markus Armbruster Message-Id: <20180727062204.10401-2-armbru@redhat.com> Reviewed-by: Eric Blake --- qobject/qstring.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'qobject/qstring.c') diff --git a/qobject/qstring.c b/qobject/qstring.c index 18b8eb8..1bb7784 100644 --- a/qobject/qstring.c +++ b/qobject/qstring.c @@ -41,17 +41,19 @@ QString *qstring_from_substr(const char *str, size_t start, size_t end) { QString *qstring; + assert(start <= end + 1); + qstring = g_malloc(sizeof(*qstring)); qobject_init(QOBJECT(qstring), QTYPE_QSTRING); qstring->length = end - start + 1; qstring->capacity = qstring->length; + assert(qstring->capacity < SIZE_MAX); qstring->string = g_malloc(qstring->capacity + 1); memcpy(qstring->string, str + start, qstring->length); qstring->string[qstring->length] = 0; - return qstring; } @@ -68,7 +70,9 @@ QString *qstring_from_str(const char *str) static void capacity_increase(QString *qstring, size_t len) { if (qstring->capacity < (qstring->length + len)) { + assert(len <= SIZE_MAX - qstring->capacity); qstring->capacity += len; + assert(qstring->capacity <= SIZE_MAX / 2); qstring->capacity *= 2; /* use exponential growth */ qstring->string = g_realloc(qstring->string, qstring->capacity + 1); -- cgit v1.1