From: Bjorn Andersson <bjorn.andersson-QSEj5FYQhm4dnm+yROfE0A@public.gmane.org>
To: Rob Herring <robh+dt-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org>
Cc: Frank Rowand
<frowand.list-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>,
"devicetree-u79uwXL29TY76Z2rM5mHXA@public.gmane.org"
<devicetree-u79uwXL29TY76Z2rM5mHXA@public.gmane.org>,
"linux-kernel-u79uwXL29TY76Z2rM5mHXA@public.gmane.org"
<linux-kernel-u79uwXL29TY76Z2rM5mHXA@public.gmane.org>
Subject: Re: [PATCH] of/device: Fix of_device_get_modalias() buffer handling
Date: Thu, 24 Aug 2017 10:25:09 -0700 [thread overview]
Message-ID: <20170824172509.GE29306@minitux> (raw)
In-Reply-To: <CAL_JsqLqc488ALEqZT-PKw54h5QAbU=q=CEhM9jwROmukR7fZg-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
On Thu 24 Aug 09:51 PDT 2017, Rob Herring wrote:
> On Wed, Aug 23, 2017 at 8:03 PM, Bjorn Andersson
> <bjorn.andersson-QSEj5FYQhm4dnm+yROfE0A@public.gmane.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
--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
prev parent reply other threads:[~2017-08-24 17:25 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-08-24 1:03 [PATCH] of/device: Fix of_device_get_modalias() buffer handling Bjorn Andersson
[not found] ` <20170824010352.9085-1-bjorn.andersson-QSEj5FYQhm4dnm+yROfE0A@public.gmane.org>
2017-08-24 16:51 ` Rob Herring
[not found] ` <CAL_JsqLqc488ALEqZT-PKw54h5QAbU=q=CEhM9jwROmukR7fZg-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2017-08-24 17:25 ` Bjorn Andersson [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20170824172509.GE29306@minitux \
--to=bjorn.andersson-qsej5fyqhm4dnm+yrofe0a@public.gmane.org \
--cc=devicetree-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
--cc=frowand.list-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org \
--cc=linux-kernel-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
--cc=robh+dt-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).