linux-trace-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Slavomir Kaslev <slavomir.kaslev@gmail.com>
To: "Yordan Karadzhov (VMware)" <y.karadz@gmail.com>
Cc: Yordan Karadzhov <ykaradzhov@vmware.com>,
	Steven Rostedt <rostedt@goodmis.org>,
	linux-trace-devel@vger.kernel.org
Subject: Re: [PATCH v2 4/4] kernel-shark: Fix a bug in KsPluginManager
Date: Mon, 11 Mar 2019 15:44:36 +0200	[thread overview]
Message-ID: <CAE0o1Nv5g6wujxY8ey2mQ72kk977s7tCWq9Hcab1zfNU3rNOVg@mail.gmail.com> (raw)
In-Reply-To: <351105f9-557d-5a2d-99f1-0f93db2848be@gmail.com>

On Mon, Mar 11, 2019 at 3:20 PM Yordan Karadzhov (VMware)
<y.karadz@gmail.com> wrote:
>
>
>
> On 11.03.19 г. 14:09 ч., Slavomir Kaslev wrote:
> > On Thu, Mar 7, 2019 at 5:44 PM Yordan Karadzhov <ykaradzhov@vmware.com> wrote:
> >>
> >> const char *lib = plugin.toStdString().c_str();
> >>
> >> This line is a bad idea because the returned array may (will) be
> >> invalidated when the destructor of std::string is called.
> >>
> >> Signed-off-by: Yordan Karadzhov <ykaradzhov@vmware.com>
> >> ---
> >>   kernel-shark/src/KsUtils.cpp | 4 ++--
> >>   1 file changed, 2 insertions(+), 2 deletions(-)
> >>
> >> diff --git a/kernel-shark/src/KsUtils.cpp b/kernel-shark/src/KsUtils.cpp
> >> index 34b2e2d..d7b1753 100644
> >> --- a/kernel-shark/src/KsUtils.cpp
> >> +++ b/kernel-shark/src/KsUtils.cpp
> >> @@ -439,7 +439,7 @@ void KsPluginManager::registerFromList(kshark_context *kshark_ctx)
> >>
> >>          auto lamRegUser = [&kshark_ctx](const QString &plugin)
> >>          {
> >> -               const char *lib = plugin.toStdString().c_str();
> >> +               const char *lib = plugin.toLocal8Bit().data();
> >>                  kshark_register_plugin(kshark_ctx, lib);
> >>          };
> >>
> >> @@ -474,7 +474,7 @@ void KsPluginManager::unregisterFromList(kshark_context *kshark_ctx)
> >>
> >>          auto lamUregUser = [&kshark_ctx](const QString &plugin)
> >>          {
> >> -               const char *lib = plugin.toStdString().c_str();
> >> +               const char *lib = plugin.toLocal8Bit().data();
> >>                  kshark_unregister_plugin(kshark_ctx, lib);
> >>          };
> >
> > Doesn't the new version have the same problem with the temporary QByteArray?
> >
> > How about saving the data in a local std::string/QByteArray variable?
> >
> >                 std::string lib = plugin.toStdString();
> >                 kshark_register_plugin(kshark_ctx, lib.c_str());
> >
>
> Hi Slavi,
>
> As far I can understand toStdString() will create a fresh std::string
> and this string will has its own copy of the characters. However, this
> copy gets out-of-scope as soon as we hit the semicolon of the line.
>
> My understanding was that toLocal8Bit().data() makes no copy so the
> array will go out-of-scope only when the QString is destroyed.

I did some digging into QString's implementation. From my reading of
the code, toLocal8Bit() calls into qt_convert_to_latin1()[1] which
does allocation, copying/transform-to-latin1 and returns the
QByteArray. So it seems that toLocal8Bit() is still making a copy.

[1] https://github.com/qt/qtbase/blob/5.12/src/corelib/tools/qstring.cpp#L5275

Cheers,

-- Slavi

>
> But anyway, your solution looks cleaner and safer.
> I will send another version of the patch.
> Thanks a lot!
> Y.
>
>
> > Thanks!
> >
> >
> > --
> > Slavomir Kaslev
> >



-- 
Slavomir Kaslev

  reply	other threads:[~2019-03-11 13:44 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-03-07 15:43 [PATCH v2 0/4] Various minor modifications and fixes toward KS 1.0 Yordan Karadzhov
2019-03-07 15:43 ` [PATCH v2 1/4] kernel-shark: Specify the OpenGL interface used by KernelShark Yordan Karadzhov
2019-03-07 15:43 ` [PATCH v2 2/4] kernel-shark: Fix a bug in ksmodel_zoom Yordan Karadzhov
2019-03-13 13:51   ` Steven Rostedt
2019-03-13 13:57     ` Yordan Karadzhov
2019-03-07 15:43 ` [PATCH v2 3/4] kernel-shark: Fix Doxygen warning from sched_events Yordan Karadzhov
2019-03-07 15:43 ` [PATCH v2 4/4] kernel-shark: Fix a bug in KsPluginManager Yordan Karadzhov
2019-03-11 12:09   ` Slavomir Kaslev
2019-03-11 13:20     ` Yordan Karadzhov (VMware)
2019-03-11 13:44       ` Slavomir Kaslev [this message]
2019-03-11 17:55         ` Yordan Karadzhov (VMware)
2019-03-11 18:15           ` Steven Rostedt
2019-03-26  1:58             ` Steven Rostedt
2019-03-26 13:37               ` Yordan Karadzhov (VMware)

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=CAE0o1Nv5g6wujxY8ey2mQ72kk977s7tCWq9Hcab1zfNU3rNOVg@mail.gmail.com \
    --to=slavomir.kaslev@gmail.com \
    --cc=linux-trace-devel@vger.kernel.org \
    --cc=rostedt@goodmis.org \
    --cc=y.karadz@gmail.com \
    --cc=ykaradzhov@vmware.com \
    /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).