From e32d5724d14a1f903fd26cf594ea8528e64cddf8 Mon Sep 17 00:00:00 2001 From: Vladimir Marek Date: Sat, 27 Apr 2013 17:59:22 +0200 Subject: [PATCH] Re: [PATCH] don't store temporary value returned from c_str() --- 7b/77097af2ac286d7e167826e742d6ab62927113 | 104 ++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 7b/77097af2ac286d7e167826e742d6ab62927113 diff --git a/7b/77097af2ac286d7e167826e742d6ab62927113 b/7b/77097af2ac286d7e167826e742d6ab62927113 new file mode 100644 index 000000000..3b7327615 --- /dev/null +++ b/7b/77097af2ac286d7e167826e742d6ab62927113 @@ -0,0 +1,104 @@ +Return-Path: +X-Original-To: notmuch@notmuchmail.org +Delivered-To: notmuch@notmuchmail.org +Received: from localhost (localhost [127.0.0.1]) + by olra.theworths.org (Postfix) with ESMTP id 778A4431FB6 + for ; Sat, 27 Apr 2013 08:59:35 -0700 (PDT) +X-Virus-Scanned: Debian amavisd-new at olra.theworths.org +X-Spam-Flag: NO +X-Spam-Score: -2.299 +X-Spam-Level: +X-Spam-Status: No, score=-2.299 tagged_above=-999 required=5 + tests=[RCVD_IN_DNSWL_MED=-2.3, UNPARSEABLE_RELAY=0.001] + autolearn=disabled +Received: from olra.theworths.org ([127.0.0.1]) + by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024) + with ESMTP id I7OGM8QcRFdN for ; + Sat, 27 Apr 2013 08:59:35 -0700 (PDT) +Received: from userp1040.oracle.com (userp1040.oracle.com [156.151.31.81]) + (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) + (No client certificate requested) + by olra.theworths.org (Postfix) with ESMTPS id 1E18A431FAE + for ; Sat, 27 Apr 2013 08:59:35 -0700 (PDT) +Received: from ucsinet22.oracle.com (ucsinet22.oracle.com [156.151.31.94]) + by userp1040.oracle.com (Sentrion-MTA-4.3.1/Sentrion-MTA-4.3.1) with + ESMTP id r3RFxWMJ030917 + (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) + for ; Sat, 27 Apr 2013 15:59:33 GMT +Received: from userz7021.oracle.com (userz7021.oracle.com [156.151.31.85]) + by ucsinet22.oracle.com (8.14.4+Sun/8.14.4) with ESMTP id + r3RFxW10015168 + (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=FAIL) + for ; Sat, 27 Apr 2013 15:59:32 GMT +Received: from ubhmt103.oracle.com (ubhmt103.oracle.com [156.151.24.8]) + by userz7021.oracle.com (8.14.4+Sun/8.14.4) with ESMTP id + r3RFxWDg002993 + for ; Sat, 27 Apr 2013 15:59:32 GMT +Received: from vi64-x3-2e-prg06.cz.oracle.com (/10.163.102.127) + by default (Oracle Beehive Gateway v4.0) + with ESMTP ; Sat, 27 Apr 2013 08:59:31 -0700 +Date: Sat, 27 Apr 2013 17:59:22 +0200 +From: Vladimir Marek +To: notmuch@notmuchmail.org +Subject: Re: [PATCH] don't store temporary value returned from c_str() +Message-ID: <20130427155922.GA17459@vi64-x3-2e-prg06.cz.oracle.com> +References: <1366405933-17223-1-git-send-email-Vladimir.Marek@oracle.com> + + <20130427101134.GI10394@pub.cz.oracle.com> + <8738ucw5oi.fsf@zancas.localnet> +MIME-Version: 1.0 +Content-Type: text/plain; charset=utf-8 +Content-Disposition: inline +In-Reply-To: <8738ucw5oi.fsf@zancas.localnet> +User-Agent: Mutt/ (2012-12-30) +X-Source-IP: ucsinet22.oracle.com [156.151.31.94] +X-BeenThere: notmuch@notmuchmail.org +X-Mailman-Version: 2.1.13 +Precedence: list +List-Id: "Use and development of the notmuch mail system." + +List-Unsubscribe: , + +List-Archive: +List-Post: +List-Help: +List-Subscribe: , + +X-List-Received-Date: Sat, 27 Apr 2013 15:59:35 -0000 + +> > const char* +> > string::c_str(void) { +> > char buf[100]; +> > +> > strcpy (buf, this->internal_representation); +> > return buf; +> > } +> +> Isn't that undefined behavior, returning a pointer to a (non-static) +> local variable? + +Right, I was trying to bring up an example and this one is not very +good. Maybe something like that: + + +const char* +c_str() { + vector X(my_string); + return &X[0]; +}; + +char buf[]; +strcpy(buf, c_str()); + +X destructor is called after strcpy is done. + +char buf[]; +char *tmp=c_str(); +strcpy(buf, tmp); + +X destructor is called before strcpy. + +At least this is how I understand it. + +-- + Vlad -- 2.26.2