<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
</head>
<body text="#000000" bgcolor="#FFFFFF">
<div class="moz-cite-prefix">On 09/03/2015 06:52 PM, Bob Kuo via
cfe-dev wrote:<br>
</div>
<blockquote
cite="mid:CAGuPgHKheLELEbvtsa0U9F8SxnRXydxCFyXiRhtfL8FHkEN0CA@mail.gmail.com"
type="cite">
<div dir="ltr">Hello cfe-dev,
<div><br>
</div>
<div>I saw a presentation from someone using another static
analysis tool (<a moz-do-not-send="true"
href="http://www.slideshare.net/Andrey_Karpov/200-open-sourceprojectslater">http://www.slideshare.net/Andrey_Karpov/200-open-sourceprojectslater</a>)
and in that presentation they mentioned finding a relatively
simple error with strncmp. They searched for calls to strncmp
where one or both of the string parameters were shorter than
the limit provided. That is, we have something like:</div>
<div><br>
</div>
<div> bar = strncmp(foo, "asdf", 6);</div>
<div><br>
</div>
<div>That this was flagged as an error by the static analyzer.
I did a simple regex search through one of the larger code
bases at work and found several errors of this type.</div>
</div>
</blockquote>
This code is not erroneous. It's mildly suspicious, but given the
second operand is a constant, only vaguely so. A style warning
would be reasonable. <br>
<blockquote
cite="mid:CAGuPgHKheLELEbvtsa0U9F8SxnRXydxCFyXiRhtfL8FHkEN0CA@mail.gmail.com"
type="cite">
<div dir="ltr">
<div><br>
</div>
<div>I've got a basic patch to clang-tidy to add this as a
checker.</div>
<div><br>
</div>
<div>A few questions:</div>
<div><br>
</div>
<div>1) Is this type of checking sound? If so, should I put it
up for review?</div>
<div>2) In the above example, would strncmp(foo, "asdf", 5) be
an error? That is, should we include the null byte at the end
of the string?</div>
<div>3) One possible valid use case of strncmp is to do prefix
checking. In that case the limit n would be less than one or
both of the strings. However, in my completely unscientific
sample at work I saw what appear to be copy-paste errors where
the string was changed but not the size with the resulting
strncmp only checking a prefix of the given string. Should we
warn in these cases as well?</div>
</div>
</blockquote>
No. Possibly when one of the arguments is a constant, but not
otherwise. <br>
<blockquote
cite="mid:CAGuPgHKheLELEbvtsa0U9F8SxnRXydxCFyXiRhtfL8FHkEN0CA@mail.gmail.com"
type="cite">
<div dir="ltr">
<div>4) My clang-tidy checker is implemented with ASTMatchers
for very simplistic checking. Would it be better to implement
this for context-sensitive checking? If so, can someone point
me to an existing checker to study?</div>
<div><br>
</div>
<div>Thanks,</div>
<div><br>
</div>
<div>Bob Kuo</div>
</div>
<br>
<fieldset class="mimeAttachmentHeader"></fieldset>
<br>
<pre wrap="">_______________________________________________
cfe-dev mailing list
<a class="moz-txt-link-abbreviated" href="mailto:cfe-dev@lists.llvm.org">cfe-dev@lists.llvm.org</a>
<a class="moz-txt-link-freetext" href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev">http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev</a>
</pre>
</blockquote>
<br>
</body>
</html>