When an invalid string is passed to JSONObjectWithData like this

NSError * NSJSONError = nil;
NSString * json_string = [NSString stringWithFormat:@"\"key1"];
NSDictionary * json_object = [NSJSONSerialization
JSONObjectWithData:[json_string
dataUsingEncoding:NSUTF8StringEncoding] options:0 error:&NSJSONError];

OR like this

NSError * NSJSONError = nil;
NSString * json_string = [NSString stringWithFormat:@"{\"key1"];
NSDictionary * json_object = [NSJSONSerialization
JSONObjectWithData:[json_string
dataUsingEncoding:NSUTF8StringEncoding] options:0 error:&NSJSONError];

An exception as below is raised

Uncaught exception NSRangeException, reason: in getCharacters:range:, range
{ 6, 64 } extends beyond size (5)

This happens because the string is starting with double quotes " but not
ending with double quotes " and we reach
end of data. So if string is invalid the program should not generate the
exception. Instead it should set error to
JSON Parse error. This patch fixes the issue.

Signed-off-by: Abbas Raza <abbas.raza.1...@gmail.com>
---
 Source/NSJSONSerialization.m | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/Source/NSJSONSerialization.m b/Source/NSJSONSerialization.m
index 191546a..f3c925e 100644
--- a/Source/NSJSONSerialization.m
+++ b/Source/NSJSONSerialization.m
@@ -409,6 +409,13 @@ parseString(ParserState *state)
       next = consumeChar(state);
     }

+  if (currentChar(state) != '"')
+  {
+         [val release];
+         parseError(state);
+         return nil;
+  }
+
   if (bufferIndex > 0)
     {
       NSMutableString *str;
--
2.6.4 (Apple Git-63)
_______________________________________________
Bug-gnustep mailing list
Bug-gnustep@gnu.org
https://lists.gnu.org/mailman/listinfo/bug-gnustep

Reply via email to