Analyzer doesn't reason symbolically about disquality
| Bugzilla Link | 27674 |
| Resolution | DUPLICATE |
| Resolved on | May 08, 2016 12:33 |
| Version | unspecified |
| OS | MacOS X |
| Reporter | LLVM Bugzilla Contributor |
| CC | @devincoughlin |
Extended Description
$ clang --version Apple LLVM version 7.0.2 (clang-700.1.81) Target: x86_64-apple-darwin14.5.0 Thread model: posix
Reproduction code:
class Buffer { void* m_pBuffer; size_t m_size;
bool Equal( const Buffer& rhs)
{
if( !m_pBuffer && rhs.m_pBuffer )
{
// m_pBuffer is null
return false;
}
else if( m_pBuffer && !rhs.m_pBuffer )
{
// rhs.m_pBuffer is null
return false;
}
else
{
// neither argument is null but analyzer warns:
// Null pointer argument in call to memory comparison functions
int cmp = memcmp( m_pBuffer, rhs.m_pBuffer, std::min( m_size, rhs.m_size ) );
return cmp == 0;
}
}
};
This is a dup of llvm/llvm-project#13798 .
*** This bug has been marked as a duplicate of bug llvm/llvm-project#13798 ***
The root cause here is that the analyzer doesn't reason symbolically about dis-equality.
$ cat disequality.c void clang_analyzer_eval(int);
void bar(int *a, int *b) { if (a == b) return;
if (a != 0) return;
clang_analyzer_eval(b != 0); // UNKNOWN but should be TRUE }
$ clang -cc1 -analyze -analyzer-checker=core,debug.ExprInspection disequality.c disequality.c:10:3: warning: UNKNOWN clang_analyzer_eval(b != 0); // UNKNOWN but should be TRUE ^~~~~~~~~~~~~~~~~~~~~~~~~~~ 1 warning generated.
Here the analyzer doesn't know that if a != b and a == 0 then b != 0.
Sorry, previous test case was simplified too much and should have warned (because both m_pBuffer pointers could be NULL). Here's a better test case:
class Buffer { void* m_pBuffer; size_t m_size;
bool EqualShouldntWarn( const Buffer& rhs)
{
if( m_pBuffer == rhs.m_pBuffer )
{
// pointers equal or both null
return true;
}
else if( !m_pBuffer && rhs.m_pBuffer )
{
// m_pBuffer is null
return false;
}
else if( m_pBuffer && !rhs.m_pBuffer )
{
// rhs.m_pBuffer is null
return false;
}
else
{
// neither m_pBuffer is null but analyzer warns:
// Null pointer argument in call to memory comparison functions
int cmp = memcmp( m_pBuffer, rhs.m_pBuffer, std::min( m_size, rhs.m_size ) );
return cmp == 0;
}
}
bool EqualShouldWarn( const Buffer& rhs)
{
if( !m_pBuffer && rhs.m_pBuffer )
{
// m_pBuffer is null
return false;
}
else if( m_pBuffer && !rhs.m_pBuffer )
{
// rhs.m_pBuffer is null
return false;
}
else
{
// both m_pBuffers may be null so analyzer should warn:
// Null pointer argument in call to memory comparison functions
int cmp = memcmp( m_pBuffer, rhs.m_pBuffer, std::min( m_size, rhs.m_size ) );
return cmp == 0;
}
}
};
assigned to @tkremenek