My favorites | Sign in
Project Home
READ-ONLY: This project has been archived. For more information see this post.
Search
for
  Advanced search   Search tips   Subscriptions
Issue 85: CJSONScanner crashes on a json like: {"array":[,]}
2 people starred this issue and may be notified of changes. Back to list
Status:  Fixed
Owner:  ----
Closed:  Nov 2010


 
Reported by Ishaq.Ma...@gmail.com, Aug 11, 2010
Please replace the |scanJSONArray:error| in CJSONScanner.m with the following code:

--


- (BOOL)scanJSONArray:(NSArray **)outArray error:(NSError **)outError
{
NSUInteger theScanLocation = [self scanLocation];

if ([self scanCharacter:'['] == NO)
	{
	if (outError)
		{
		NSDictionary *theUserInfo = [NSDictionary dictionaryWithObjectsAndKeys:
			@"Could not scan array. Array not started by a '{' character.", NSLocalizedDescriptionKey,
			NULL];
		*outError = [NSError errorWithDomain:kJSONScannerErrorDomain code:-7 userInfo:theUserInfo];
		}
	return(NO);
	}

NSMutableArray *theArray = [[NSMutableArray alloc] init];

[self skipWhitespace];
while ([self currentCharacter] != ']')
	{
	NSString *theValue = NULL;
	if ([self scanJSONObject:&theValue error:outError] == NO)
		{
		[self setScanLocation:theScanLocation];
		if (outError)
			{
			NSDictionary *theUserInfo = [NSDictionary dictionaryWithObjectsAndKeys:
				@"Could not scan array. Could not scan a value.", NSLocalizedDescriptionKey,
				NULL];
			*outError = [NSError errorWithDomain:kJSONScannerErrorDomain code:-8 userInfo:theUserInfo];
			}
		[theArray release];
		return(NO);
		}
	
	if(theValue == nil)
	{
		if (outError)
		{
			NSDictionary *theUserInfo = [NSDictionary dictionaryWithObjectsAndKeys:
										 @"Could not scan array. Value is NULL.", NSLocalizedDescriptionKey,
										 NULL];
			*outError = [NSError errorWithDomain:kJSONScannerErrorDomain code:-9 userInfo:theUserInfo];
		}
		[theArray release];
		return(NO);
	}

	[theArray addObject:theValue];
	
	[self skipWhitespace];
	if ([self scanCharacter:','] == NO)
		{
		[self skipWhitespace];
		if ([self currentCharacter] != ']')
			{
			[self setScanLocation:theScanLocation];
			if (outError)
				{
				NSDictionary *theUserInfo = [NSDictionary dictionaryWithObjectsAndKeys:
					@"Could not scan array. Array not terminated by a ']' character.", NSLocalizedDescriptionKey,
					NULL];
				*outError = [NSError errorWithDomain:kJSONScannerErrorDomain code:-9 userInfo:theUserInfo];
				}
			[theArray release];
			return(NO);
			}
		
		break;
		}
	[self skipWhitespace];
	}

[self skipWhitespace];

if ([self scanCharacter:']'] == NO)
	{
	[self setScanLocation:theScanLocation];
	if (outError)
		{
		NSDictionary *theUserInfo = [NSDictionary dictionaryWithObjectsAndKeys:
			@"Could not scan array. Array not terminated by a ']' character.", NSLocalizedDescriptionKey,
			NULL];
		*outError = [NSError errorWithDomain:kJSONScannerErrorDomain code:-10 userInfo:theUserInfo];
		}
	[theArray release];
	return(NO);
	}

if (outArray != NULL)
	*outArray = [[theArray copy] autorelease];

[theArray release];

return(YES);
}


---

this code has the following check:

if(theValue == nil)
	{
		if (outError)
		{
			NSDictionary *theUserInfo = [NSDictionary dictionaryWithObjectsAndKeys:
										 @"Could not scan array. Value is NULL.", NSLocalizedDescriptionKey,
										 NULL];
			*outError = [NSError errorWithDomain:kJSONScannerErrorDomain code:-9 userInfo:theUserInfo];
		}
		[theArray release];
		return(NO);
	}


without this check, it crashes on strings like: {"array":[,]}


Thanks.

Aug 11, 2010
Project Member #1 jwight
Please provide a patch or use git to make a branch and send me a pull request (http://github.com/schwa/TouchXML).
Nov 1, 2010
Project Member #2 jwight
Fixed in latest github.
Status: Fixed

Powered by Google Project Hosting