Package: python-cjson
Version: 1.0.5-1
Severity: normal

Description: A patch to unescape the solidus for decoding.

-- 
Adrien DELLE CAVE
Proformatique - 67 rue Voltaire - 92800 Puteaux
Tel. : 01 41 38 99 68 - Fax. : 01 41 38 99 70
a...@proformatique.com - http://www.proformatique.com/

Index: python-cjson-1.0.5/cjson.c
===================================================================
--- python-cjson-1.0.5.orig/cjson.c	2009-06-24 17:05:43.000000000 +0200
+++ python-cjson-1.0.5/cjson.c		2009-06-24 17:15:33.000000000 +0200
@@ -117,26 +117,38 @@
 decode_string(JSONData *jsondata)
 {
     PyObject *object;
-    int c, escaping, has_unicode, string_escape;
+    int c, escaping, has_unicode, string_escape, finished;
     Py_ssize_t len;
     char *ptr;
+    char *dest, *dest_scan;
+
+    dest = malloc(strlen(jsondata->ptr) /* not + 1, first char is skipped */);
+    if (dest == NULL)
+        return NULL;
 
-    // look for the closing quote
-    escaping = has_unicode = string_escape = False;
+    dest_scan = dest;
+    escaping = has_unicode = string_escape = finished = False;
     ptr = jsondata->ptr + 1;
-    while (True) {
+    while (!finished) {
         c = *ptr;
         if (c == 0) {
             PyErr_Format(JSON_DecodeError,
                          "unterminated string starting at position " SSIZE_T_F,
                          (Py_ssize_t)(jsondata->ptr - jsondata->str));
+            free(dest);
             return NULL;
         }
+        *dest_scan = c;
         if (!escaping) {
             if (c == '\\') {
-                escaping = True;
+                if (ptr[1] == '/') {
+                    *dest_scan = '/';
+                    ptr++;
+                } else {
+                    escaping = True;
+                }
             } else if (c == '"') {
-                break;
+                finished = True;
             } else if (!isascii(c)) {
                 has_unicode = True;
             }
@@ -158,16 +170,19 @@
             escaping = False;
         }
         ptr++;
+        dest_scan++;
     }
 
-    len = ptr - jsondata->ptr - 1;
+    *dest_scan = '\0';
+
+    len = dest_scan - dest - 1;
 
     if (has_unicode || jsondata->all_unicode)
-        object = PyUnicode_DecodeUnicodeEscape(jsondata->ptr+1, len, NULL);
+        object = PyUnicode_DecodeUnicodeEscape(dest, len, NULL);
     else if (string_escape)
-        object = PyString_DecodeEscape(jsondata->ptr+1, len, NULL, 0, NULL);
+        object = PyString_DecodeEscape(dest, len, NULL, 0, NULL);
     else
-        object = PyString_FromStringAndSize(jsondata->ptr+1, len);
+        object = PyString_FromStringAndSize(dest, len);
 
     if (object == NULL) {
         PyObject *type, *value, *tb, *reason;
@@ -195,9 +210,10 @@
         Py_XDECREF(value);
         Py_XDECREF(tb);
     } else {
-        jsondata->ptr = ptr+1;
+        jsondata->ptr = ptr;
     }
 
+    free(dest);
     return object;
 }
 
Index: python-cjson-1.0.5/jsontest.py
===================================================================
--- python-cjson-1.0.5.orig/jsontest.py	2009-06-24 17:12:57.000000000 +0200
+++ python-cjson-1.0.5/jsontest.py	2009-06-24 17:15:09.000000000 +0200
@@ -49,9 +49,9 @@
         obj = cjson.decode(r'"\""')
         self.assertEqual(r'"', obj)
 
-#    def testReadEscapedSolidus(self):
-#        obj = cjson.decode(r'"\/"')
-#        self.assertEqual(r'/', obj)
+    def testReadEscapedSolidus(self):
+        obj = cjson.decode(r'"\/"')
+        self.assertEqual(r'/', obj)
 
     def testReadEscapedReverseSolidus(self):
         obj = cjson.decode(r'"\\"')

Attachment: signature.asc
Description: OpenPGP digital signature

Reply via email to