Skip to contentSkip to conversation

util/id: fully allocate names table

PATCH Applied John Snow 4 participants 4 messages Nov 25, 2015 – Nov 29, 2015

lore
Share & tools
lore

John Snow

author
#0 lore raw

PATCH v2 1/1 util/id: fully allocate names table

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.

v2: Fix the range assertion, too. Compare against the known actual size
of the table instead of what it "should" be.

---
 util/id.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
util/id.c +2 -2
index bcc64d8..7883fbe 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",
 };
@@ -53,7 +53,7 @@ char *id_generate(IdSubSystems id)
     static uint64_t id_counters[ID_MAX];
     uint32_t rnd;
 
-    assert(id < ID_MAX);
+    assert(id < ARRAY_SIZE(id_subsys_str));
     assert(id_subsys_str[id]);
 
     rnd = g_random_int_range(0, 100);
Signature · 1 line
2.4.3
Reviewed-by on v2 1/1
#1 lore raw
Quoted 6 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.

v2: Fix the range assertion, too. Compare against the known actual size
of the table instead of what it "should" be.

The v2 blurb would normally appear after the ---; it can be removed by the maintainer.

Quoted 5 lines
---
 util/id.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
Reviewed-by on v2 1/1
#2 lore raw
In reply to John Snow
Quoted 37 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.

v2: Fix the range assertion, too. Compare against the known actual size
of the table instead of what it "should" be.

---
 util/id.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
util/id.c +2 -2
index bcc64d8..7883fbe 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",
 };
@@ -53,7 +53,7 @@ char *id_generate(IdSubSystems id)
     static uint64_t id_counters[ID_MAX];
     uint32_t rnd;
 
-    assert(id < ID_MAX);
+    assert(id < ARRAY_SIZE(id_subsys_str));
     assert(id_subsys_str[id]);
 
     rnd = g_random_int_range(0, 100);
Signature · 1 line
2.4.3
#3 lore raw
In reply to John Snow
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.

Applied to -trivial as a bugfix, with a trivial commit description cleanup, removing this part:

Quoted 2 lines

v2: Fix the range assertion, too. Compare against the known actual size
of the table instead of what it "should" be.

Thanks!

/mjt