From: Markus Armbruster <armbru@redhat.com>
To: qemu-devel@nongnu.org
Cc: liujunjie23@huawei.com
Subject: [Qemu-devel] [PATCH 1/2] qstring: Assert size calculations don't overflow
Date: Thu, 26 Jul 2018 08:18:43 +0200 [thread overview]
Message-ID: <20180726061844.25992-2-armbru@redhat.com> (raw)
In-Reply-To: <20180726061844.25992-1-armbru@redhat.com>
Signed-off-by: Markus Armbruster <armbru@redhat.com>
---
qobject/qstring.c | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)
diff --git a/qobject/qstring.c b/qobject/qstring.c
index 18b8eb82f8..7990569c5a 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 + len <= SIZE_MAX / 2);
qstring->capacity *= 2; /* use exponential growth */
qstring->string = g_realloc(qstring->string, qstring->capacity + 1);
--
2.17.1
next prev parent reply other threads:[~2018-07-26 6:18 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-07-26 6:18 [Qemu-devel] [PATCH 0/2] qstring: Safer qstring_from_substr() Markus Armbruster
2018-07-26 6:18 ` Markus Armbruster [this message]
2018-07-26 14:04 ` [Qemu-devel] [PATCH 1/2] qstring: Assert size calculations don't overflow Eric Blake
2018-07-26 17:27 ` Markus Armbruster
2018-07-26 6:18 ` [Qemu-devel] [PATCH 2/2] qstring: Move qstring_from_substr()'s @end one to the right Markus Armbruster
2018-07-26 14:07 ` Eric Blake
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20180726061844.25992-2-armbru@redhat.com \
--to=armbru@redhat.com \
--cc=liujunjie23@huawei.com \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).