From 2cff8b89bf2fc0509feaa10a0270006a3d0d7097 Mon Sep 17 00:00:00 2001 From: Vladimir Marek Date: Tue, 30 Apr 2013 10:48:52 +0200 Subject: [PATCH] Re: [PATCH] don't store temporary value returned from c_str() --- 9e/df1bcc7b7d5861e2287f2d559ee0c70db1bb67 | 181 ++++++++++++++++++++++ 1 file changed, 181 insertions(+) create mode 100644 9e/df1bcc7b7d5861e2287f2d559ee0c70db1bb67 diff --git a/9e/df1bcc7b7d5861e2287f2d559ee0c70db1bb67 b/9e/df1bcc7b7d5861e2287f2d559ee0c70db1bb67 new file mode 100644 index 000000000..42370b212 --- /dev/null +++ b/9e/df1bcc7b7d5861e2287f2d559ee0c70db1bb67 @@ -0,0 +1,181 @@ +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 B11ED431FBC + for ; Tue, 30 Apr 2013 01:51:32 -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 1ahaPOuZ2KbI for ; + Tue, 30 Apr 2013 01:51:26 -0700 (PDT) +Received: from aserp1040.oracle.com (aserp1040.oracle.com [141.146.126.69]) + (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) + (No client certificate requested) + by olra.theworths.org (Postfix) with ESMTPS id DD646431FAF + for ; Tue, 30 Apr 2013 01:51:26 -0700 (PDT) +Received: from acsinet22.oracle.com (acsinet22.oracle.com [141.146.126.238]) + by aserp1040.oracle.com (Sentrion-MTA-4.3.1/Sentrion-MTA-4.3.1) with + ESMTP id r3U8pAGp004285 + (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); + Tue, 30 Apr 2013 08:51:11 GMT +Received: from aserz7022.oracle.com (aserz7022.oracle.com [141.146.126.231]) + by acsinet22.oracle.com (8.14.4+Sun/8.14.4) with ESMTP id + r3U8p65m016017 + (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=FAIL); + Tue, 30 Apr 2013 08:51:07 GMT +Received: from abhmt119.oracle.com (abhmt119.oracle.com [141.146.116.71]) + by aserz7022.oracle.com (8.14.4+Sun/8.14.4) with ESMTP id + r3U8p6U3016009; Tue, 30 Apr 2013 08:51:06 GMT +Received: from pub.cz.oracle.com (/10.163.101.122) + by default (Oracle Beehive Gateway v4.0) + with ESMTP ; Tue, 30 Apr 2013 01:51:06 -0700 +Date: Tue, 30 Apr 2013 10:48:52 +0200 +From: Vladimir Marek +To: Kim Minh Kaplan +Subject: Re: [PATCH] don't store temporary value returned from c_str() +Message-ID: <20130430084852.GA19599@pub.cz.oracle.com> +Mail-Followup-To: Kim Minh Kaplan , + Tomi Ollila , notmuch@notmuchmail.org, + Vladimir Marek +References: <1366405933-17223-1-git-send-email-Vladimir.Marek@oracle.com> + + <20130427101134.GI10394@pub.cz.oracle.com> + <8761z4h7ih.fsf@kaki.tech.prive.nic.fr> +MIME-Version: 1.0 +Content-Type: multipart/mixed; boundary="J/dobhs11T7y2rNN" +Content-Disposition: inline +Content-Transfer-Encoding: 8bit +In-Reply-To: <8761z4h7ih.fsf@kaki.tech.prive.nic.fr> +User-Agent: Mutt/1.5.21 (2010-09-15) +X-Source-IP: acsinet22.oracle.com [141.146.126.238] +Cc: Tomi Ollila , notmuch@notmuchmail.org, + Vladimir Marek +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: Tue, 30 Apr 2013 08:51:32 -0000 + + +--J/dobhs11T7y2rNN +Content-Type: text/plain; charset=utf-8 +Content-Disposition: inline +Content-Transfer-Encoding: 8bit + +Hi, + +Thank you, I found it eventually too. But I wrote little test program +(attached) which confused me. I haven't had much time to take a look +into it since weekend. + +The idea is to have temporary object where I can detect whether +destructor was called. + +I thought that + +printf ("%s\n", s.c_str()); +will print "test" + +and + +x=s.c_str(); +printf ("%s\n", x); + +will print "destroyed" + +On my machine both prints "destroyed". + + +I still believe my fix is correct, but I'm not at the position to be +able to defend it at the moment :) + +Thank you +-- + Vlad + +> The january 2012 working draft: +> http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3337.pdf +> +> 12.2 Temporary objects [class.temporary] +> +> 1 Temporaries of class type are created in various contexts: binding a +> reference to a prvalue (8.5.3), returning a prvalue (6.6.3) […] +> +> 3 When an implementation introduces a temporary object of a class that +> has a non-trivial constructor (12.1, 12.8), it shall ensure that a +> constructor is called for the temporary object. Similarly, the +> destructor shall be called for a temporary with a non-trivial destructor +> (12.4). Temporary objects are destroyed as the last step in evaluating +> the full-expression (1.9) that (lexically) contains the point where they +> were created. + +--J/dobhs11T7y2rNN +Content-Type: text/plain; charset=utf-8 +Content-Disposition: attachment; filename="a.cc" + +#include +#include +#include + +using namespace std; + +class array { + vector impl; + + public: + + array(int size):impl(size) { } + array(array &in):impl(in.impl) { } + + char* + operator[](size_t i) { + return &impl[i]; + }; + + ~array() { + strcpy(&impl[0], "destroyed"); + } +}; + +class str { + array tmp; + + public: + + str(const char *input):tmp(100) { + strcpy(tmp[0], input); + }; + + const char* c_str() { + return (array(tmp))[0]; + }; +}; + + +int +main (int argc, char **argv) +{ + str s("test"); + const char *x; + printf ("%s\n", s.c_str()); + x=s.c_str(); + printf ("%s\n", x); + return 0; +} + +--J/dobhs11T7y2rNN-- -- 2.26.2