[cfe-dev] is there any way to detect temporary referencing with clang?

Dennis Luehring dl.soluz at gmx.net
Tue Jul 15 23:54:46 PDT 2014


or is that already covered by 
https://code.google.com/p/address-sanitizer/issues/detail?id=73

example:

#include <cstdint>
#include <cstdio>

class user_t
{
public:
   user_t(const int64_t& p_a):
     m_a(p_a)
   {
   }

   const int64_t& m_a; // someone added evil & while refactoring

   int64_t use_it() const
   {
     return m_a;
   }
};

#define EVIL_TEMPORARY

//example result:
//EVIL_TEMPORARY  Mode
//     X          Release  --> a: 8, user_a: 75769734271860736 FAIL
//     X          Debug    --> a: 8, user_a: 7 FAIL
//     -          Release  --> a: 8, user_a: 8 OK
//     -          Debug    --> a: 8, user_a: 8 OK

int main(int argc, char** argv)
{
   const int64_t value = argc+6;
   int64_t a = value;

#if defined(EVIL_TEMPORARY)
   const user_t user(a++); // the temporary result of a++ is referenced 
by member m_a
#else
   const user_t user(a);
   ++a;
#endif
   const int64_t user_a = user.use_it();
   const bool ok = user_a == a;
   printf("a: %I64d, user_a: %I64d %s\n", a, user_a, ok ? "OK":"FAIL");
   return ok;
}



More information about the cfe-dev mailing list