Skip to contentSkip to conversation

hw/ssi/xilinx_spips: Avoid variable length array

PATCH In review Peter Maydell 6 participants 8 messages Mar 28, 2019 – Mar 29, 2019 Arm/Zynq architecture

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hw/ssi/xilinx_spips: Avoid variable length array

In the stripe8() function we use a variable length array; however we know that the maximum length required is MAX_NUM_BUSSES. Use a fixed-length array and an assert instead.

---
 hw/ssi/xilinx_spips.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
hw/ssi/xilinx_spips.c +4 -2
index 16f88f74029..c615058cc1b 100644
@@ -429,12 +429,14 @@ static void xlnx_zynqmp_qspips_reset(DeviceState *d)
 
 static inline void stripe8(uint8_t *x, int num, bool dir)
 {
-    uint8_t r[num];
-    memset(r, 0, sizeof(uint8_t) * num);
+    uint8_t r[MAX_NUM_BUSSES];
     int idx[2] = {0, 0};
     int bit[2] = {0, 7};
     int d = dir;
 
+    assert(num <= MAX_NUM_BUSSES);
+    memset(r, 0, sizeof(uint8_t) * num);
+
     for (idx[0] = 0; idx[0] < num; ++idx[0]) {
         for (bit[0] = 7; bit[0] >= 0; bit[0]--) {
             r[idx[!d]] |= x[idx[d]] & 1 << bit[d] ? 1 << bit[!d] : 0;
Signature · 1 line
2.20.1
#1 lore raw

+ Francisco

Quoted 34 lines · On Thu, 28 Mar. 2019, 16:26 Peter Maydell, <[email protected]> wrote:

In the stripe8() function we use a variable length array; however we know that the maximum length required is MAX_NUM_BUSSES. Use a fixed-length array and an assert instead.

---
 hw/ssi/xilinx_spips.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
hw/ssi/xilinx_spips.c +4 -3
index 16f88f74029..c615058cc1b 100644
@@ -429,12 +429,14 @@ static void xlnx_zynqmp_qspips_reset(DeviceState *d)
 
 static inline void stripe8(uint8_t *x, int num, bool dir)
 {
-    uint8_t r[num];
-    memset(r, 0, sizeof(uint8_t) * num);
+    uint8_t r[MAX_NUM_BUSSES];
     int idx[2] = {0, 0};
     int bit[2] = {0, 7};
     int d = dir;
 
+    assert(num <= MAX_NUM_BUSSES);
+    memset(r, 0, sizeof(uint8_t) * num);
+
     for (idx[0] = 0; idx[0] < num; ++idx[0]) {
         for (bit[0] = 7; bit[0] >= 0; bit[0]--) {
             r[idx[!d]] |= x[idx[d]] & 1 << bit[d] ? 1 << bit[!d] : 0;
--

2.20.1

#2 lore raw

Le jeu. 28 mars 2019 16:41, Edgar E. Iglesias <[email protected]> a écrit :

Quoted 29 lines

+ Francisco

Quoted 24 lines · On Thu, 28 Mar. 2019, 16:26 Peter Maydell, <[email protected]> wrote:

In the stripe8() function we use a variable length array; however we know that the maximum length required is MAX_NUM_BUSSES. Use a fixed-length array and an assert instead.

---
 hw/ssi/xilinx_spips.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
hw/ssi/xilinx_spips.c +1 -2
index 16f88f74029..c615058cc1b 100644
@@ -429,12 +429,14 @@ static void xlnx_zynqmp_qspips_reset(DeviceState *d)
 
 static inline void stripe8(uint8_t *x, int num, bool dir)
 {
-    uint8_t r[num];
-    memset(r, 0, sizeof(uint8_t) * num);
+    uint8_t r[MAX_NUM_BUSSES];
     int idx[2] = {0, 0};
     int bit[2] = {0, 7};
     int d = dir;
 
Quoted 10 lines
Quoted 9 lines
hw/ssi/xilinx_spips.c +3 -1
+    assert(num <= MAX_NUM_BUSSES);
+    memset(r, 0, sizeof(uint8_t) * num);
+
     for (idx[0] = 0; idx[0] < num; ++idx[0]) {
         for (bit[0] = 7; bit[0] >= 0; bit[0]--) {
             r[idx[!d]] |= x[idx[d]] & 1 << bit[d] ? 1 << bit[!d] : 0;
--

2.20.1

Quoted 1 line
Reviewed-by on v1 1/1
#3 lore raw
In reply to Peter Maydell
Quoted 5 lines · Peter Maydell

In the stripe8() function we use a variable length array; however we know that the maximum length required is MAX_NUM_BUSSES. Use a fixed-length array and an assert instead.

Quoted 29 lines
---
 hw/ssi/xilinx_spips.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
hw/ssi/xilinx_spips.c +4 -2
index 16f88f74029..c615058cc1b 100644
@@ -429,12 +429,14 @@ static void xlnx_zynqmp_qspips_reset(DeviceState *d)
 
 static inline void stripe8(uint8_t *x, int num, bool dir)
 {
-    uint8_t r[num];
-    memset(r, 0, sizeof(uint8_t) * num);
+    uint8_t r[MAX_NUM_BUSSES];
     int idx[2] = {0, 0};
     int bit[2] = {0, 7};
     int d = dir;
 
+    assert(num <= MAX_NUM_BUSSES);
+    memset(r, 0, sizeof(uint8_t) * num);
+
     for (idx[0] = 0; idx[0] < num; ++idx[0]) {
         for (bit[0] = 7; bit[0] >= 0; bit[0]--) {
             r[idx[!d]] |= x[idx[d]] & 1 << bit[d] ? 1 << bit[!d] : 0;
Signature · 1 line
2.20.1
Reviewed-by on v1 1/1
#4 lore raw
In reply to Peter Maydell
Quoted 6 lines · Peter Maydell

In the stripe8() function we use a variable length array; however we know that the maximum length required is MAX_NUM_BUSSES. Use a fixed-length array and an assert instead.

Who's tree should this go through?

Alistair

Quoted 29 lines
---
 hw/ssi/xilinx_spips.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
hw/ssi/xilinx_spips.c +4 -3
index 16f88f74029..c615058cc1b 100644
@@ -429,12 +429,14 @@ static void xlnx_zynqmp_qspips_reset(DeviceState *d)
 
 static inline void stripe8(uint8_t *x, int num, bool dir)
 {
-    uint8_t r[num];
-    memset(r, 0, sizeof(uint8_t) * num);
+    uint8_t r[MAX_NUM_BUSSES];
     int idx[2] = {0, 0};
     int bit[2] = {0, 7};
     int d = dir;
 
+    assert(num <= MAX_NUM_BUSSES);
+    memset(r, 0, sizeof(uint8_t) * num);
+
     for (idx[0] = 0; idx[0] < num; ++idx[0]) {
         for (bit[0] = 7; bit[0] >= 0; bit[0]--) {
             r[idx[!d]] |= x[idx[d]] & 1 << bit[d] ? 1 << bit[!d] : 0;
--

2.20.1

Reviewed-by on v1 1/1
#5 lore raw
In reply to Peter Maydell
Quoted 9 lines · Peter Maydell

In the stripe8() function we use a variable length array; however we know that the maximum length required is MAX_NUM_BUSSES. Use a fixed-length array and an assert instead.

---
 hw/ssi/xilinx_spips.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

I really think is a good idea to remove variable length arrays!

Thanks,
Stefano

#6 lore raw
Quoted 12 lines · Stefano Garzarella
Quoted 9 lines · Peter Maydell

In the stripe8() function we use a variable length array; however we know that the maximum length required is MAX_NUM_BUSSES. Use a fixed-length array and an assert instead.

---
 hw/ssi/xilinx_spips.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
Quoted 3 lines

I really think is a good idea to remove variable length arrays!

Yeah; we have some but not a vast number, so I thought I'd whittle down a few easy candidates. Feel free to have a look at some yourself :-)

thanks
-- PMM

#7 lore raw
Quoted 13 lines · Peter Maydell
Quoted 12 lines · Stefano Garzarella
Quoted 9 lines · Peter Maydell

In the stripe8() function we use a variable length array; however we know that the maximum length required is MAX_NUM_BUSSES. Use a fixed-length array and an assert instead.

---
 hw/ssi/xilinx_spips.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
Quoted 7 lines
Quoted 3 lines

I really think is a good idea to remove variable length arrays!

Yeah; we have some but not a vast number, so I thought I'd whittle down a few easy candidates. Feel free to have a look at some yourself :-)

I'll take a look for others candidates :)

Thanks,
Stefano