Warning for initializing a reference with literal

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Warning for initializing a reference with literal

Tom Stellard via cfe-dev
Hi,

would it be possible to emit a warning for the code below? I could imagine this case could be detected easily in the frontend / middleend.

ASan finds this with detect_stack_use_after_return enabled, but this option is sometimes not feasible due to high memory usage.

struct RefStorer
{
  RefStorer(const bool& ref): m_ref(ref) {}

  const bool& m_ref;
};

RefStorer getRefStorer()
{
  return RefStorer(false); // false passed as reference
}

int main()
{
  RefStorer r = getRefStorer(); // m_ref now dangling
  return 0;
}

Thanks and Best regards,
Martin
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Warning for initializing a reference with literal

Tom Stellard via cfe-dev

Hello!

It's not enough to just warn about passing literal. Imagine the user fixes the warning like this:

RefStorer getRefStorer()
{
  bool b = false;
  return RefStorer(b);
}

If warning goes away with this solution, I guess many users will probably do that.

We need path sensitive analysis.

Writing a warning for this bug sounds very good, if we add a proper path sensitive check.

Best regards,
Daniel Marjamäki

..................................................................................................................
Daniel Marjamäki Senior Engineer
Evidente ES East AB  Warfvinges väg 34  SE-112 51 Stockholm  Sweden

Mobile:                 +46 (0)709 12 42 62
E-mail:                 [hidden email]

www.evidente.se

________________________________________
From: cfe-dev [[hidden email]] on behalf of Richtarsky, Martin via cfe-dev [[hidden email]]
Sent: 21 April 2017 10:38
To: [hidden email]
Subject: [cfe-dev] Warning for initializing a reference with literal

Hi,

would it be possible to emit a warning for the code below? I could imagine this case could be detected easily in the frontend / middleend.

ASan finds this with detect_stack_use_after_return enabled, but this option is sometimes not feasible due to high memory usage.

struct RefStorer
{
  RefStorer(const bool& ref): m_ref(ref) {}

  const bool& m_ref;
};

RefStorer getRefStorer()
{
  return RefStorer(false); // false passed as reference
}

int main()
{
  RefStorer r = getRefStorer(); // m_ref now dangling
  return 0;
}

Thanks and Best regards,
Martin
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Warning for initializing a reference with literal

Tom Stellard via cfe-dev
> We need path sensitive analysis.
I agree, those more complicated cases should be detected as well (including a helpful warning text that makes it clear why this is bad)

Should I file an (enhancement) bug?

Best regards,
Martin

-----Original Message-----
From: Daniel Marjamäki [mailto:[hidden email]]
Sent: Freitag, 21. April 2017 12:08
To: Richtarsky, Martin <[hidden email]>; [hidden email]
Subject: RE: Warning for initializing a reference with literal


Hello!

It's not enough to just warn about passing literal. Imagine the user fixes the warning like this:

RefStorer getRefStorer()
{
  bool b = false;
  return RefStorer(b);
}

If warning goes away with this solution, I guess many users will probably do that.

We need path sensitive analysis.

Writing a warning for this bug sounds very good, if we add a proper path sensitive check.

Best regards,
Daniel Marjamäki

..................................................................................................................
Daniel Marjamäki Senior Engineer
Evidente ES East AB  Warfvinges väg 34  SE-112 51 Stockholm  Sweden

Mobile:                 +46 (0)709 12 42 62
E-mail:                 [hidden email]

www.evidente.se

________________________________________
From: cfe-dev [[hidden email]] on behalf of Richtarsky, Martin via cfe-dev [[hidden email]]
Sent: 21 April 2017 10:38
To: [hidden email]
Subject: [cfe-dev] Warning for initializing a reference with literal

Hi,

would it be possible to emit a warning for the code below? I could imagine this case could be detected easily in the frontend / middleend.

ASan finds this with detect_stack_use_after_return enabled, but this option is sometimes not feasible due to high memory usage.

struct RefStorer
{
  RefStorer(const bool& ref): m_ref(ref) {}

  const bool& m_ref;
};

RefStorer getRefStorer()
{
  return RefStorer(false); // false passed as reference
}

int main()
{
  RefStorer r = getRefStorer(); // m_ref now dangling
  return 0;
}

Thanks and Best regards,
Martin
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Warning for initializing a reference with literal

Tom Stellard via cfe-dev

> Should I file an (enhancement) bug?

yes please.

Best regards,
Daniel Marjamäki

..................................................................................................................
Daniel Marjamäki Senior Engineer
Evidente ES East AB  Warfvinges väg 34  SE-112 51 Stockholm  Sweden

Mobile:                 +46 (0)709 12 42 62
E-mail:                 [hidden email]

www.evidente.se

________________________________________
From: Richtarsky, Martin [[hidden email]]
Sent: 24 April 2017 09:32
To: Daniel Marjamäki; [hidden email]
Subject: RE: Warning for initializing a reference with literal

