Re: [PATCH] virnetdevopenvswitch: Don't call strlen() twice on the same string

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

 



n Wed, 2016-01-27 at 10:49 +0100, Jiri Denemark wrote:
> On Wed, Jan 27, 2016 at 10:41:15 +0100, Andrea Bolognani wrote:
> > Commit 871e10f fixed a memory corruption error, but called strlen()
> > twice on the same string to do so. Even though the compiler is
> > probably smart enough to optimize the second call away, having a
> > single invocation makes the code slightly cleaner.
> > 
> > Suggested-by: Michal Privoznik <mprivozn@xxxxxxxxxx>
> > ---
> > 
> > How about this? :)
> > 
> >  src/util/virnetdevopenvswitch.c | 6 ++++--
> >  1 file changed, 4 insertions(+), 2 deletions(-)
> > 
> > diff --git a/src/util/virnetdevopenvswitch.c b/src/util/virnetdevopenvswitch.c
> > index db01dcf..9283bbb 100644
> > --- a/src/util/virnetdevopenvswitch.c
> > +++ b/src/util/virnetdevopenvswitch.c
> > @@ -207,6 +207,7 @@ int virNetDevOpenvswitchRemovePort(const char *brname ATTRIBUTE_UNUSED, const ch
> >  int virNetDevOpenvswitchGetMigrateData(char **migrate, const char *ifname)
> >  {
> >      virCommandPtr cmd = NULL;
> > +    size_t len;
> >      int ret = -1;
> >  
> >      cmd = virCommandNewArgList(OVSVSCTL, "--timeout=5", "--if-exists", "get", "Interface",
> > @@ -223,8 +224,9 @@ int virNetDevOpenvswitchGetMigrateData(char **migrate, const char *ifname)
> >      }
> >  
> >      /* Wipeout the newline, if it exists */
> > -    if (strlen(*migrate) > 0)
> > -        (*migrate)[strlen(*migrate) - 1] = '\0';
> 
> Or just
> 
>     if (**migrate)
>         (*migrate)[strlen(*migrate) - 1] = '\0';
> 
> (or similar check for the first character in *migrate) since we only
> need to check if it's empty or not :-)

Yeah, that would work just as nicely, and would even save us the
call to strlen() altogether when the string is empty.

However, I'd argue that it makes the code a tiny bit more opaque
rather than a tiny bit cleaner, so I'd still go with the version
I posted if that's okay with you :)

Cheers.

-- 
Andrea Bolognani
Software Engineer - Virtualization Team

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]