Re: [PATCH] Generic WorkQueue Engine (GenWQE) device driver (v4)

From: Frank Haverkamp
Date: Mon Nov 04 2013 - 11:44:14 EST


Hi Ryan,

Am Donnerstag, den 31.10.2013, 14:49 +1100 schrieb Ryan Mallon:
> > +static ssize_t show_card_status(struct device *dev,
> > + struct device_attribute *attr,
> > + char *buf)
> > +{
> > + ssize_t len = 0;
> > + struct genwqe_dev *cd = dev_get_drvdata(dev);
> > + const char *cs[GENWQE_CARD_STATE_MAX] = { "unused", "used",
> "error" };
> > +
> > + len += scnprintf(&buf[len], PAGE_SIZE - len,
> > + "%s\n", cs[cd->card_state]);
> > + return len;
>
> This is a bit confusingly written. Why do:
>
> scnprintf(&buf[len], ...
>
> When len is always zero at that point? Since you are printing from an
> array of statically sized strings that are guaranteed to be smaller
> than
> PAGE_SIZE, you can safely do:
>
> sprintf(buf, "%s\n", cs[cd->card_state]);
>
> You might want to add some checking for cd->card_state being out of
> bounds, and printing "unknown" or something in that case.
>
> Same issue exists in other sysfs handlers.
>

I kept using scnprintf(), but reworked the code around it. ssize_t len
is not needed of course and the &buf[len] stuff is obsolete too. I had
sysfs entries with multiple lines of output, where it was convenient and
safe to code it like that. Now since those interfaces were moved to
debugfs, the sysfs code can be simpler, as you pointed it out.

Regards

Frank

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/