From: Jani Nikula Date: Sat, 27 Apr 2013 12:30:46 +0000 (+0300) Subject: Re: [PATCH] don't store temporary value returned from c_str() X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=74d96412c62ad9a2cf73c796e98902e15ffef8a0;p=notmuch-archives.git Re: [PATCH] don't store temporary value returned from c_str() --- diff --git a/fb/b048e974b4acb5401891a2ebdd4abbfdddb222 b/fb/b048e974b4acb5401891a2ebdd4abbfdddb222 new file mode 100644 index 000000000..fe084bbc3 --- /dev/null +++ b/fb/b048e974b4acb5401891a2ebdd4abbfdddb222 @@ -0,0 +1,132 @@ +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 AAEBC431FBD + for ; Sat, 27 Apr 2013 05:30:56 -0700 (PDT) +X-Virus-Scanned: Debian amavisd-new at olra.theworths.org +X-Spam-Flag: NO +X-Spam-Score: -0.7 +X-Spam-Level: +X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5 + tests=[RCVD_IN_DNSWL_LOW=-0.7] 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 x5NihX5qfrQc for ; + Sat, 27 Apr 2013 05:30:55 -0700 (PDT) +Received: from mail-lb0-f182.google.com (mail-lb0-f182.google.com + [209.85.217.182]) (using TLSv1 with cipher RC4-SHA (128/128 bits)) + (No client certificate requested) + by olra.theworths.org (Postfix) with ESMTPS id ADF5C431FB6 + for ; Sat, 27 Apr 2013 05:30:54 -0700 (PDT) +Received: by mail-lb0-f182.google.com with SMTP id v20so4506809lbc.27 + for ; Sat, 27 Apr 2013 05:30:53 -0700 (PDT) +X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; + d=google.com; s=20120113; + h=x-received:from:to:cc:subject:in-reply-to:references:user-agent + :date:message-id:mime-version:content-type:x-gm-message-state; + bh=HjRTd7Hess3a4+bCduGoXkLCyYPSsb6pAeQBA7yOcSI=; + b=THLqAhmRPYCpX8uqblzhITI61uLklGFhaYfIpE8CQuz/wbvMAFfj0S8Ldc1DlWtopk + cd2xBUmtFR8TPPb+JbY6g9bnYJp0ho9lJNA5Pnx29Eihcp/7E8w7K4jJZKCS0EnvmX/h + /brOU/2aoKNDx/lTnc4O4i8GJlEdjN8kwWjawJ3+pINy9rQKE/2WZOogmUTVyiD7AY+j + tdcVJ9eqo98IZs1kxah82YUl6FAJFSgt3k2dksJtADGWKxPQpMz5w5DnfwZulYMRwzFo + GPWCpXgJtqMiPCQk0Uix7btLeECyOT2OHDWj+g4AgvjOZgB1Y3Q532aiRKLwnQCbi4p7 + sibA== +X-Received: by 10.112.139.226 with SMTP id rb2mr22889383lbb.12.1367065853120; + Sat, 27 Apr 2013 05:30:53 -0700 (PDT) +Received: from localhost (dsl-hkibrasgw2-58c376-211.dhcp.inet.fi. + [88.195.118.211]) + by mx.google.com with ESMTPSA id x9sm6088526lbi.15.2013.04.27.05.30.51 + for + (version=TLSv1.2 cipher=RC4-SHA bits=128/128); + Sat, 27 Apr 2013 05:30:52 -0700 (PDT) +From: Jani Nikula +To: Vladimir.Marek@oracle.com, notmuch@notmuchmail.org +Subject: Re: [PATCH] don't store temporary value returned from c_str() +In-Reply-To: <1366405933-17223-1-git-send-email-Vladimir.Marek@oracle.com> +References: <1366405933-17223-1-git-send-email-Vladimir.Marek@oracle.com> +User-Agent: Notmuch/0.15.2+70~g2eeb96a (http://notmuchmail.org) Emacs/24.2.1 + (x86_64-pc-linux-gnu) +Date: Sat, 27 Apr 2013 15:30:46 +0300 +Message-ID: <878v44qhop.fsf@nikula.org> +MIME-Version: 1.0 +Content-Type: text/plain +X-Gm-Message-State: + ALoCoQlBQJPPF/vUlE025ejIvSVzDwUhAPdFGI2QeE2mqVfYhnnZjBJf9zOKGtuKu+0SPJq27wGH +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 12:30:57 -0000 + +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. + +BR, +Jani. + + +> + 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