[llvm-bugs] [Bug 33340] New: false-positive with Xcode9.0: Assigned value is garbage or undefined

via llvm-bugs llvm-bugs at lists.llvm.org
Tue Jun 6 23:22:00 PDT 2017


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

            Bug ID: 33340
           Summary: false-positive with Xcode9.0: Assigned value is
                    garbage or undefined
           Product: clang
           Version: unspecified
          Hardware: Macintosh
                OS: MacOS X
            Status: NEW
          Severity: normal
          Priority: P
         Component: Static Analyzer
          Assignee: kremenek at apple.com
          Reporter: coeur at gmx.fr
                CC: llvm-bugs at lists.llvm.org

Created attachment 18585
  --> https://bugs.llvm.org/attachment.cgi?id=18585&action=edit
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
https://web.archive.org/web/20031221230916/http://www.merriampark.com:80/ldobjc.htm,
but since I changed `int` to `unsigned int`, I'm providing a gist for it:
https://gist.github.com/Coeur/72b4d0637f1cbdfef384a20022d10d28

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.

-- 
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/20170607/9876ded3/attachment.html>


More information about the llvm-bugs mailing list