rtc: mcp795: remove VLA usage
PATCH 1/1 rtc: mcp795: remove VLA usage
In preparation to enabling -Wvla, remove VLAs and replace them with fixed-length arrays instead.
rtc-mcp795.c uses a variable-length array declaration to contain the command to write the rtcc; this can be replaced by a fixed- size array of length 2 (instruction, address) + 32 (data out), assuming a maximum data length of 32 bytes before wrap up.
This was prompted by https://lkml.org/lkml/2018/3/7/621
--- drivers/rtc/rtc-mcp795.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/rtc/rtc-mcp795.c +7 -1
@@ -61,6 +61,9 @@#define SEC_PER_DAY (24 * 60 * 60) +/* Maximum length for data out in write operation to RTCC */ +#define MCP795_MAX_DATAOUT_LEN 32 +static int mcp795_rtcc_read (struct device *dev,u8 addr,u8 *buf,u8 count) {struct spi_device *spi =to_spi_device (dev);
@@ -82,7 +85,10 @@ static int mcp795_rtcc_write(struct device *dev, u8 addr, u8 *data, u8 count) {struct spi_device *spi =to_spi_device (dev);int ret; -u8 tx[2 + count]; +u8 tx[2 +MCP795_MAX_DATAOUT_LEN ]; + +if (count >MCP795_MAX_DATAOUT_LEN ) +return -EINVAL ; tx[0 ] =MCP795_WRITE ; tx[1 ] = addr;
Signature · 1 line
2.14.3
Hi,
Quoted 20 lines · Stefano Manni
In preparation to enabling -Wvla, remove VLAs and replace them with fixed-length arrays instead.
rtc-mcp795.cuses a variable-length array declaration to contain the command to write the rtcc; this can be replaced by a fixed- size array of length 2 (instruction, address) + 32 (data out), assuming a maximum data length of 32 bytes before wrap up.This was prompted by https://lkml.org/lkml/2018/3/7/621
Signed-off-by Stefano Manni <[email protected]>--- drivers/rtc/rtc-mcp795.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)drivers/rtc/rtc-mcp795.c +0
-0
drivers/rtc/rtc-mcp795.c line 66
Quoted 6 lines
drivers/rtc/rtc-mcp795.c +3
-0@@ -61,6 +61,9 @@#define SEC_PER_DAY (24 * 60 * 60) +/* Maximum length for data out in write operation to RTCC */ +#define MCP795_MAX_DATAOUT_LEN 32 +
This is wrong, see https://marc.info/?l=linux-kernel&m=152046370320811&w=2
Signature · 3 lines
Alexandre Belloni, Bootlin (formerly Free Electrons) Embedded Linux and Kernel engineering https://bootlin.com
Hi,
2018-03-13 0:31 GMT+01:00 Alexandre Belloni
<[email protected]>:
Quoted 26 lines
Hi,
Quoted 23 lines · Stefano Manni
In preparation to enabling -Wvla, remove VLAs and replace them with fixed-length arrays instead.
rtc-mcp795.cuses a variable-length array declaration to contain the command to write the rtcc; this can be replaced by a fixed- size array of length 2 (instruction, address) + 32 (data out), assuming a maximum data length of 32 bytes before wrap up.This was prompted by https://lkml.org/lkml/2018/3/7/621
Signed-off-by Stefano Manni <[email protected]>--- drivers/rtc/rtc-mcp795.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)drivers/rtc/rtc-mcp795.c +0
-0@@ -61,6 +61,9 @@#define SEC_PER_DAY (24 * 60 * 60)
Quoted 8 lines
drivers/rtc/rtc-mcp795.c line 66
Quoted 3 lines
drivers/rtc/rtc-mcp795.c +3
-0+/* Maximum length for data out in write operation to RTCC */ +#define MCP795_MAX_DATAOUT_LEN 32 +This is wrong, see https://marc.info/?l=linux-kernel&m=152046370320811&w=2
You sure that the right value to use is 255 + 2? mcp795_rtcc_write() just writes into the RTCC that contains only 32 registers (table 4-1 of datasheet). I assumed
32 as the maximum length of data to write before wrapping up (start from reg 0x0).
Probably 32 is just a wrong assumption but why did you choose 255?
Another thing: don't we need also to check count against the array length?
if (count > MCP795_MAX_DATAOUT_LEN)
return -EINVAL;
Quoted 39 lines · Stefano Manni
Hi,
2018-03-13 0:31 GMT+01:00 Alexandre Belloni
<[email protected]>:Quoted 26 lines
Hi,
Quoted 23 lines · Stefano Manni
In preparation to enabling -Wvla, remove VLAs and replace them with fixed-length arrays instead.
rtc-mcp795.cuses a variable-length array declaration to contain the command to write the rtcc; this can be replaced by a fixed- size array of length 2 (instruction, address) + 32 (data out), assuming a maximum data length of 32 bytes before wrap up.This was prompted by https://lkml.org/lkml/2018/3/7/621
Signed-off-by Stefano Manni <[email protected]>--- drivers/rtc/rtc-mcp795.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)drivers/rtc/rtc-mcp795.c +0
-0@@ -61,6 +61,9 @@#define SEC_PER_DAY (24 * 60 * 60)Quoted 8 lines
drivers/rtc/rtc-mcp795.c line 66
Quoted 3 lines
drivers/rtc/rtc-mcp795.c +3
-0+/* Maximum length for data out in write operation to RTCC */ +#define MCP795_MAX_DATAOUT_LEN 32 +This is wrong, see https://marc.info/?l=linux-kernel&m=152046370320811&w=2
Quoted 7 lines
You sure that the right value to use is 255 + 2?
mcp795_rtcc_write()just writes into the RTCC that contains only 32 registers (table 4-1 of datasheet). I assumed
32 as the maximum length of data to write before wrapping up (start from reg0x0).
Probably 32 is just a wrong assumption but why did you choose 255?
It is not so wrong but the plan is to be able to support reading/writing the EEPROM later but I agree it will need to be changed anyway.
Quoted 4 lines
Another thing: don't we need also to check count against the array length?
if (count >
MCP795_MAX_DATAOUT_LEN)return -EINVAL;
u8 count will never be greater than 255.
Signature · 3 lines
Alexandre Belloni, Bootlin (formerly Free Electrons) Embedded Linux and Kernel engineering https://bootlin.com