Re: [PATCH v2] iio: sx9324: avoid copying property strings

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Quoting Jonathan Cameron (2023-10-28 08:26:38)
> On Thu, 26 Oct 2023 23:53:28 +0000
> Justin Stitt <justinstitt@xxxxxxxxxx> wrote:
>
> > We're doing some needless string copies when trying to assign the proper
> > `prop` string. We can make `prop` a const char* and simply assign to
> > string literals.
> >
> > For the case where a format string is used, let's allocate some memory
> > via kasprintf() and point prop to it.
> >
> > This also cleans up some deprecated strncpy() uses [1].
> >
> > Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings [1]
> > Link: https://github.com/KSPP/linux/issues/90
> > Cc: linux-hardening@xxxxxxxxxxxxxxx
> > Signed-off-by: Justin Stitt <justinstitt@xxxxxxxxxx>
>
> Seems reasonable to me.
>
> +CC Gwendal (+ Stephen) as it's Gwendal's driver and I think they are still actively
> maintaining it.

Thanks! I have some review comments.

> > diff --git a/drivers/iio/proximity/sx9324.c b/drivers/iio/proximity/sx9324.c
> > index 438f9c9aba6e..c8547035cb47 100644
> > --- a/drivers/iio/proximity/sx9324.c
> > +++ b/drivers/iio/proximity/sx9324.c
> > @@ -885,7 +885,7 @@ sx9324_get_default_reg(struct device *dev, int idx,
> >  #define SX9324_RESOLUTION_DEF "semtech,ph01-resolution"
> >  #define SX9324_PROXRAW_DEF "semtech,ph01-proxraw-strength"
> >       unsigned int pin_defs[SX9324_NUM_PINS];
> > -     char prop[] = SX9324_PROXRAW_DEF;
> > +     const char *prop = SX9324_PROXRAW_DEF;

Do we need this define anymore, or the initialization?

> >       u32 start = 0, raw = 0, pos = 0;
> >       int ret, count, ph, pin;
> >       const char *res;
> > @@ -899,7 +899,7 @@ sx9324_get_default_reg(struct device *dev, int idx,
> >       case SX9324_REG_AFE_PH2:
> >       case SX9324_REG_AFE_PH3:
> >               ph = reg_def->reg - SX9324_REG_AFE_PH0;
> > -             snprintf(prop, ARRAY_SIZE(prop), "semtech,ph%d-pin", ph);
> > +             prop = kasprintf(GFP_KERNEL, "semtech,ph%d-pin", ph);

Do we not care if the allocation fails? We just use the default?

> >
> >               count = device_property_count_u32(dev, prop);
> >               if (count != ARRAY_SIZE(pin_defs))
> > @@ -913,6 +913,7 @@ sx9324_get_default_reg(struct device *dev, int idx,
> >                       raw |= (pin_defs[pin] << (2 * pin)) &
> >                              SX9324_REG_AFE_PH0_PIN_MASK(pin);
> >               reg_def->def = raw;
> > +             kfree(prop);

We need to free it in other places too, like if the count doesn't match.
It may be easier to extract this section and just have 4 string
literals.

	switch (reg_def->reg) {
        case SX9324_REG_AFE_PH0:
		reg_def = sx9324_parse_phase_prop(dev, reg_def, "semtech,ph0-pin");
		break;
        case SX9324_REG_AFE_PH1:
		reg_def = sx9324_parse_phase_prop(dev, reg_def, "semtech,ph1-pin");
		break;
        case SX9324_REG_AFE_PH2:
		reg_def = sx9324_parse_phase_prop(dev, reg_def, "semtech,ph2-pin");
		break;
        case SX9324_REG_AFE_PH3:
		reg_def = sx9324_parse_phase_prop(dev, reg_def, "semtech,ph3-pin");
		break;

> >       case SX9324_REG_AFE_CTRL0:
> >               ret = device_property_read_string(dev,



[Index of Archives]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Input]     [Linux Kernel]     [Linux SCSI]     [X.org]

  Powered by Linux