qstring: Assert size calculations don't overflow

Backports commit b65ab77b3afadd7bb3051b341a5258ff7fb9d246 from qemu
This commit is contained in:
Markus Armbruster 2018-08-02 21:23:15 -04:00 committed by Lioncash
parent ea6ea4313d
commit 0a6e77ed42
No known key found for this signature in database
GPG key ID: 4E3C3CC1031BA9C7

View file

@ -41,9 +41,12 @@ 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);
assert(qstring->capacity < SIZE_MAX);
qstring->length = end - start + 1;
qstring->capacity = qstring->length;
@ -67,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);