[cfe-dev] Warning for initializing a reference with literal

Daniel Marjamäki via cfe-dev cfe-dev at lists.llvm.org
Fri Apr 21 03:07:31 PDT 2017


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:                 Daniel.Marjamaki at evidente.se

www.evidente.se

________________________________________
From: cfe-dev [cfe-dev-bounces at lists.llvm.org] on behalf of Richtarsky, Martin via cfe-dev [cfe-dev at lists.llvm.org]
Sent: 21 April 2017 10:38
To: cfe-dev at lists.llvm.org
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
cfe-dev at lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev



More information about the cfe-dev mailing list