> We need path sensitive analysis.
I agree, those more complicated cases should be detected as well (including a helpful warning text that makes it clear why this is bad)

Should I file an (enhancement) bug?

Best regards,
Martin

-----Original Message-----
From: Daniel Marjamäki [mailto:[hidden email]]
Sent: Freitag, 21. April 2017 12:08
To: Richtarsky, Martin <[hidden email]>; [hidden email]
Subject: RE: Warning for initializing a reference with literal


Hello!

It's not enough to just warn about passing literal. Imagine the user fixes the warning like this:

RefStorer getRefStorer()
{
  bool b = false;
  return RefStorer(b);
}

If warning goes away with this solution, I guess many users will probably do that.

We need path sensitive analysis.

Writing a warning for this bug sounds very good, if we add a proper path sensitive check.

Best regards,
Daniel Marjamäki

..................................................................................................................
Daniel Marjamäki Senior Engineer
Evidente ES East AB  Warfvinges väg 34  SE-112 51 Stockholm  Sweden

Mobile:                 +46 (0)709 12 42 62
E-mail:                 [hidden email]

www.evidente.se

________________________________________
From: cfe-dev [[hidden email]] on behalf of Richtarsky, Martin via cfe-dev [[hidden email]]
Sent: 21 April 2017 10:38
To: [hidden email]
Subject: [cfe-dev] Warning for initializing a reference with literal

Hi,

would it be possible to emit a warning for the code below? I could imagine this case could be detected easily in the frontend / middleend.

ASan finds this with detect_stack_use_after_return enabled, but this option is sometimes not feasible due to high memory usage.

struct RefStorer
{
  RefStorer(const bool& ref): m_ref(ref) {}

  const bool& m_ref;
};

RefStorer getRefStorer()
{
  return RefStorer(false); // false passed as reference
}

int main()
{
  RefStorer r = getRefStorer(); // m_ref now dangling
  return 0;
}

Thanks and Best regards,
Martin
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Warning for initializing a reference with literal

Tom Stellard via cfe-dev
Bug 32800 - Emit warning for initializing a reference with literal/temporary
https://bugs.llvm.org//show_bug.cgi?id=32800

Best regards,
Martin

-----Original Message-----
From: Daniel Marjamäki [mailto:[hidden email]]
Sent: Montag, 24. April 2017 11:32
To: Richtarsky, Martin <[hidden email]>; [hidden email]
Subject: RE: Warning for initializing a reference with literal


> Should I file an (enhancement) bug?

yes please.

Best regards,
Daniel Marjamäki

..................................................................................................................
Daniel Marjamäki Senior Engineer
Evidente ES East AB  Warfvinges väg 34  SE-112 51 Stockholm  Sweden

Mobile:                 +46 (0)709 12 42 62
E-mail:                 [hidden email]

www.evidente.se

________________________________________
From: Richtarsky, Martin [[hidden email]]
Sent: 24 April 2017 09:32
To: Daniel Marjamäki; [hidden email]
Subject: RE: Warning for initializing a reference with literal

> We need path sensitive analysis.
I agree, those more complicated cases should be detected as well (including a helpful warning text that makes it clear why this is bad)

Should I file an (enhancement) bug?

Best regards,
Martin

-----Original Message-----
From: Daniel Marjamäki [mailto:[hidden email]]
Sent: Freitag, 21. April 2017 12:08
To: Richtarsky, Martin <[hidden email]>; [hidden email]
Subject: RE: Warning for initializing a reference with literal


Hello!

It's not enough to just warn about passing literal. Imagine the user fixes the warning like this:

RefStorer getRefStorer()
{
  bool b = false;
  return RefStorer(b);
}

If warning goes away with this solution, I guess many users will probably do that.

We need path sensitive analysis.

Writing a warning for this bug sounds very good, if we add a proper path sensitive check.

Best regards,
Daniel Marjamäki

..................................................................................................................
Daniel Marjamäki Senior Engineer
Evidente ES East AB  Warfvinges väg 34  SE-112 51 Stockholm  Sweden

Mobile:                 +46 (0)709 12 42 62
E-mail:                 [hidden email]

www.evidente.se

________________________________________
From: cfe-dev [[hidden email]] on behalf of Richtarsky, Martin via cfe-dev [[hidden email]]
Sent: 21 April 2017 10:38
To: [hidden email]
Subject: [cfe-dev] Warning for initializing a reference with literal

Hi,

would it be possible to emit a warning for the code below? I could imagine this case could be detected easily in the frontend / middleend.

ASan finds this with detect_stack_use_after_return enabled, but this option is sometimes not feasible due to high memory usage.

struct RefStorer
{
  RefStorer(const bool& ref): m_ref(ref) {}

  const bool& m_ref;
};

RefStorer getRefStorer()
{
  return RefStorer(false); // false passed as reference
}

int main()
{
  RefStorer r = getRefStorer(); // m_ref now dangling
  return 0;
}

Thanks and Best regards,
Martin
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Loading...