From 6abc9b27e545ffda5b9aaebb464f4b99af261b9d Mon Sep 17 00:00:00 2001 From: Tomi Ollila Date: Sat, 27 Apr 2013 16:22:01 +0300 Subject: [PATCH] Re: [PATCH] don't store temporary value returned from c_str() --- 44/289fda70461cc851cec536db843a6560dd97cd | 128 ++++++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100644 44/289fda70461cc851cec536db843a6560dd97cd diff --git a/44/289fda70461cc851cec536db843a6560dd97cd b/44/289fda70461cc851cec536db843a6560dd97cd new file mode 100644 index 000000000..9ab3169b2 --- /dev/null +++ b/44/289fda70461cc851cec536db843a6560dd97cd @@ -0,0 +1,128 @@ +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 EE6B2431FC0 + for ; Sat, 27 Apr 2013 06:22:06 -0700 (PDT) +X-Virus-Scanned: Debian amavisd-new at olra.theworths.org +X-Spam-Flag: NO +X-Spam-Score: 0 +X-Spam-Level: +X-Spam-Status: No, score=0 tagged_above=-999 required=5 tests=[none] + 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 yEZujMmb6P+0 for ; + Sat, 27 Apr 2013 06:22:04 -0700 (PDT) +Received: from guru.guru-group.fi (guru.guru-group.fi [46.183.73.34]) + by olra.theworths.org (Postfix) with ESMTP id 30504431FB6 + for ; Sat, 27 Apr 2013 06:22:04 -0700 (PDT) +Received: from guru.guru-group.fi (localhost [IPv6:::1]) + by guru.guru-group.fi (Postfix) with ESMTP id D0B0C1001F0; + Sat, 27 Apr 2013 16:22:01 +0300 (EEST) +From: Tomi Ollila +To: Jani Nikula , Vladimir.Marek@oracle.com, + notmuch@notmuchmail.org +Subject: Re: [PATCH] don't store temporary value returned from c_str() +In-Reply-To: <878v44qhop.fsf@nikula.org> +References: <1366405933-17223-1-git-send-email-Vladimir.Marek@oracle.com> + <878v44qhop.fsf@nikula.org> +User-Agent: Notmuch/0.15.2+75~gd7fa7c4 (http://notmuchmail.org) Emacs/24.3.1 + (x86_64-unknown-linux-gnu) +X-Face: HhBM'cA~ +MIME-Version: 1.0 +Content-Type: text/plain +Cc: 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: Sat, 27 Apr 2013 13:22:07 -0000 + +On Sat, Apr 27 2013, Jani Nikula wrote: + +> On Sat, 20 Apr 2013, Vladimir.Marek@oracle.com wrote: +>> From: Vladimir Marek +>> +>> This is causing problems when compiled by Oracle Studio. Memory pointed +>> by (const char*)term was already changed once talloc_strdup was called. +>> +>> Signed-off-by: Vladimir Marek +>> --- +>> lib/message.cc | 9 ++++----- +>> 1 files changed, 4 insertions(+), 5 deletions(-) +>> +>> diff --git a/lib/message.cc b/lib/message.cc +>> index 8720c1b..8d329d1 100644 +>> --- a/lib/message.cc +>> +++ b/lib/message.cc +>> @@ -266,18 +266,17 @@ _notmuch_message_get_term (notmuch_message_t *message, +>> const char *prefix) +>> { +>> int prefix_len = strlen (prefix); +>> - const char *term = NULL; +>> char *value; +>> +>> i.skip_to (prefix); +>> +>> - if (i != end) +>> - term = (*i).c_str (); +> +> It's okay to use the result of .c_str() as long as the string object +> stays in scope, and none of the non-const member functions are +> called. Here, I think the problem is that TermIterator's overloaded +> operator*() returns a string object within the if block's scope, and it +> goes immediately out of scope. You could check this by adding +> +> string s = *i; +> +> in function scope, and replacing (*i) with s in the if block. This might +> also be more obvious than the presented patch, but I think the patch is +> fine too. + +Thanks, now I understand. I'd like to see updated patch using Jani's +example but I also think that the current patch is fine too. + +> +> BR, +> Jani. + +Tomi + +> +> +>> + if (i == end) +>> + return NULL; +>> +>> - if (!term || strncmp (term, prefix, prefix_len)) +>> + if (strncmp ((*i).c_str(), prefix, prefix_len)) +>> return NULL; +>> +>> - value = talloc_strdup (message, term + prefix_len); +>> + value = talloc_strdup (message, (*i).c_str() + prefix_len); +>> +>> #if DEBUG_DATABASE_SANITY +>> i++; +>> -- +>> 1.7.3.2 +>> +>> _______________________________________________ +>> notmuch mailing list +>> notmuch@notmuchmail.org +>> http://notmuchmail.org/mailman/listinfo/notmuch +> _______________________________________________ +> notmuch mailing list +> notmuch@notmuchmail.org +> http://notmuchmail.org/mailman/listinfo/notmuch -- 2.26.2