<div dir="ltr">Your example doesn't compile.  However, I suspect what you are seeing from gcc is due to it performing analysis from the backend after inlining has taken place.  As a policy, Clang won't emit warnings from the backend since it will cause different diagnostics depending on the optimization levels and other options specified.<div><br></div><div><div class="gmail_extra"><div class="gmail_quote">On Fri, Jun 24, 2016 at 2:16 PM, Riyaz Puthiyapurayil <span dir="ltr"><<a href="mailto:Riyaz.Puthiyapurayil@synopsys.com" target="_blank">Riyaz.Puthiyapurayil@synopsys.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">





<div lang="EN-US" link="blue" vlink="purple">
<div>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif">Thanks. UndefinedBehaviorSanitizer looks interesting. Will take a look.<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif">I had replied to my original email with a re-code of FooLI and FooBI which will expose this issue with gcc (and with a debug build).
<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif">Also note that my request for warning was for the specific case where two constructors exist in FooLI, one accepting a pointer and another accepting a reference, and the user
 of the class chose the reference version by dereferencing a pointer when he/she should have actually used the other version (because the pointer can sometimes be null). This is not an expensive check for clang. OTOH, I admit that a warning in this case can
 cause many false positives if the intent of the user was to avoid the null check because (s)he knows the pointer cannot be null (especially my recoded version of the classes no longer does a null check in the reference version even with gcc which wasn’t optimizing
 away the redundant nullcheck). But then I cannot think of any reasonable recoding to silence a false positive when it occurs. So I will withdraw my request for a warning.<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif">My recoding to expose this issue with gcc is as follows:<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif"><u></u> <u></u></span></p>
