<html>
    <head>
      <base href="http://llvm.org/bugs/" />
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW --- - static analyzer fails to evaluate strlen"
   href="http://llvm.org/bugs/show_bug.cgi?id=18915">18915</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>static analyzer fails to evaluate strlen
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Linux
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>Static Analyzer
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>kremenek@apple.com
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>ed0.88.prez@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvmbugs@cs.uiuc.edu
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Example from the Juliet test suite:

$ cat strlenbug.c
#include <stdlib.h>
#include <string.h>

void f()
{
    char *c = (char *)malloc(100*sizeof(char));
    if (!c)
        return;
    c[0] = '\0';
    size_t cLen = strlen(c);
    if (100-cLen > 1)
        ; //ok
    else {
        if (c[1] == '\0') // should not go here!
            ;
    }
    free(c);
}
EOF

When I run the analyzer I get:

$ scan-build --use-analyzer /usr/bin/clang -o buildres/ clang -c strlenbug.c -o
/dev/null > /dev/null
strlenbug.c:14:18: warning: The left operand of '==' is a garbage value
        if (c[1] == '\0') // should not go here!
            ~~~~ ^
1 warning generated.


In the html-generated file, the analyzer takes the 'false' branch for the
condition "100-cLen > 1", which is not possible, but if I replace line 10 with
the inlined version of strlen():

size_t cLen = 0;
while(c[cLen] != '\0')
    ++c;

the report goes away (and alpha checker reports correctly "never executed"
warning for the expressions '++c;' and 'if (c[1] == '\0')').

Unrelated problem: if I remove the 'does malloc return null' line, I don't get
any message regarding the possible null pointer dereference in "c[0] = '\0';"
when malloc fails, even with alpha check enabled.</pre>
        </div>
      </p>
      <hr>
      <span>You are receiving this mail because:</span>
      
      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>