Question

I am using an open-source json parser, but while compiling its giving me dereferencing warnings.

MyProject/Classes/SBJSON.m:403:13 Potential null dereference. According to coding standards in 'Creating and Returning NSError Objects' the parameter 'error' may be null

- (BOOL)scanValue:(NSObject **)o error:(NSError **)error
{
skipWhitespace(c);

switch (*c++) {
    case '{':
        return [self scanRestOfDictionary:(NSMutableDictionary **)o error:error];
        break;
    case '[':
        return [self scanRestOfArray:(NSMutableArray **)o error:error];
        break;
    case '"':
        return [self scanRestOfString:(NSMutableString **)o error:error];
        break;
    case 'f':
        return [self scanRestOfFalse:(NSNumber **)o error:error];
        break;
    case 't':
        return [self scanRestOfTrue:(NSNumber **)o error:error];
        break;
    case 'n':
        return [self scanRestOfNull:(NSNull **)o error:error];
        break;
    case '-':
    case '0'...'9':
        c--; // cannot verify number correctly without the first character
        return [self scanNumber:(NSNumber **)o error:error];
        break;
    case '+':
        *error = err(EPARSENUM, @"Leading + disallowed in number");
        return NO;
        break;
    case 0x0:
        *error = err(EEOF, @"Unexpected end of string");
        return NO;
        break;
    default:
        *error = err(EPARSE, @"Unrecognised leading character");
        return NO;
        break;
}

NSAssert(0, @"Should never get here");
return NO;
}

here it shows dereferencing at error.

Was it helpful?

Solution

It complains that the error parameter might be NULL (in case the caller choses to pass NULL). Then, the statement

*error = err(EPARSENUM, @"Leading + disallowed in number");

will crash. A reported work-around is to put an if statement around the assignment:

if(error){
    *error = err(EPARSENUM, @"Leading + disallowed in number");
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top