<html>
<head>
<base href="https://bugs.llvm.org/">
</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 with Xcode9.0: Assigned value is garbage or undefined"
href="https://bugs.llvm.org/show_bug.cgi?id=33340">33340</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>false-positive with Xcode9.0: Assigned value is garbage or undefined
</td>
</tr>
<tr>
<th>Product</th>
<td>clang
</td>
</tr>
<tr>
<th>Version</th>
<td>unspecified
</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>coeur@gmx.fr
</td>
</tr>
<tr>
<th>CC</th>
<td>llvm-bugs@lists.llvm.org
</td>
</tr></table>
<p>
<div>
<pre>Created <span class=""><a href="attachment.cgi?id=18585" name="attach_18585" title="clang false-positive on Assigned value is garbage or undefined">attachment 18585</a> <a href="attachment.cgi?id=18585&action=edit" title="clang false-positive on Assigned value is garbage or undefined">[details]</a></span>
clang false-positive on Assigned value is garbage or undefined
Updating to Xcode 9 beta, using c/obj-c code from year 2003, I now get a false
positive with static-analyzer. (this issue wasn't there with Xcode 8.3.2)
Original code was available at
<a href="https://web.archive.org/web/20031221230916/http://www.merriampark.com:80/ldobjc.htm">https://web.archive.org/web/20031221230916/http://www.merriampark.com:80/ldobjc.htm</a>,
but since I changed `int` to `unsigned int`, I'm providing a gist for it:
<a href="https://gist.github.com/Coeur/72b4d0637f1cbdfef384a20022d10d28">https://gist.github.com/Coeur/72b4d0637f1cbdfef384a20022d10d28</a>
Issue:
On line `distance = d[ n * m - 1 ];`, clang will report "Assigned value is
garbage or undefined". In its reasoning, it will give "Assuming 'k' is >= 'n'",
but we are right after `n++` and `k = 0` (and `n` is an NSUInteger), so this
assumption is impossible.
Code:
@implementation NSString(Levenshtein)
// calculate the distance between two string treating them eash as a
// single word
- (float) compareWithWord: (NSString *) stringB
{
// normalize strings
NSString * stringA = [NSString stringWithString: self];
[stringA stringByTrimmingCharactersInSet:
[NSCharacterSet whitespaceAndNewlineCharacterSet]];
[stringB stringByTrimmingCharactersInSet:
[NSCharacterSet whitespaceAndNewlineCharacterSet]];
stringA = [stringA lowercaseString];
stringB = [stringB lowercaseString];
// Step 1
unsigned int k, i, j, cost, * d, distance;
NSUInteger n = [stringA length];
NSUInteger m = [stringB length];
if( n++ != 0 && m++ != 0 ) {
d = malloc( sizeof(unsigned int) * m * n );
// Step 2
for( k = 0; k < n; k++)
d[k] = k;
for( k = 0; k < m; k++)
d[ k * n ] = k;
// Step 3 and 4
for( i = 1; i < n; i++ )
for( j = 1; j < m; j++ ) {
// Step 5
if( [stringA characterAtIndex: i-1] ==
[stringB characterAtIndex: j-1] )
cost = 0;
else
cost = 1;
// Step 6
d[ j * n + i ] = [self smallestOf: d [ (j - 1) * n + i ] +
1
andOf: d[ j * n + i - 1 ] + 1
andOf: d[ (j - 1) * n + i -1 ]
+ cost ];
}
distance = d[ n * m - 1 ];
free( d );
return distance;
}
return 0.0;
}
// return the minimum of a, b and c
- (int) smallestOf: (int) a andOf: (int) b andOf: (int) c
{
int min = a;
if( b < min )
min = b;
if( c < min )
min = c;
return min;
}
@end
Screenshot attached.</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>