public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: Marc Glisse <marc.glisse@inria.fr>
To: David Malcolm <dmalcolm@redhat.com>
Cc: Eric Batchelor <eric@bookmanager.com>, gcc@gcc.gnu.org
Subject: Re: Expected warning maybe-uninitialized does not appear using g++13.2.0?
Date: Fri, 22 Dec 2023 05:45:07 +0100 (CET)	[thread overview]
Message-ID: <c36a1735-fbe1-9211-01a8-2377ef7b6fe3@inria.fr> (raw)
In-Reply-To: <8c605c042acb7ea3de38fdb7f953969a1560fd19.camel@redhat.com>

On Thu, 21 Dec 2023, David Malcolm via Gcc wrote:

> On Wed, 2023-12-20 at 11:16 -0800, Eric Batchelor wrote:
>> Hello, I unintentionally stumbled upon some strange behaviour that
>> occurred due to a typo.
>> I reproduced the behaviour where an object (std::string in my case)
>> can
>> be passed to a function by reference, uninitialized, WITHOUT a
>> compiler
>> warning.
>> Changing the code to pass the object by value DOES emit the warning.
>> I don't think the compiled code is incorrect, it segfaults presumably
>> due to uninitialized members.
>> I understand there may seldom be a reason to use uninitialized
>> objects,
>> so "don't do that," but as I said this was unintentional and it seems
>> that it should have generated a warning, which have saved some
>> head-scratching.
>>
>> Code to reproduce:
>>
>> #include <string>
>> std::string f(std::string &s) {
>>    s.append("x");
>>    return s;
>> }
>> int main() {
>>    std::string a = f(a);
>> }
>>
>> Compile and run (no warning):
>>
>> $ g++ -o uninit_obj uninit_obj.cpp -std=c++23 -Wall -Wpedantic -
>> Wextra
>> && ./uninit_obj
>> Segmentation fault (core dumped)
>>
>> No difference whether using -O0 (or 1 2 3)
>
> As I understand it, -Wmaybe-uninitialized is purely intraprocedural
> i.e. it works within each individual function, without considering the
> interactions *between* functions.

If you compile

#include <string>
static std::string f(std::string &s) {
  s.append("x");
  return s;
}
void g() {
  std::string a = f(a);
}

with -O3, by the time we get to the uninit pass, function g starts with

void g ()
{
   size_type __dnew;
   struct string a;
[...]
   <bb 2> [local count: 1073741824]:
   _26 = a._M_string_length;
   if (_26 == 4611686018427387903)

which should not require any interprocedural logic.

-- 
Marc Glisse

      reply	other threads:[~2023-12-22  4:45 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-20 19:16 Eric Batchelor
2023-12-22  3:43 ` David Malcolm
2023-12-22  4:45   ` Marc Glisse [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=c36a1735-fbe1-9211-01a8-2377ef7b6fe3@inria.fr \
    --to=marc.glisse@inria.fr \
    --cc=dmalcolm@redhat.com \
    --cc=eric@bookmanager.com \
    --cc=gcc@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).