lkml.org 
[lkml]   [2017]   [Aug]   [24]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
Date
From
SubjectRe: [PATCH] of/device: Fix of_device_get_modalias() buffer handling
On Thu 24 Aug 09:51 PDT 2017, Rob Herring wrote:

> On Wed, Aug 23, 2017 at 8:03 PM, Bjorn Andersson
> <bjorn.andersson@linaro.org> wrote:
[..]
> > @@ -206,12 +207,16 @@ static ssize_t of_device_get_modalias(struct device *dev, char *str, ssize_t len
> > /* Name & Type */
> > csize = snprintf(str, len, "of:N%sT%s", dev->of_node->name,
> > dev->of_node->type);
> > + tsize = csize;
> > len -= csize;
> > - str += csize;
> > + if (str)
> > + str += csize;
> >
> > of_property_for_each_string(dev->of_node, "compatible", p, compat) {
> > - if (strlen(compat) + 2 > len)
> > - break;
> > + csize = strlen(compat) + 1;
> > + tsize += csize;
> > + if (csize > len)
> > + continue;
> >
> > csize = snprintf(str, len, "C%s", compat);
>
> We could just use the snprintf to give us the length. Something like
> this following the snprintf:
>
> tsize +=csize;
> if (csize > len) {
> if (len) {
> str[0] = '\0';
> len = 0;
> }
> continue;
> }
>
> You'd need to prevent len from going negative up above too. It ends up
> being more lines but you save a strlen call. So perhaps it's fine as
> is, but since I've written it already throwing it out there...
>

Indeed, I did attempt a few variants of this as well, but ended up
taking the cost of an extra strlen() to keep the complexity of the code
down.

Regards,
Bjorn

\
 
 \ /
  Last update: 2017-08-24 19:25    [W:0.077 / U:5.724 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site