<div style="border-top:solid #1f497d 1.0pt;border-left:none;border-bottom:solid #1f497d 1.0pt;border-right:none;padding:1.0pt 0in 1.0pt 0in;background:#f2f2f2"><span class="">
<p style="background:#f2f2f2">class FooBI {<u></u><u></u></p>
<p>public:<u></u><u></u></p>
<p>    :<u></u><u></u></p>
<p>    FooBI(const FooB* foo_) :<u></u><u></u></p>
</span><p>        foo(foo_ && foo_->bar ? foo_ : NULL) {}   // [1] has a null check<u></u><u></u></p>
<p>   FooBI(const FooB& foo_) :<u></u><u></u></p>
<p>        foo(foo.bar ? &foo_ : NULL) {}   // There is no null-check here
<u></u><u></u></p>
<p>                                         // as this expects the reference<u></u><u></u></p>
<p>                                         // to be non-NULL (gcc and unoptimized
<u></u><u></u></p>
<p>                                         // code will now generate crash for<u></u><u></u></p>
<p>                                         // a null reference)<u></u><u></u></p><span class="">
<p>    :<u></u><u></u></p>
<p>};<u></u><u></u></p>
<p>template<class T>class FooLI : public FooBI {<u></u><u></u></p>
<p>public:<u></u><u></u></p>
<p>    :    <u></u><u></u></p>
<p>    FooLI(const FooL<T>* foo) :<u></u><u></u></p>
<p>        FooBI(foo)<u></u><u></u></p>
<p>    {}<u></u><u></u></p>
<p>    FooLI(const Foo<T>& foo) :<u></u><u></u></p>
</span><p>        FooBI(foo)     // changed from FooBI(&foo)<u></u><u></u></p>
<p>    {}<u></u><u></u></p>
<p>    :<u></u><u></u></p>
<p>};<u></u><u></u></p>
</div>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif"><u></u> <u></u></span></p>
<p class="MsoNormal" style="text-align:justify"><i><span style="font-size:11.0pt;font-family:"Viner Hand ITC";color:black">-Riyaz<u></u><u></u></span></i></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif"><u></u> <u></u></span></p>
<div style="border:none;border-left:solid blue 1.5pt;padding:0in 0in 0in 4.0pt">
<div>
<div style="border:none;border-top:solid #e1e1e1 1.0pt;padding:3.0pt 0in 0in 0in">
<p class="MsoNormal"><b><span style="font-size:11.0pt;font-family:"Calibri",sans-serif">From:</span></b><span style="font-size:11.0pt;font-family:"Calibri",sans-serif"> Richard Trieu [mailto:<a href="mailto:rtrieu@google.com" target="_blank">rtrieu@google.com</a>]
<br>
<b>Sent:</b> Thursday, June 23, 2016 2:47 PM<br>
<b>To:</b> Riyaz Puthiyapurayil <<a href="mailto:Riyaz.Puthiyapurayil@synopsys.com" target="_blank">Riyaz.Puthiyapurayil@synopsys.com</a>><br>
<b>Cc:</b> <a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a><br>
<b>Subject:</b> Re: [cfe-dev] Can clang generate a warning for this?<u></u><u></u></span></p>
</div>
</div><div><div class="h5">
<p class="MsoNormal"><u></u> <u></u></p>
<div>
<p class="MsoNormal">There's two suggestions for warnings here:<u></u><u></u></p>
<div>
<p class="MsoNormal">1) Warn that pointer p is given a pointer from somewhere and then dereferenced without a null check.<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">2) Warn that two similar constructions only differing by pointer versus reference, and should suggest the appropriate constructor.<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">1 is hard because the assignment and dereference can be separated by some amount of intervening code that needs to be checked.  This requires at least a path-sensitive checker.  There's also the case that the check is performed by a different
 function, which further complicates the checking.  Also, getFooL() may have guarantees not visible to Clang.  If it already returns a non-null pointer, then this warning would be a false positive.<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">2 requires the comparison of the structure of functions to determine that they are identical except for the pointer/reference difference.  That would be a very fragile check and even small changes to the constructors would render Clang
 unable to see the equivalence between the two functions.<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">As you've noted, Clang does have specific warnings for very obvious and very simple cases.  More complicated cases involving examining code across different functions is difficult and time-consuming for Clang, if not impossible if the functions
 are across different translation units.  If you are concerned about this problem in the future, I recommend the dynamic analysis tool Undefined Behavior Sanitizer (<a href="http://clang.llvm.org/docs/UndefinedBehaviorSanitizer.html" target="_blank">http://clang.llvm.org/docs/UndefinedBehaviorSanitizer.html</a>). 
 Using the null checker will allow you to catch the null dereference at runtime.<u></u><u></u></p>
</div>
<div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
<div>
<p class="MsoNormal">On Tue, Jun 21, 2016 at 10:28 PM, Riyaz Puthiyapurayil via cfe-dev <<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>> wrote:<u></u><u></u></p>
<blockquote style="border:none;border-left:solid #cccccc 1.0pt;padding:0in 0in 0in 6.0pt;margin-left:4.8pt;margin-top:5.0pt;margin-right:0in;margin-bottom:5.0pt">
<div>
<div>
<p>[corrected some typos in the earlier email]<u></u><u></u></p>
<p> <u></u><u></u></p>
<p>We have some very old code that resembles the following that causes it to crash when compiled with clang (it “works” with gcc).  The problem is the dereference of ‘p’ in the constructor argument of fooLI below (line marked as [2]).  Due to this dereference,
 the constructor FooLI(const Foo<T>& foo) is invoked which in turn invokes FooBI(…). Since p has already been dereferenced, clang assumes that p is non-null (which is OK because well-formed C++ code would not dereference a NULL pointer). Clang therefore eliminates
 the null check on foo_ at line [1] assuming that it always non-NULL. So when a null pointer is returned by getFooL(), a crash occurs.<u></u><u></u></p>
<p> <u></u><u></u></p>
<p>class FooBI {<u></u><u></u></p>
<p>public:<u></u><u></u></p>
<p>    :<u></u><u></u></p>
<p>    FooBI(const FooB* foo_) :<u></u><u></u></p>
<p>        foo(foo_ && foo_->bar ? foo_ : NULL) {}   // [1]<u></u><u></u></p>
<p>    :<u></u><u></u></p>
<p>};<u></u><u></u></p>
<p>template<class T>class FooLI : public FooBI {<u></u><u></u></p>
<p>public:<u></u><u></u></p>
<p>    :    <u></u><u></u></p>
<p>    FooLI(const FooL<T>* foo) :<u></u><u></u></p>
<p>        FooBI(foo)<u></u><u></u></p>
<p>    {}<u></u><u></u></p>
<p>    FooLI(const Foo<T>& foo) :<u></u><u></u></p>
<p>        FooBI(&foo)<u></u><u></u></p>
<p>    {}<u></u><u></u></p>
<p>    :<u></u><u></u></p>
<p>};<u></u><u></u></p>
<p>:<u></u><u></u></p>
<p>    FooL<Boo>* p = getFooL(); <u></u><u></u></p>
<p>    FooLI fooLI = FooLI(*p); // [2] p can be sometimes NULL <u></u><u></u></p>
<p> <u></u><u></u></p>
<p>The fix for this problem in our code is to avoid the dereference on line [2] which will then invoke FooLI(const FooL<T>* foo) and the null check on line [1] will be retained. Is it possible for clang to generate a warning about this? -Wundefined-bool-conversion
 does generate warnings in similar situations but not specifically for this case. Perhaps, this should be a different warning. There are two choices of constructors, one accepting a pointer and another accepting a reference. When the pointer is then dereferenced
 and converted to a reference, do you think it merits a warning?<u></u><u></u></p>
<p> <u></u><u></u></p>
<p> <u></u><u></u></p>
</div>
</div>
<p class="MsoNormal" style="margin-bottom:12.0pt"><br>
_______________________________________________<br>
cfe-dev mailing list<br>
<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev</a><u></u><u></u></p>
</blockquote>
</div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
</div>
</div>
</div></div></div>
</div>
</div>

</blockquote></div><br></div></div></div>