<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 --- - False positive null reference warning in C++ code"
href="http://llvm.org/bugs/show_bug.cgi?id=15893">15893</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>False positive null reference warning in C++ code
</td>
</tr>
<tr>
<th>Product</th>
<td>clang
</td>
</tr>
<tr>
<th>Version</th>
<td>trunk
</td>
</tr>
<tr>
<th>Hardware</th>
<td>Macintosh
</td>
</tr>
<tr>
<th>OS</th>
<td>MacOS X
</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>bugzilla@jwwalker.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>Overview:
The static analyzer says that a null reference is being returned. As far as I
can tell, the code cannot do that, though it could throw an exception.
Steps to reproduce:
Analyze this code:
-------------------------
typedef int MySizeType;
class MyArray
{
public:
MyArray();
~MyArray();
int& operator[]( int index ) { return mArray[ index ]; }
MySizeType size() const { return mSize; }
MySizeType capacity() const { return mCapacity; }
void grow( MySizeType newSize );
private:
int* mArray;
MySizeType mSize;
MySizeType mCapacity;
};
MyArray::MyArray()
: mArray( NULL )
, mSize( 0 )
, mCapacity( 0 )
{
}
MyArray::~MyArray()
{
delete [] mArray;
}
void MyArray::grow( MySizeType newSize )
{
mSize = newSize;
if (mSize > mCapacity)
{
mCapacity = mSize;
delete [] mArray;
mArray = new int[ mCapacity ];
}
}
static void foo( MyArray& outFaces )
{
outFaces.grow( 4 );
for ( unsigned int i = 0; i < 4; ++i )
{
outFaces[ 0 ] = 1;
}
}
static void bar()
{
MyArray xFaces;
MyArray facesWithCopies;
foo( facesWithCopies );
const unsigned int kNumFaces = facesWithCopies.size();
xFaces.grow( kNumFaces );
if (kNumFaces > 0)
{
xFaces[0] = 0;
}
}
-------------------------
Actual results:
warning: Returning null reference
pointing to the line of code xFaces[0] = 0.
Expected results:
No warning.
Build date:
clang version 3.3 (trunk 180897)
Target: x86_64-apple-darwin11.4.2
Thread model: posix
Additional information:
If I change the first line to
typedef unsigned int MySizeType;
then the warning goes away. I can see that it's probably a good idea to
represent a size with an unsigned type, but I don't understand what Clang is
complaining about in this particular case.</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>