Re: [PATCH 5/8] rtc: rx8010: don't use magic values for time buffer length

From: Alexandre Belloni
Date: Fri Sep 04 2020 - 11:39:18 EST


On 04/09/2020 17:21:13+0200, Bartosz Golaszewski wrote:
> From: Bartosz Golaszewski <bgolaszewski@xxxxxxxxxxxx>
>
> The time buffer len is used directly in this driver. For readability
> it's better to define a constant.
>
> Signed-off-by: Bartosz Golaszewski <bgolaszewski@xxxxxxxxxxxx>
> ---
> drivers/rtc/rtc-rx8010.c | 13 ++++++++-----
> 1 file changed, 8 insertions(+), 5 deletions(-)
>
> diff --git a/drivers/rtc/rtc-rx8010.c b/drivers/rtc/rtc-rx8010.c
> index 67ff06a76629..f3bed7be2533 100644
> --- a/drivers/rtc/rtc-rx8010.c
> +++ b/drivers/rtc/rtc-rx8010.c
> @@ -48,6 +48,8 @@
>
> #define RX8010_ALARM_AE BIT(7)
>
> +#define RX8010_TIME_BUF_LEN 7
> +
> static const struct i2c_device_id rx8010_id[] = {
> { "rx8010", 0 },
> { }
> @@ -108,7 +110,7 @@ static irqreturn_t rx8010_irq_1_handler(int irq, void *dev_id)
> static int rx8010_get_time(struct device *dev, struct rtc_time *dt)
> {
> struct rx8010_data *rx8010 = dev_get_drvdata(dev);
> - u8 date[7];

I'm usually fine with a magic value here...

> + u8 date[RX8010_TIME_BUF_LEN];
> int flagreg, err;
>
> flagreg = i2c_smbus_read_byte_data(rx8010->client, RX8010_FLAG);
> @@ -121,8 +123,8 @@ static int rx8010_get_time(struct device *dev, struct rtc_time *dt)
> }
>
> err = i2c_smbus_read_i2c_block_data(rx8010->client, RX8010_SEC,
> - 7, date);
> - if (err != 7)
> + RX8010_TIME_BUF_LEN, date);

..as long as sizeof(date) is used here.

Even better, you could have date[RX8010_YEAR - RX8010_SEC + 1] and then
use sizeof. Or also have #define RX8010_TIME_BUF_LEN (RX8010_YEAR -
RX8010_SEC + 1) which would be safer than 7.

--
Alexandre Belloni, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com