<div dir="ltr">On Wed, Oct 23, 2013 at 8:35 AM, Anna Zaks <span dir="ltr"><<a href="mailto:ganna@apple.com" target="_blank">ganna@apple.com</a>></span> wrote:<br><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<div style="word-wrap:break-word">Daniel,<div><br></div><div>I think Richard was asking about false positives/true positives ratio and I do not see any false positives in the list below. It looks like all the reports you found are valid. Did you run this check on large C++ codebases?</div>
<div><br></div><div>As far as I can tell there are two outstanding issues:</div><div> - Addressing these questions from Richard:</div><div><div class="im"><blockquote type="cite"><div style="word-wrap:break-word"><div style="direction:ltr;font-family:Tahoma;font-size:10pt">
<div style="font-family:'Times New Roman';font-size:16px"><blockquote type="cite"><div style="font-family:Helvetica;font-size:12px;word-wrap:break-word"><blockquote type="cite"><div style="word-wrap:break-word"><blockquote type="cite">
<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div>sizeof(expression) is a common idiom in SFINAE contexts. Is that covered here?</div></div></div></div></blockquote></div></blockquote></div></blockquote>
</div></div></div></blockquote></div>Richard, can you provide examples?</div></div></blockquote><div><br></div><div>There's one here:</div><div><br></div><div>  <a href="http://en.wikipedia.org/wiki/Substitution_failure_is_not_an_error">http://en.wikipedia.org/wiki/Substitution_failure_is_not_an_error</a></div>
<div><br></div><div>... but that's not the case I was thinking of; it's not likely that this pattern will be used with sizeof(binary operator).</div><div><br></div><div>The case I meant is more like the one here:</div>
<div><br></div><div>  <a href="http://stackoverflow.com/questions/12654067/what-is-expression-sfinae">http://stackoverflow.com/questions/12654067/what-is-expression-sfinae</a><br></div><div><br></div><div>... where 'sizeof' is just used to hide an arbitrary expression inside a function type. Here's another example using a binary operator inside a sizeof expression:</div>
<div><br></div><div>  <a href="http://stackoverflow.com/questions/2127693/sfinae-sizeof-detect-if-expression-compiles">http://stackoverflow.com/questions/2127693/sfinae-sizeof-detect-if-expression-compiles</a><br></div><div>
<br></div><div>IIRC, sizeof(expression) was the *only* way to do this kind of thing in C++98.</div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<div style="word-wrap:break-word"><div class="im"><div><blockquote type="cite"><div style="word-wrap:break-word"><div style="direction:ltr;font-family:Tahoma;font-size:10pt"><div style="font-family:'Times New Roman';font-size:16px">
<blockquote type="cite"><div style="font-family:Helvetica;font-size:12px;word-wrap:break-word"><blockquote type="cite"><div style="word-wrap:break-word"><blockquote type="cite"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">
<div>sizeof(sizeof(int)) is a "traditional" way to get sizeof(size_t). Why should we warn on that?</div></div></div></div></blockquote></div></blockquote></div></blockquote></div></div></div></blockquote></div></div>
<div>(I personally think that if this check goes into the analyzer, it's fine to warn in the second case.)</div></div></blockquote><div><br></div><div>Sure. I think it's even fine for a compiler warning, since sizeof(size_t) is a clearer way to get the same result.</div>
<div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div style="word-wrap:break-word"><div><div>
 - Where this check should go (compiler or the analyzer)?</div></div><div>I think that if you can address the questions above, this check could go into the compiler.</div><div><br></div><div>Cheers,</div><div>Anna.</div><div>
<div class="h5"><div><br><div><div>On Oct 14, 2013, at 12:43 PM, Daniel Marjamäki <<a href="mailto:Daniel.Marjamaki@evidente.se" target="_blank">Daniel.Marjamaki@evidente.se</a>> wrote:</div><br><blockquote type="cite">
<div style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;word-wrap:break-word">
<div style="direction:ltr;font-family:Tahoma;font-size:10pt"><br>Hello!<br><br>As I understood it.. you wanted to know what the hit ratio is. I have investigated the hit ratio on a number of packages in debian. Below is the negative and positives I've found. I provide the package URLs so you can look for yourself..<br>
<br>I will continue investigating and let you know if I see any false positives...<br><br><br>NEGATIVE (macro)<br>package:<span> </span><a href="http://ftp.us.debian.org/debian/pool/main/a/agedu/agedu_9723.orig.tar.gz" target="_blank">http://ftp.us.debian.org/debian/pool/main/a/agedu/agedu_9723.orig.tar.gz</a><br>
<br>#define sresize(array, number, type) \<br>    ( (void)sizeof((array)-(type *)0), \<br>      (type *) srealloc ((array), (number) * sizeof (type)) )<br><br>We don't warn about this since it's in a macro. However it is interesting:<br>
    sizeof((array)-(type *)0)<br>It seems to be written this way by intention. To get sizeof(ptrdiff_t)? Maybe sizeof(ptr-ptr) should be ok.<br><br><br><br>POSITIVE<br>package:<span> </span><a href="http://ftp.us.debian.org/debian/pool/main/a/argus-client_3.0.0.orig.tar.gz" target="_blank">http://ftp.us.debian.org/debian/pool/main/a/argus-client_3.0.0.orig.tar.gz</a><br>
<br>file: argus-clients-3.0.0/radump/print-ldp.c<br>line: 607<br>            print_unknown_data(tptr+sizeof(sizeof(struct ldp_msg_header)),"\n\t  ",<br>                               msg_len);<br><br>file: argus-clients-3.0.0/radump/print-lmp.c<br>
line: 878<br>            print_unknown_data(tptr+sizeof(sizeof(struct lmp_object_header)),"\n\t    ",<br>                               lmp_obj_len-sizeof(struct lmp_object_header));<br><br>Not sure if these are intended to be sizeof(size_t) or sizeof(struct lmp_object_header) but I guess the latter.<br>
<br><br><br>POSITIVE<br>package:<span> </span><a href="http://ftp.us.debian.org/debian/pool/main/b/babel/babel_1.4.0.dfsg.orig.tar.gz" target="_blank">http://ftp.us.debian.org/debian/pool/main/b/babel/babel_1.4.0.dfsg.orig.tar.gz</a><br>
file: babel-1.4.0.dfsg/regression/output/libC/synch_RegOut_Impl.c<br><br>332:    if ((res < 0) || (res > sizeof(s_result_strings)/sizeof(sizeof(char *)))) {<br><br>This "sizeof(sizeof(char *))" should be "sizeof(char*)" since s_result_strings is a array of char * pointers<br>
<br><br><br>POSITIVE<br>package:<span> </span><a href="http://ftp.us.debian.org/debian/pool/main/b/bird/bird_1.3.7.orig.tar.gz" target="_blank">http://ftp.us.debian.org/debian/pool/main/b/bird/bird_1.3.7.orig.tar.gz</a><br>
file: bird-1.3.7/lib/mempool.c<br><br>253:<br>  return ALLOC_OVERHEAD + sizeof(struct linpool) +<br>    cnt * (ALLOC_OVERHEAD + sizeof(sizeof(struct lp_chunk))) +<br>    m->total + m->total_large;<br><br>Not sure if this is intended to be sizeof(size_t) or sizeof(struct lp_chunk) but I guess the latter.<br>
<br><br>POSITIVE:<br>package:<span> </span><a href="http://ftp.us.debian.org/debian/pool/main/b/blender/blender_2.49.2~dfsg.orig.tar.gz" target="_blank">http://ftp.us.debian.org/debian/pool/main/b/blender/blender_2.49.2~dfsg.orig.tar.gz</a><br>
file: blender-2.49b.orig/source/blender/blenloader/intern/readfile.c<br>6987:    if(0==strncmp(ima-><a href="http://id.name" target="_blank">id.name</a>+2, "Viewer Node", sizeof(ima-><a href="http://id.name" target="_blank">id.name</a>+2))) {<br>
6991:    if(0==strncmp(ima-><a href="http://id.name" target="_blank">id.name</a>+2, "Render Result", sizeof(ima-><a href="http://id.name" target="_blank">id.name</a>+2))) {<br><br>These are bad, ima-><a href="http://id.name" target="_blank">id.name</a> is an array, it should probably be changed to:<br>
    if(0==strncmp(ima-><a href="http://id.name" target="_blank">id.name</a>+2, "Viewer Node", sizeof(ima-><a href="http://id.name" target="_blank">id.name</a>) - 2)) {<br><br><br>Best regards,<br>Daniel Marjamäki<br>
<div><br><div style="font-family:Tahoma;font-size:13px"><div style="font-family:Tahoma;font-size:13px"><div style="font-family:Tahoma;font-size:13px"><div style="font-family:Tahoma;font-size:13px"><div style="font-family:Tahoma;font-size:13px">
<div style="margin:0cm 0cm 0pt"><span style="font-family:Arial,sans-serif;color:gray;font-size:8pt">..................................................................................................................</span></div>
<div style="margin:0cm 0cm 0pt"><span style="font-family:Arial,sans-serif;font-size:8pt">Daniel Marjamäki<span> </span></span><span style="font-family:Arial,sans-serif;color:gray;font-size:8pt">Senior Engineer</span></div>
<div style="margin:0cm 0cm 0pt"><span lang="EN-US" style="font-family:Arial,sans-serif;color:gray;font-size:8.5pt">Evidente ES East</span><span lang="EN-US" style="font-family:Arial,sans-serif;color:gray;font-size:8pt"><span> </span>AB<span> <span> </span></span>Warfvinges väg 34<span> <span> </span></span>SE-112 51 Stockholm<span> <span> </span></span>Sweden</span></div>
<p class="MsoNormal" style="margin:0cm 0cm 0pt"><span lang="EN-US" style="font-family:Arial,sans-serif;color:gray;font-size:8pt"></span> </p><div style="margin:0cm 0cm 0pt"><span lang="EN-GB" style="font-family:Arial,sans-serif;color:gray;font-size:8pt">Mobile</span><span lang="EN-GB" style="font-family:Arial,sans-serif;color:gray;font-size:8pt">:<span>                <span> </span></span><a href="tel:%2B46%20%280%29709%2012%2042%2062" value="+46709124262" target="_blank">+46 (0)709 12 42 62</a></span></div>
<div style="margin:0cm 0cm 0pt"><span lang="EN-GB" style="font-family:Arial,sans-serif;color:gray;font-size:8pt"></span><span lang="EN-GB" style="font-family:Arial,sans-serif;color:gray;font-size:8pt">E-mail:<span>   <span> </span></span><span>            <span> </span><a href="mailto:Daniel.Marjamaki@evidente.se" target="_blank">Daniel.Marjamaki</a></span><a href="mailto:Daniel.Marjamaki@evidente.se" target="_blank">@evidente.se</a><span>     <span> </span></span><span>                </span></span></div>
<p class="MsoNormal" style="margin:0cm 0cm 0pt"><span lang="EN-GB" style="font-family:Arial,sans-serif;color:gray;font-size:8pt"></span> </p><div style="margin:0cm 0cm 0pt"><span lang="EN-GB" style="font-family:Arial,sans-serif;font-size:8pt"><a href="http://www.evidente.se/" target="_blank">www.evidente.se</a></span></div>
</div></div></div></div></div></div><div style="font-family:'Times New Roman';font-size:16px"><hr><div style="direction:ltr"><font face="Tahoma"><b>Från:</b><span> </span><a href="mailto:cfe-commits-bounces@cs.uiuc.edu" target="_blank">cfe-commits-bounces@cs.uiuc.edu</a><span> </span>[<a href="mailto:cfe-commits-bounces@cs.uiuc.edu" target="_blank">cfe-commits-bounces@cs.uiuc.edu</a>] för Anna Zaks [<a href="mailto:ganna@apple.com" target="_blank">ganna@apple.com</a>]<br>
<b>Skickat:</b><span> </span>den 8 oktober 2013 18:18<br><b>Till:</b><span> </span>Ted Kremenek<br><b>Cc:</b><span> </span>Anders Rönnholm; Richard Smith; <a href="mailto:cfe-commits@cs.uiuc.edu" target="_blank">cfe-commits@cs.uiuc.edu</a><br>
<b>Ämne:</b><span> </span>Re: [PATCH] [StaticAnalyzer] New checker Sizeof on expression<br></font><br></div><div></div><div><br><div><div>On Oct 7, 2013, at 10:05 PM, Ted Kremenek <<a href="mailto:kremenek@apple.com" target="_blank">kremenek@apple.com</a>> wrote:</div>
<br><blockquote type="cite"><div style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;word-wrap:break-word">
On Oct 7, 2013, at 6:05 PM, Jordan Rose <<a href="mailto:jordan_rose@apple.com" target="_blank">jordan_rose@apple.com</a>> wrote:<br><div><br><blockquote type="cite"><div style="word-wrap:break-word"><br><div><div>On Oct 7, 2013, at 13:58, Richard Smith <<a href="mailto:richard@metafoo.co.uk" target="_blank">richard@metafoo.co.uk</a>> wrote:</div>
<br><blockquote type="cite"><div dir="ltr">On Mon, Oct 7, 2013 at 9:55 AM, Jordan Rose<span> </span><span dir="ltr"><<a href="mailto:jordan_rose@apple.com" target="_blank">jordan_rose@apple.com</a>></span><span> </span>wrote:<br>
<div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div style="word-wrap:break-word">
I'm fine with this staying in the analyzer for now, unless David, Richard, or Eli feel it should be a warning right away.</div></blockquote><div><br></div><div>Do we have evidence that we want this? Does it catch bugs? If so, what do they look like? It seems like this would trigger on legitimate code; how does a user suppress the warning in that case, and does that suppression make their code clearer?<br>
</div><div><br></div><div>What is the false/true positive ratio for bug finding here?</div><div><br></div><div>sizeof(expression) is a common idiom in SFINAE contexts. Is that covered here?</div><div><br></div><div>sizeof(sizeof(int)) is a "traditional" way to get sizeof(size_t). Why should we warn on that?</div>
<div><br></div><div>And more as a general question than something specific to this patch: Is there a region in the space of false positive ratios where we think a syntactic warning should go into the static analyzer? If so, why? And what is that region? I would have thought that the static analyzer, like the clang warnings, would be aimed at (eventually) having a false positive ratio of near zero. If so, then should we ever put a warning in the static analyzer if it doesn't require the static analyzer's technology (or have a high runtime cost)?<br>
</div></div></div></div></blockquote><br></div><div>On this last (and bringing in Ted and Anna):</div><div><br></div><div>I think the main difference between compiler warnings and syntactic analyzer checks is that we try very hard to turn new compiler warnings on by default. A second-order effect of this is that we generally avoid style warnings. The analyzer can be a bit looser about this, though: because people<span> </span><i>know</i> the analyzer is stricter and more in-depth, I think they might also accept that a particular check doesn't fit their project.</div>
<div><br></div><div>On the other hand, we still haven't gotten around to designing a proper bug tracking and/or manual suppression system, so that's one advantage of compiler warnings. And as you say, checks without a high runtime cost don't really have a<span> </span><i>technical</i> reason to be in the analyzer.</div>
</div></blockquote><br></div><div>Richard’s point is correct that we want the static analyzer to also have a high signal-to-noise ratio.  Otherwise it is a useless tool.  I’m also not a fan of having the analyzer having a bunch of “junk” checkers that aren’t on by default, but if a checker, when it is enabled, is HIGHLY useful to some set of people (e.g., security experts who are more tolerant of false positive rates if they want to do an aggressive code audit)  I think the analyzer is a reasonable place to put them, given the current warning policy in Clang where we want the warnings there to be generally useful to everybody.</div>
<div><br></div><div>To put it in more context, in the beginning the guiding principal of what goes in the static analyzer was:</div><div><br></div><div>(a) The warning is very expensive to compute.</div><div><br></div><div>
 OR</div><div><br></div><div>(b) The warning is very domain-specific.  For example, an API such as CFNumberCreate() on OS X has some interesting API invariants, but we generally should not be hacking those API-specific warnings into the compiler.  Exceptions exist of course, e.g., printf checking, but often the are grounded when such APIs are fairly standard (e.g., in the C standard itself) or the checking is based on some annotation like __attribute__((format)) where the compiler doesn’t know anything about a specific function itself, just the annotation.</div>
<div><br></div><div>Style warnings can sometimes fall into (a) (in which case not putting them in the compiler makes obvious sense), but one could argue that they are more the flavor of (b) then a traditional compiler warning.  As I mentioned earlier, the static analyzer can be home to some highly specialized checkers that may not be generally useful for everybody but when enabled are very useful to certain people.  Style warnings often fit in this category of warnings.</div>
<div><br></div><div>A related problem is that we don’t have an ontology for style warnings in clang (the compiler).  Should they really be lumped into the same group of all other compiler warnings?  What about their behavior with -Werror?  -Weverything?  Style warnings really are about personal style, and users can be highly polarized about them.  For example, it would be highly useful for Clang to have a -W80-columns or -Wspaces-instead-of-tabs (not a serious proposal).  Both would be cheap to compute, would obviously benefit LLVM developers, but they wouldn’t work for everybody, or even the majority of software developers.  Where should they go?  The compiler?  The static analyzer?  Both have discoverability and usability concerns in either location.  I’d argue that these two warnings would be better suited in the compiler because (when the user wants them) they’d get run all the time, but having these warnings counteracts the general guiding principal that compiler warnings should generally be useful to most people, or clear categories of people (e.g, library authors), but not specific groups of people (e.g., LLVM developers).</div>
<div><br></div><div>This is not a thought out proposal, but I would not be opposed to a new category of warnings, say -Wstyle, and have all style warnings under -Wstyle, perhaps named “-Wstyle-80-columns” or “-Wstyle-spaces-instead-of-tabs”.  None of these warnings would be on by default.  If we want these kind of warnings in the compiler, I think these kind of warnings are worth calling out as being something “different” that people shouldn’t get hung up about if they don’t think the warning applies to them.  Also, having them in a special category of them own allows institutions to set broad warning polices such as “-Werror -Wno-error=style” where they want regular warnings to be treated as error, but not style warnings, etc.</div>
</div></blockquote></div><div><br></div>I agree with Ted. Currently, there is no good way of adding style and highly specialized warnings to clang; given the behavior of Werror and Weverything. For example, we encourage users to learn about new generic warnings by turning on Weverything, so the specialized warnings should not be included there. </div>
</div></div></div></blockquote></div><br></div></div></div></div></blockquote></div><br></div></div>