[llvm-bugs] [Bug 43469] New: clang-static-analyzer performs invalid range analysis with bitwise operators

via llvm-bugs llvm-bugs at lists.llvm.org
Thu Sep 26 10:48:00 PDT 2019


https://bugs.llvm.org/show_bug.cgi?id=43469

            Bug ID: 43469
           Summary: clang-static-analyzer performs invalid range analysis
                    with bitwise operators
           Product: clang
           Version: trunk
          Hardware: PC
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: Static Analyzer
          Assignee: dcoughlin at apple.com
          Reporter: zturner at google.com
                CC: dcoughlin at apple.com, llvm-bugs at lists.llvm.org

https://gcc.godbolt.org/z/c4Rbhl

Given the code:

void foo(int count)
{
    char data[1024];
    assert(count <= 1024);

    int count_aligned = (count + 15) & ~15;
    assert(count_aligned >= count);

    for (int i = 0; i < count_aligned; ++i)
        data[i] = 1;

    for (int i = 0; i < count; ++i)
        if (data[i] != 1)
            break;
}

static analyzer is unable to deduce that count_aligned >= count.  This results
in:
warning: The left operand of '!=' is a garbage value
        if (data[i] != 1)

This in and of itself is not super surprising, because static range analysis
can be difficult (although there *are* well known results with respect to range
analysis involving bitwise operators, so this case is solvable).  What is
surprising is that no amount of hints can convince static analyzer that this
code is safe.  For example, the assert there does not silence the warning. 
Even if I wrap the second loop in a conditional such as:

if (count_aligned >= count) {
    for (int i = 0; i < count; ++i)
        if (data[i] != 1)
            break;
}

it *still* does not silence the warning.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20190926/8456b533/attachment.html>


More information about the llvm-bugs mailing list