From owner-svn-src-head@freebsd.org Thu Mar 23 19:35:37 2017 Return-Path: Delivered-To: svn-src-head@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 2F514CBB866 for ; Thu, 23 Mar 2017 19:35:37 +0000 (UTC) (envelope-from ian@freebsd.org) Received: from pmta2.delivery6.ore.mailhop.org (pmta2.delivery6.ore.mailhop.org [54.200.129.228]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 13F141F70 for ; Thu, 23 Mar 2017 19:35:36 +0000 (UTC) (envelope-from ian@freebsd.org) X-MHO-User: b9245c40-0fff-11e7-8c45-c35e37f62db1 X-Report-Abuse-To: https://support.duocircle.com/support/solutions/articles/5000540958-duocircle-standard-smtp-abuse-information X-Originating-IP: 73.78.92.27 X-Mail-Handler: DuoCircle Outbound SMTP Received: from ilsoft.org (unknown [73.78.92.27]) by outbound2.ore.mailhop.org (Halon) with ESMTPSA id b9245c40-0fff-11e7-8c45-c35e37f62db1; Thu, 23 Mar 2017 19:34:26 +0000 (UTC) Received: from rev (rev [172.22.42.240]) by ilsoft.org (8.15.2/8.15.2) with ESMTP id v2NJYVAd004352; Thu, 23 Mar 2017 13:34:31 -0600 (MDT) (envelope-from ian@freebsd.org) Message-ID: <1490297671.58015.102.camel@freebsd.org> Subject: Re: svn commit: r315773 - head/sbin/devd From: Ian Lepore To: Roman Divacky , Warner Losh Cc: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Date: Thu, 23 Mar 2017 13:34:31 -0600 In-Reply-To: <20170323185853.GA87388@vlakno.cz> References: <201703230236.v2N2apvX051799@repo.freebsd.org> <20170323185853.GA87388@vlakno.cz> Content-Type: text/plain; charset="ISO-8859-1" X-Mailer: Evolution 3.18.5.1 FreeBSD GNOME Team Port Mime-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 23 Mar 2017 19:35:37 -0000 On Thu, 2017-03-23 at 19:58 +0100, Roman Divacky wrote: > On Thu, Mar 23, 2017 at 02:36:51AM +0000, Warner Losh wrote: > > > > Author: imp > > Date: Thu Mar 23 02:36:51 2017 > > New Revision: 315773 > > URL: https://svnweb.freebsd.org/changeset/base/315773 > > > > Log: > >   Implement quote escaping. String values may now contain " if you > >   it is preceded by \. > >    > >   foo="I \"like\" C++" > >    > >   gives the value 'I "like" C++' to the variable 'foo'. If a > > character > >   other than " follows the \, both the \ and that character are > > passed > >   through. > >    > >   Differential Revision: https://reviews.freebsd.org/D6286 > >   Sponsored by: Netflix > > > > Modified: > >   head/sbin/devd/devd.cc > >   head/sbin/devd/devd.hh > > > > Modified: head/sbin/devd/devd.cc > > =================================================================== > > =========== > > --- head/sbin/devd/devd.cc Thu Mar 23 02:33:27 2017 ( > > r315772) > > +++ head/sbin/devd/devd.cc Thu Mar 23 02:36:51 2017 ( > > r315773) > > @@ -411,6 +411,32 @@ var_list::is_set(const string &var) cons > >   return (_vars.find(var) != _vars.end()); > >  } > >   > > +/** fix_value > > + * > > + * Removes quoted characters that have made it this far. \" are > > + * converted to ". For all other characters, both \ and following > > + * character. So the string 'fre\:\"' is translated to 'fred\:"'. > > + */ > > +const std::string & > > +var_list::fix_value(const std::string &val) const > > +{ > > + char *tmp, *dst; > > + const char *src; > > + std::string *rv; > > + > > + dst = tmp = new char[val.length()]; > > + src = val.c_str(); > > + while (*src) { > > + if (*src == '\\' && src[1] == '"') > > + src++; > > + else > > + *dst++ = *src++; > > + } > > + rv = new string(tmp); > > + delete tmp; > > + return *rv; > > +} > Can the temporary char[] be stack allocated? Also, when returning a > reference to > a heap allocated string who is responsible for freeing it? Perhaps > you can just > return std::string and let the compiler optimize it? > > Roman > Returning a reference to a new'd string is not good. IMO, the implementation of the function should take into account the idea that embedded escaped quotes are rare.  The function should be optimized for readability and the common case of simply copying the string unmodified, something like: std::string var_list::fix_value(const std::string &val) const { std::string rv(val); std::string::size_type pos(0); while ((pos = rv.find("\\\"", pos)) != rv.npos) { rv.erase(pos, 1); } return (rv); } -- Ian