Skip to contentSkip to conversation

util/id: fully allocate names table

Qemu-devel John Snow 3 participants 5 messages Nov 24, 2015 – Nov 25, 2015

lore
Share & tools
lore

John Snow

author
#0 lore raw

Trivial: this array should be allocated to have ID_MAX entries always. Otherwise if someone were to forget to expand this table, the assertion in the id generator won't actually trigger; it will read junk data.

---
 util/id.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
util/id.c +1 -1
index bcc64d8..b7ca4d2 100644
@@ -29,7 +29,7 @@ bool id_wellformed(const char *id)
 
 #define ID_SPECIAL_CHAR '#'
 
-static const char *const id_subsys_str[] = {
+static const char *const id_subsys_str[ID_MAX] = {
     [ID_QDEV]  = "qdev",
     [ID_BLOCK] = "block",
 };
Signature · 1 line
2.4.3
#1 lore raw
Quoted 3 lines · John Snow

Trivial: this array should be allocated to have ID_MAX entries always. Otherwise if someone were to forget to expand this table, the assertion in the id generator won't actually trigger; it will read junk data.

You mean this one:

    assert(id < ID_MAX);

The assertion is crap, because it fails to protect array access id_subsys_str[id]. Here's one that does:

    assert(0 <= id && id < ARRAY_SIZE(id_subsys_str));
Quoted 18 lines
---
 util/id.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
util/id.c +1 -1
index bcc64d8..b7ca4d2 100644
@@ -29,7 +29,7 @@ bool id_wellformed(const char *id)
 
 #define ID_SPECIAL_CHAR '#'
 
-static const char *const id_subsys_str[] = {
+static const char *const id_subsys_str[ID_MAX] = {
     [ID_QDEV]  = "qdev",
     [ID_BLOCK] = "block",
 };
#2 lore raw

Am 25.11.2015 um 09:18 hat Markus Armbruster geschrieben:

Quoted 6 lines
Quoted 3 lines · John Snow

Trivial: this array should be allocated to have ID_MAX entries always. Otherwise if someone were to forget to expand this table, the assertion in the id generator won't actually trigger; it will read junk data.

Quoted 8 lines

You mean this one:

    assert(id < ID_MAX);

The assertion is crap, because it fails to protect array access id_subsys_str[id]. Here's one that does:

    assert(0 <= id && id < ARRAY_SIZE(id_subsys_str));

Or without the kraxelism id >= 0. However, depending on whether enums are signed or unsigned, I seem to remember that this could trigger compiler warnings (comparison is always true). And this one should be unsigned with gcc because it doesn't include negative values.

Kevin

#3 lore raw
Quoted 14 lines · Kevin Wolf

Am 25.11.2015 um 09:18 hat Markus Armbruster geschrieben:

Quoted 13 lines
Quoted 3 lines · John Snow

Trivial: this array should be allocated to have ID_MAX entries always. Otherwise if someone were to forget to expand this table, the assertion in the id generator won't actually trigger; it will read junk data.

You mean this one:

    assert(id < ID_MAX);

The assertion is crap, because it fails to protect array access id_subsys_str[id]. Here's one that does:

Quoted 6 lines
Quoted 1 line
    assert(0 <= id && id < ARRAY_SIZE(id_subsys_str));

Or without the kraxelism id >= 0. However, depending on whether enums are signed or unsigned, I seem to remember that this could trigger compiler warnings (comparison is always true). And this one should be unsigned with gcc because it doesn't include negative values.

Whatever it takes to express the range check in a way the compiler likes.

Since ARRAY_SIZE()'s value is size_t, and size_t is unsigned, the comparison with 0 can simply be omitted.

author
#4 lore raw
In reply to Markus Armbruster
Quoted 10 lines · Markus Armbruster
Quoted 3 lines · John Snow

Trivial: this array should be allocated to have ID_MAX entries always. Otherwise if someone were to forget to expand this table, the assertion in the id generator won't actually trigger; it will read junk data.

You mean this one:

    assert(id < ID_MAX);

Well, sort of. I meant 'assert(id_subsys_str[id])' itself. If you forget to expand the list (It happened to a friend of mine) this assert will pass because it reads garbage.

If you just always expand the full table, though, it will catch you (Err, my friend) being a dummy a little more nicely.

My thought is we need both the range and presence checks. I'll v2 it, thanks.

--js

Quoted 23 lines

The assertion is crap, because it fails to protect array access id_subsys_str[id]. Here's one that does:

    assert(0 <= id && id < ARRAY_SIZE(id_subsys_str));
Quoted 18 lines
---
 util/id.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
util/id.c +1 -1
index bcc64d8..b7ca4d2 100644
@@ -29,7 +29,7 @@ bool id_wellformed(const char *id)
 
 #define ID_SPECIAL_CHAR '#'
 
-static const char *const id_subsys_str[] = {
+static const char *const id_subsys_str[ID_MAX] = {
     [ID_QDEV]  = "qdev",
     [ID_BLOCK] = "block",
 };