Skip to contentSkip to conversation

qdict: fix unbounded stack for qdict_array_entries

PATCH Superseded Peter Xu 2 participants 4 messages Mar 9, 2016 – Mar 22, 2016

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Peter Xu

author
#0 lore raw

PATCH 1/1 qdict: fix unbounded stack for qdict_array_entries

---
 qobject/qdict.c | 15 ++++++---------
 1 file changed, 6 insertions(+), 9 deletions(-)
qobject/qdict.c +6 -9
index 9833bd0..9188a87 100644
@@ -704,19 +704,16 @@ int qdict_array_entries(QDict *src, const char *subqdict)
     for (i = 0; i < INT_MAX; i++) {
         QObject *subqobj;
         int subqdict_entries;
-        size_t slen = 32 + subqdict_len;
-        char indexstr[slen], prefix[slen];
-        size_t snprintf_ret;
+        char *prefix = g_strdup_printf("%s%u.", subqdict, i);
 
-        snprintf_ret = snprintf(indexstr, slen, "%s%u", subqdict, i);
-        assert(snprintf_ret < slen);
+        subqdict_entries = qdict_count_prefixed_entries(src, prefix);
 
-        subqobj = qdict_get(src, indexstr);
+        /* Remove ending "." */
+        prefix[strlen(prefix) - 1] = 0x00;
+        subqobj = qdict_get(src, prefix);
 
-        snprintf_ret = snprintf(prefix, slen, "%s%u.", subqdict, i);
-        assert(snprintf_ret < slen);
+        g_free(prefix);
 
-        subqdict_entries = qdict_count_prefixed_entries(src, prefix);
         if (subqdict_entries < 0) {
             return subqdict_entries;
         }
Signature · 1 line
2.4.3
author
#1 lore raw

Sorry to forgot CCing Eric/Markus/Kevin.

This patch title is not correct, which should be:

"Fix unbounded stack warning for qdict_array_entries"

Do I need to re-send with the same content?

I'm using g_strdup_printf() here, considering it's most convenient, safe, and as long as it's called rarely only when quorum device opens.

Thanks.
Peter

Quoted 38 lines · Peter Xu
---
 qobject/qdict.c | 15 ++++++---------
 1 file changed, 6 insertions(+), 9 deletions(-)
qobject/qdict.c +6 -9
index 9833bd0..9188a87 100644
@@ -704,19 +704,16 @@ int qdict_array_entries(QDict *src, const char *subqdict)
     for (i = 0; i < INT_MAX; i++) {
         QObject *subqobj;
         int subqdict_entries;
-        size_t slen = 32 + subqdict_len;
-        char indexstr[slen], prefix[slen];
-        size_t snprintf_ret;
+        char *prefix = g_strdup_printf("%s%u.", subqdict, i);
 
-        snprintf_ret = snprintf(indexstr, slen, "%s%u", subqdict, i);
-        assert(snprintf_ret < slen);
+        subqdict_entries = qdict_count_prefixed_entries(src, prefix);
 
-        subqobj = qdict_get(src, indexstr);
+        /* Remove ending "." */
+        prefix[strlen(prefix) - 1] = 0x00;
+        subqobj = qdict_get(src, prefix);
 
-        snprintf_ret = snprintf(prefix, slen, "%s%u.", subqdict, i);
-        assert(snprintf_ret < slen);
+        g_free(prefix);
 
-        subqdict_entries = qdict_count_prefixed_entries(src, prefix);
         if (subqdict_entries < 0) {
             return subqdict_entries;
         }
Signature · 1 line
2.4.3
Reviewed-by
#2 lore raw
Quoted 5 lines · Peter Xu

Sorry to forgot CCing Eric/Markus/Kevin.

This patch title is not correct, which should be:

"Fix unbounded stack warning for qdict_array_entries"

Keep the 'qdict:' prefix, but yes, adding "warning" helps the commit message.

Quoted 2 lines

Do I need to re-send with the same content?

For just the title adjustment, it's up to the maintainer. Often, a maintainer will make small changes like that before sending a pull request.

Quoted 4 lines

I'm using g_strdup_printf() here, considering it's most convenient, safe, and as long as it's called rarely only when quorum device opens.

On the other hand, this information might have been useful...

Quoted 6 lines

Thanks.
Peter

Quoted 1 line · Peter Xu

...in the commit body proper (explaining why you are always allocating, because it is not a hot path). So a v2 might indeed be easier.

Quoted 9 lines

qobject/qdict.c int qdict_array_entries(QDict *src, const char *subqdict) · line 707

Quoted 9 lines

+++ b/qobject/qdict.c

qobject/qdict.c +1 -3
@@ -704,19 +704,16 @@ int qdict_array_entries(QDict *src, const char *subqdict)
     for (i = 0; i < INT_MAX; i++) {
         QObject *subqobj;
         int subqdict_entries;
-        size_t slen = 32 + subqdict_len;
-        char indexstr[slen], prefix[slen];
-        size_t snprintf_ret;
+        char *prefix = g_strdup_printf("%s%u.", subqdict, i);

If we were worried that this could be a hot path, you could add a %n and &len here...

Quoted 8 lines

qobject/qdict.c int qdict_array_entries(QDict *src, const char *subqdict) · line 712

Quoted 8 lines
qobject/qdict.c +3 -3
-        snprintf_ret = snprintf(indexstr, slen, "%s%u", subqdict, i);
-        assert(snprintf_ret < slen);
+        subqdict_entries = qdict_count_prefixed_entries(src, prefix);
 
-        subqobj = qdict_get(src, indexstr);
+        /* Remove ending "." */
+        prefix[strlen(prefix) - 1] = 0x00;

...to avoid the strlen() call here. But this is not a hot path, and %n always makes me worry about security, so I'm fine with your approach.

However, 0x00 is a rather verbose way of writing 0 (and even if you want verbosity, '\0' is more idiomatic 0x00).

At this point, if you send a v2 with s/0x00/0/ and the improved commit message, you can also include:

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
author
#3 lore raw
Quoted 58 lines · Eric Blake
Quoted 5 lines · Peter Xu

Sorry to forgot CCing Eric/Markus/Kevin.

This patch title is not correct, which should be:

"Fix unbounded stack warning for qdict_array_entries"

Keep the 'qdict:' prefix, but yes, adding "warning" helps the commit message.

Quoted 2 lines

Do I need to re-send with the same content?

For just the title adjustment, it's up to the maintainer. Often, a maintainer will make small changes like that before sending a pull request.

Quoted 4 lines

I'm using g_strdup_printf() here, considering it's most convenient, safe, and as long as it's called rarely only when quorum device opens.

On the other hand, this information might have been useful...

Quoted 6 lines

Thanks.
Peter

Quoted 1 line · Peter Xu

...in the commit body proper (explaining why you are always allocating, because it is not a hot path). So a v2 might indeed be easier.

Quoted 9 lines

qobject/qdict.c int qdict_array_entries(QDict *src, const char *subqdict) · line 707

Quoted 9 lines

+++ b/qobject/qdict.c

qobject/qdict.c +1 -3
@@ -704,19 +704,16 @@ int qdict_array_entries(QDict *src, const char *subqdict)
     for (i = 0; i < INT_MAX; i++) {
         QObject *subqobj;
         int subqdict_entries;
-        size_t slen = 32 + subqdict_len;
-        char indexstr[slen], prefix[slen];
-        size_t snprintf_ret;
+        char *prefix = g_strdup_printf("%s%u.", subqdict, i);

If we were worried that this could be a hot path, you could add a %n and &len here...

Quoted 8 lines

qobject/qdict.c int qdict_array_entries(QDict *src, const char *subqdict) · line 712

Quoted 8 lines
qobject/qdict.c +3 -3
-        snprintf_ret = snprintf(indexstr, slen, "%s%u", subqdict, i);
-        assert(snprintf_ret < slen);
+        subqdict_entries = qdict_count_prefixed_entries(src, prefix);
 
-        subqobj = qdict_get(src, indexstr);
+        /* Remove ending "." */
+        prefix[strlen(prefix) - 1] = 0x00;

...to avoid the strlen() call here. But this is not a hot path, and %n always makes me worry about security, so I'm fine with your approach.

Quoted 6 lines

However, 0x00 is a rather verbose way of writing 0 (and even if you want verbosity, '\0' is more idiomatic 0x00).

At this point, if you send a v2 with s/0x00/0/ and the improved commit message, you can also include:

Will respin just like above, and with you r-b. Thanks!

-- peterx