2024-03-04 16:18:09

by David Lechner

[permalink] [raw]
Subject: [PATCH v2 2/3] spi: axi-spi-engine: use __counted_by() attribute

This adds the __counted_by() attribute to the flex array at the end of
struct spi_engine_program in the AXI SPI Engine controller driver.

The assignment of the length field has to be reordered to be before
the access to the flex array in order to avoid potential compiler
warnings/errors due to adding the __counted_by() attribute.

Suggested-by: Nuno Sá <[email protected]>
Signed-off-by: David Lechner <[email protected]>
---
v2 changes:
* Reordered assignment of length field.
---
drivers/spi/spi-axi-spi-engine.c | 7 ++++---
1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/drivers/spi/spi-axi-spi-engine.c b/drivers/spi/spi-axi-spi-engine.c
index d89f75170c9e..a8f626165f44 100644
--- a/drivers/spi/spi-axi-spi-engine.c
+++ b/drivers/spi/spi-axi-spi-engine.c
@@ -75,7 +75,7 @@

struct spi_engine_program {
unsigned int length;
- uint16_t instructions[];
+ uint16_t instructions[] __counted_by(length);
};

/**
@@ -115,9 +115,10 @@ struct spi_engine {
static void spi_engine_program_add_cmd(struct spi_engine_program *p,
bool dry, uint16_t cmd)
{
- if (!dry)
- p->instructions[p->length] = cmd;
p->length++;
+
+ if (!dry)
+ p->instructions[p->length - 1] = cmd;
}

static unsigned int spi_engine_get_config(struct spi_device *spi)

--
2.43.2



2024-03-04 17:55:28

by Kees Cook

[permalink] [raw]
Subject: Re: [PATCH v2 2/3] spi: axi-spi-engine: use __counted_by() attribute

On Mon, Mar 04, 2024 at 10:04:24AM -0600, David Lechner wrote:
> This adds the __counted_by() attribute to the flex array at the end of
> struct spi_engine_program in the AXI SPI Engine controller driver.
>
> The assignment of the length field has to be reordered to be before
> the access to the flex array in order to avoid potential compiler
> warnings/errors due to adding the __counted_by() attribute.
>
> Suggested-by: Nuno S? <[email protected]>
> Signed-off-by: David Lechner <[email protected]>

Looks good! Thanks for the respin.

Reviewed-by: Kees Cook <[email protected]>

--
Kees Cook