Skip to contentSkip to conversation

Fix JSON string formatter

PATCH In review Markus Armbruster 3 participants 20 messages Mar 14, 2013 – Apr 11, 2013

lore
Share & tools
lore
#0 lore raw

cover Fix JSON string formatter

This should unbreak "make check" on machines where char is unsigned. Blue, please give it a whirl.

The JSON parser is still as broken as ever. Left for another day.

Markus Armbruster (4):
unicode: New mod_utf8_codepoint()
check-qjson: Fix up a few bogus comments
check-qjson: Test noncharacters other than U+FFFE, U+FFFF in strings
qjson: to_json() case QTYPE_QSTRING is buggy, rewrite

 include/qemu-common.h |   3 +
 qobject/qjson.c       | 102 ++++++++----------
 tests/check-qjson.c   | 280 +++++++++++++++++++++++++++++---------------------
 util/Makefile.objs    |   1 +
 util/unicode.c        |  96 +++++++++++++++++
 5 files changed, 306 insertions(+), 176 deletions(-)
 create mode 100644 util/unicode.c
Signature · 1 line
1.7.11.7
#1 lore raw

PATCH 1/4 unicode: New mod_utf8_codepoint()

---
 include/qemu-common.h |  3 ++
 util/Makefile.objs    |  1 +
 util/unicode.c        | 96 +++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 100 insertions(+)
 create mode 100644 util/unicode.c
include/qemu-common.h +3 -0
index 5e13708..28c10a0 100644
@@ -442,4 +442,7 @@ int64_t pow2floor(int64_t value);
 int uleb128_encode_small(uint8_t *out, uint32_t n);
 int uleb128_decode_small(const uint8_t *in, uint32_t *n);
 
+/* unicode.c */
+int mod_utf8_codepoint(const char *s, size_t n, char **end);
+
 #endif
util/Makefile.objs +1 -0
index cad5ce8..65c8c12 100644
@@ -9,3 +9,4 @@ util-obj-y += error.o qemu-error.o
 util-obj-$(CONFIG_POSIX) += compatfd.o
 util-obj-y += iov.o aes.o qemu-config.o qemu-sockets.o uri.o notify.o
 util-obj-y += qemu-option.o qemu-progress.o
+util-obj-y += unicode.o
util/unicode.c new file +96 -0

96 lines not on this page · raw message

Signature · 1 line
1.7.11.7
#2 lore raw

Re: [Qemu-devel] [PATCH 1/4] unicode: New mod_utf8_codepoint()

I've (re-)read the UTF-8 article in wikipedia now... comments below

Quoted 12 lines · Markus Armbruster
util/unicode.c new file +8 -0
new file mode 100644
index 0000000..954bcf7
 
+/**
+ * mod_utf8_codepoint:
+ * @s: string encoded in modified UTF-8
+ * @n: maximum number of bytes to read from @s, if less than 6
+ * @end: set to end of sequence on return
+ *
+ * Convert the modified UTF-8 sequence at the start of @s.  Modified
+ * UTF-8 is exactly like UTF-8, except U+0000 is encoded as

util/unicode.c line 32

Quoted 10 lines
util/unicode.c new file +10 -0
+ * "\xC0\x80".
+ *
+ * If @s points to an impossible byte (0xFE or 0xFF) or a continuation
+ * byte, the sequence is invalid, and @end is set to @s + 1
+ *
+ * Else, the first byte determines how many continuation bytes are
+ * expected.  If there are fewer, the sequence is invalid, and @end is
+ * set to @s + 1 + actual number of continuation bytes.  Else, the
+ * sequence is well-formed, and @end is set to @s + 1 + expected
+ * number of continuation bytes.

The wording also covers (number of cont. bytes == 0), OK.

... One point that wasn't clear to me until I read the code too is that "there are fewer" covers both "out of bytes" and "byte available, but it's not a cont. byte". Subtle :)

The way "*end" is set ensures progress.

Quoted 11 lines

+ *
+ * A well-formed sequence is valid unless it encodes a code point
+ * outside the Unicode range U+0000..U+10FFFF, one of Unicode's 66
+ * noncharacters, a surrogate code point, or is overlong. Except the
+ * overlong sequence "\xC0\x80" is valid.
+ *
+ * Conversion succeeds if and only if the sequence is valid.
+ *
+ * Returns: the Unicode code point on success, -1 on failure.
+ */
+int mod_utf8_codepoint(const char *s, size_t n, char **end)

The type of "end" follows the strtol() prototype. I guess I'd prefer "const char **end", and "unsigned char" all around actually. (The input is binary garbage.) Anyway this is irrelevant. ("const char **end" would be probably quite inconvenient for the caller,
<http://www.c-faq.com/ansi/constmismatch.html>.)

util/unicode.c line 53

Quoted 10 lines
util/unicode.c new file +10 -0
+{
+    static int min_cp[5] = { 0x80, 0x800, 0x10000, 0x200000, 0x4000000 };
+    const unsigned char *p;
+    unsigned byte, mask, len, i;
+    int cp;
+
+    if (n == 0) {
+        *end = (char *)s;
+        return 0;
+    }

This is a special case (we return the code point U+0000 after looking at zero bytes); we can probably expect the caller to know about n==0.

util/unicode.c line 56

Quoted 3 lines
util/unicode.c new file +3 -0
+
+    p = (const unsigned char *)s;
+    byte = *p++;

We have n>=1 bytes here, and pointing one past the last one (in case n==1) is allowed.

util/unicode.c line 58

Quoted 2 lines
util/unicode.c new file +2 -0
+    if (byte < 0x80) {
+        cp = byte;              /* one byte sequence */

So, since this is modified UTF-8, the U+0000 code point is represented with the overlong "\xC0\x80" sequence, and a bare '\0' can never be part of the Modified UTF-8 byte stream. (According to wp, '\0' is used in C and similar to zero-terminate the string, but I think that's outside the scope of the wire format.)

If we find a '\0' here, we report it as code point U+0000 instead of rejecting it. One could maybe argue that it's a violation of the interface contract (namely, due to '\0' being at offset #0, the caller should have passed in n==0), but I assume from the description that the caller need not have any idea about the contents, knowing the size should be enough.

IOW I'm not sure about the intended use of this function, but if the caller is allowed to throw any binary data at it (with correctly communicated size of course), then we can misreport U+0000 here. (*)

util/unicode.c line 60

Quoted 2 lines
util/unicode.c new file +2 -0
+    } else if (byte >= 0xFE) {
+        cp = -1;                /* impossible bytes 0xFE, 0xFF */

OK, binary 1111111x is as first byte misformed.

util/unicode.c line 62

Quoted 2 lines
util/unicode.c new file +2 -0
+    } else if ((byte & 0x40) == 0) {
+        cp = -1;                /* unexpected continuation byte */

We know here that byte >= 0x80, and continuation bytes look like 10xxxxxx, OK.

util/unicode.c line 64

Quoted 2 lines
util/unicode.c new file +2 -0
+    } else {
+        /* multi-byte sequence */

We have one of:

110xxxxx
1110xxxx
11110xxx
111110xx
1111110x

util/unicode.c line 69

Quoted 5 lines
util/unicode.c new file +5 -0
+        len = 0;
+        for (mask = 0x80; byte & mask; mask >>= 1) {
+            len++;
+        }
+        assert(len > 1 && len < 7);

OK, [2..6].

(Maybe use g_assert()? :))

Quoted 1 line

+ cp = byte & (mask - 1);

OK, the only bit set in "mask" matches the leftmost clear bit in "byte". (mask-1) selects the xxxx bits in "byte".

Quoted 1 line

+ for (i = 1; i < len; i++) {

Runs at least once, and as many times as we need continuation bytes. OK.

Quoted 1 line

+ byte = i < n ? *p : 0;

"p" is valid to evaluate, "*p" may not be, so we check first. OK.

util/unicode.c line 76

Quoted 4 lines
util/unicode.c new file +4 -0
+            if ((byte & 0xC0) != 0x80) {
+                cp = -1;        /* continuation byte missing */
+                goto out;
+            }

Right, if a byte is available, it must look 10xxxxxx (binary). If we're out of bytes, we also take this branch. *end will be left one past the actual cont. bytes.

util/unicode.c line 80

Quoted 4 lines
util/unicode.c new file +4 -0
+            p++;
+            cp <<= 6;
+            cp |= byte & 0x3F;
+        }

We consume the cont. byte: p++ is valid to evaluate, and we shift in the low six bits from the cont byte into the codepoint.

The loop runs at most 5 times (for len==6), shifting in (len-1)*6 bits at most, in addition to the initial cp assignment. The initial cp assignment clusters the masked-in bits at the LSB end:

    mask == (0x80 >> len) - 1

hence the number of masked-in bits in the original cp assignment is 7-len (which difference falls into [1..5]). So the total number of bits shifted into "cp" is

    7-len + (len-1)*6 == 1 + 5 * len

Since len <= 6, the above is <= 31, which is perfect for our "int" (int32_t in practice).

util/unicode.c line 82

Quoted 2 lines
util/unicode.c new file +2 -0
+        if (cp > 0x10FFFF) {
+            cp = -1;            /* beyond Unicode range */

OK.

util/unicode.c line 85

Quoted 3 lines
util/unicode.c new file +3 -0
+        } else if ((cp >= 0xFDD0 && cp <= 0xFDEF)
+                   || (cp & 0xFFFE) == 0xFFFE) {
+            cp = -1;            /* noncharacter */

http://en.wikipedia.org/wiki/Mapping_of_Unicode_characters#Noncharacters

Interesting. OK.

util/unicode.c line 87

Quoted 2 lines
util/unicode.c new file +2 -0
+        } else if (cp >= 0xD800 && cp <= 0xDFFF) {
+            cp = -1;            /* surrogate code point */

OK.

util/unicode.c line 96

Quoted 10 lines
util/unicode.c new file +9 -0
+        } else if (cp < min_cp[len - 2] && !(cp == 0 && len == 2)) {
+            cp = -1;            /* overlong, not \xC0\x80 */
+        }
+    }
+
+out:
+    *end = (char *)p;
+    return cp;
+}

I think if we want to adhere to Modified UTF-8 strictly, then (*) should be fixed. If not, I can give my Rb; this is nice code.

Laszlo

#3 lore raw

Re: [Qemu-devel] [PATCH 1/4] unicode: New mod_utf8_codepoint()

Quoted 27 lines · Laszlo Ersek

I've (re-)read the UTF-8 article in wikipedia now... comments below

Quoted 12 lines · Markus Armbruster
util/unicode.c new file +8 -0
new file mode 100644
index 0000000..954bcf7
 
+/**
+ * mod_utf8_codepoint:
+ * @s: string encoded in modified UTF-8
+ * @n: maximum number of bytes to read from @s, if less than 6
+ * @end: set to end of sequence on return
+ *
+ * Convert the modified UTF-8 sequence at the start of @s.  Modified
+ * UTF-8 is exactly like UTF-8, except U+0000 is encoded as

util/unicode.c line 32

Quoted 10 lines
util/unicode.c new file +10 -0
+ * "\xC0\x80".
+ *
+ * If @s points to an impossible byte (0xFE or 0xFF) or a continuation
+ * byte, the sequence is invalid, and @end is set to @s + 1
+ *
+ * Else, the first byte determines how many continuation bytes are
+ * expected.  If there are fewer, the sequence is invalid, and @end is
+ * set to @s + 1 + actual number of continuation bytes.  Else, the
+ * sequence is well-formed, and @end is set to @s + 1 + expected
+ * number of continuation bytes.
Quoted 5 lines

The wording also covers (number of cont. bytes == 0), OK.

... One point that wasn't clear to me until I read the code too is that "there are fewer" covers both "out of bytes" and "byte available, but it's not a cont. byte". Subtle :)

Guilty as charged :)

Quoted 1 line

The way "*end" is set ensures progress.

Yes.

Quoted 12 lines
Quoted 11 lines

+ *
+ * A well-formed sequence is valid unless it encodes a code point
+ * outside the Unicode range U+0000..U+10FFFF, one of Unicode's 66
+ * noncharacters, a surrogate code point, or is overlong. Except the
+ * overlong sequence "\xC0\x80" is valid.
+ *
+ * Conversion succeeds if and only if the sequence is valid.
+ *
+ * Returns: the Unicode code point on success, -1 on failure.
+ */
+int mod_utf8_codepoint(const char *s, size_t n, char **end)

Quoted 5 lines

The type of "end" follows the strtol() prototype. I guess I'd prefer "const char **end", and "unsigned char" all around actually. (The input is binary garbage.) Anyway this is irrelevant. ("const char **end" would be probably quite inconvenient for the caller,
<http://www.c-faq.com/ansi/constmismatch.html>.)

C's type system is far too inexpressive to do const properly. Best we could do within the constraints, I guess. Results in tension between "declare unchanging things const" (to help compilers as well as human readers) and "avoid type casts" (because they defeat the type checker and reduce legibility).

I prefer to err on the side of avoiding casts. In particular, I try to make my interfaces so that they can be used without casts as much as possible.

Thus, I made the first argument const, but not the last. I guess similar thinking prevailed when the library part of C was constified; see strtol() precedence you quoted.

Quoted 5 lines
Quoted 5 lines
util/unicode.c new file +5 -0
+{
+    static int min_cp[5] = { 0x80, 0x800, 0x10000, 0x200000, 0x4000000 };
+    const unsigned char *p;
+    unsigned byte, mask, len, i;
+    int cp;
Quoted 8 lines

util/unicode.c line 53

Quoted 5 lines
util/unicode.c new file +5 -0
+
+    if (n == 0) {
+        *end = (char *)s;
+        return 0;
+    }

This is a special case (we return the code point U+0000 after looking at zero bytes); we can probably expect the caller to know about n==0.

We could make it an error instead. What's your gut feeling?

Quoted 23 lines

util/unicode.c line 56

Quoted 3 lines
util/unicode.c new file +3 -0
+
+    p = (const unsigned char *)s;
+    byte = *p++;

We have n>=1 bytes here, and pointing one past the last one (in case n==1) is allowed.

util/unicode.c line 58

Quoted 2 lines
util/unicode.c new file +2 -0
+    if (byte < 0x80) {
+        cp = byte;              /* one byte sequence */

So, since this is modified UTF-8, the U+0000 code point is represented with the overlong "\xC0\x80" sequence, and a bare '\0' can never be part of the Modified UTF-8 byte stream. (According to wp, '\0' is used in C and similar to zero-terminate the string, but I think that's outside the scope of the wire format.)

If we find a '\0' here, we report it as code point U+0000 instead of rejecting it. One could maybe argue that it's a violation of the interface contract (namely, due to '\0' being at offset #0, the caller should have passed in n==0), but I assume from the description that the caller need not have any idea about the contents, knowing the size should be enough.

Quoted 3 lines

IOW I'm not sure about the intended use of this function, but if the caller is allowed to throw any binary data at it (with correctly communicated size of course), then we can misreport U+0000 here. (*)

Good point.

'\0' should be treated as string terminator, exactly like reaching @n.

Let's check what my current code does:

n == 0:
set @end to @s, and return 0

n > 0 && *s == 0:
set @end to @s + 1, and return 0

Conclusion: you caught a bug. Thanks, will fix!

Quoted 4 lines

util/unicode.c line 60

Quoted 2 lines
util/unicode.c new file +2 -0
+    } else if (byte >= 0xFE) {
+        cp = -1;                /* impossible bytes 0xFE, 0xFF */

OK, binary 1111111x is as first byte misformed.

Correct.

Quoted 5 lines

util/unicode.c line 62

Quoted 2 lines
util/unicode.c new file +2 -0
+    } else if ((byte & 0x40) == 0) {
+        cp = -1;                /* unexpected continuation byte */

We know here that byte >= 0x80, and continuation bytes look like 10xxxxxx, OK.

Correct.

Quoted 10 lines

util/unicode.c line 64

Quoted 2 lines
util/unicode.c new file +2 -0
+    } else {
+        /* multi-byte sequence */

We have one of:

110xxxxx
1110xxxx
11110xxx
111110xx
1111110x

Correct.

Quoted 9 lines

util/unicode.c line 69

Quoted 5 lines
util/unicode.c new file +5 -0
+        len = 0;
+        for (mask = 0x80; byte & mask; mask >>= 1) {
+            len++;
+        }
+        assert(len > 1 && len < 7);

OK, [2..6].

(Maybe use g_assert()? :))

Now you're teasing me!

Quoted 4 lines
Quoted 1 line

+ cp = byte & (mask - 1);

OK, the only bit set in "mask" matches the leftmost clear bit in "byte". (mask-1) selects the xxxx bits in "byte".

Correct.

Quoted 3 lines
Quoted 1 line

+ for (i = 1; i < len; i++) {

Runs at least once, and as many times as we need continuation bytes. OK.

Correct.

Quoted 4 lines
Quoted 1 line

+ byte = i < n ? *p : 0;

"p" is valid to evaluate, "*p" may not be, so we check first. OK.

Quoted 8 lines

util/unicode.c line 76

Quoted 4 lines
util/unicode.c new file +4 -0
+            if ((byte & 0xC0) != 0x80) {
+                cp = -1;        /* continuation byte missing */
+                goto out;
+            }

Right, if a byte is available, it must look 10xxxxxx (binary). If we're out of bytes, we also take this branch. *end will be left one past the actual cont. bytes.

Correct.

Quoted 14 lines

util/unicode.c line 80

Quoted 4 lines
util/unicode.c new file +4 -0
+            p++;
+            cp <<= 6;
+            cp |= byte & 0x3F;
+        }

We consume the cont. byte: p++ is valid to evaluate, and we shift in the low six bits from the cont byte into the codepoint.

The loop runs at most 5 times (for len==6), shifting in (len-1)*6 bits at most, in addition to the initial cp assignment. The initial cp assignment clusters the masked-in bits at the LSB end:

    mask == (0x80 >> len) - 1
Quoted 8 lines

hence the number of masked-in bits in the original cp assignment is 7-len (which difference falls into [1..5]). So the total number of bits shifted into "cp" is

    7-len + (len-1)*6 == 1 + 5 * len

Since len <= 6, the above is <= 31, which is perfect for our "int" (int32_t in practice).

Exactly!

Quoted 18 lines

util/unicode.c line 82

Quoted 2 lines
util/unicode.c new file +2 -0
+        if (cp > 0x10FFFF) {
+            cp = -1;            /* beyond Unicode range */

OK.

util/unicode.c line 85

Quoted 3 lines
util/unicode.c new file +3 -0
+        } else if ((cp >= 0xFDD0 && cp <= 0xFDEF)
+                   || (cp & 0xFFFE) == 0xFFFE) {
+            cp = -1;            /* noncharacter */

http://en.wikipedia.org/wiki/Mapping_of_Unicode_characters#Noncharacters

Interesting. OK.

util/unicode.c line 87

Quoted 2 lines
util/unicode.c new file +2 -0
+        } else if (cp >= 0xD800 && cp <= 0xDFFF) {
+            cp = -1;            /* surrogate code point */

OK.

Quoted 13 lines

util/unicode.c line 96

Quoted 10 lines
util/unicode.c new file +9 -0
+        } else if (cp < min_cp[len - 2] && !(cp == 0 && len == 2)) {
+            cp = -1;            /* overlong, not \xC0\x80 */
+        }
+    }
+
+out:
+    *end = (char *)p;
+    return cp;
+}

I think if we want to adhere to Modified UTF-8 strictly, then (*) should be fixed. If not, I can give my Rb; this is nice code.

I think it needs fixing.

Thanks for the thorough review!

#4 lore raw

Re: [Qemu-devel] [PATCH 1/4] unicode: New mod_utf8_codepoint()

Quoted 17 lines · Markus Armbruster
Quoted 15 lines · Laszlo Ersek

util/unicode.c line 53

Quoted 10 lines · Markus Armbruster
util/unicode.c new file +10 -0
+{
+    static int min_cp[5] = { 0x80, 0x800, 0x10000, 0x200000, 0x4000000 };
+    const unsigned char *p;
+    unsigned byte, mask, len, i;
+    int cp;
+
+    if (n == 0) {
+        *end = (char *)s;
+        return 0;
+    }

This is a special case (we return the code point U+0000 after looking at zero bytes); we can probably expect the caller to know about n==0.

Quoted 1 line

We could make it an error instead. What's your gut feeling?

(If the question still stands -- maybe it doesn't any more, considering future handling of '\0':) I guess this function would be called in a loop, with increasing "s" and decreasing "n" values. "end" can only be checked after the first call. If you write a loop that checks "end" in the controlling expression, then accepting n==0 without error is useful. If you write a loop that checks "n" in the controlling expression, then refusing n==0 is OK. I'd probably write the latter kind of loop (I like pre-testing more), but I can't say in general :)

Laszlo

#5 lore raw

PATCH 2/4 check-qjson: Fix up a few bogus comments

In reply to Markus Armbruster
---
 tests/check-qjson.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
tests/check-qjson.c +2 -3
index ec85a0c..852124a 100644
@@ -4,7 +4,7 @@
  *
  * Authors:
  *  Anthony Liguori   <[email protected]>
- *  Markus Armbruster <[email protected]>,
+ *  Markus Armbruster <[email protected]>
  *
  * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
  * See the COPYING.LIB file in the top-level directory.
@@ -462,8 +462,7 @@ static void utf8_string(void)
         },
         /* 3.3.4  5-byte sequence with last byte missing (U+0000) */
         {
-            /* invalid */
-            "\"\xF8\x80\x80\x80\"", /* bug: not corrected */
+            "\"\xF8\x80\x80\x80\"",
             NULL,                   /* bug: rejected */
             "\"\\u8000\\uFFFF\"",   /* bug: want "\"\\uFFFF\"" */
             "\xF8\x80\x80\x80",
Signature · 1 line
1.7.11.7
Reviewed-by on v1 2/4
#6 lore raw

Re: [Qemu-devel] [PATCH 2/4] check-qjson: Fix up a few bogus comments

I don't understand what's going on here.

Quoted 19 lines · Markus Armbruster
---
 tests/check-qjson.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
tests/check-qjson.c +1 -1
index ec85a0c..852124a 100644
@@ -4,7 +4,7 @@
  *
  * Authors:
  *  Anthony Liguori   <[email protected]>
- *  Markus Armbruster <[email protected]>,
+ *  Markus Armbruster <[email protected]>
  *
  * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
  * See the COPYING.LIB file in the top-level directory.

tests/check-qjson.c static void utf8_string(void) · line 465

Quoted 6 lines
tests/check-qjson.c +1 -2
@@ -462,8 +462,7 @@ static void utf8_string(void)
         },
         /* 3.3.4  5-byte sequence with last byte missing (U+0000) */
         {
-            /* invalid */
-            "\"\xF8\x80\x80\x80\"", /* bug: not corrected */
+            "\"\xF8\x80\x80\x80\"",

In this test case, we use an invalid UTF-8 sequence in a JSON string literal (json_in). So "/* invalid */" could be justified; perhaps it's just too laconic.

The "/* bug: not corrected */" comment seems indeed wrong, "json_in" is *input*, there's nothing to correct on it.

tests/check-qjson.c static void utf8_string(void) · line 466

Quoted 1 line
tests/check-qjson.c +0 -0
             NULL,                   /* bug: rejected */

When the JSON parser rejects the invalid sequence, it's actually correct. So why the "bug" comment? Are we expecting (according to the comment in utf8_string()) U+FFFD?

tests/check-qjson.c static void utf8_string(void) · line 468

Quoted 2 lines
tests/check-qjson.c +0 -0
             "\"\\u8000\\uFFFF\"",   /* bug: want "\"\\uFFFF\"" */
             "\xF8\x80\x80\x80",

In this test we're trying to format a UTF-8 byte sequence (utf8_in) as a JSON string. The source is invalid. The JSON formatter should either reject it, or emit an U+FFFD in its place. The actual JSON output is probably wrong, hence the "bug" part is OK, but I thought what we expected is not U+FFFF but U+FFFD. Hence that part of the comment is wrong. ... Hm, OK the leading comment has some notes on this as well.

So what your patch does here is:
- remove the halfway OK comment "/* invalid */" -- I think it wasn't really wrong, but I won't miss it,
- removes an in fact bogus comment,
- (removes a runaway comma).

My eyes are bleeding.

#7 lore raw

Re: [Qemu-devel] [PATCH 2/4] check-qjson: Fix up a few bogus comments

Quoted 24 lines · Laszlo Ersek

I don't understand what's going on here.

Quoted 21 lines · Markus Armbruster
---
 tests/check-qjson.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
tests/check-qjson.c +1 -1
index ec85a0c..852124a 100644
@@ -4,7 +4,7 @@
  *
  * Authors:
  *  Anthony Liguori   <[email protected]>
- *  Markus Armbruster <[email protected]>,
+ *  Markus Armbruster <[email protected]>
  *
  * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
  * See the COPYING.LIB file in the top-level directory.
@@ -462,8 +462,7 @@ static void utf8_string(void)
         },
         /* 3.3.4  5-byte sequence with last byte missing (U+0000) */
Quoted 8 lines

tests/check-qjson.c static void utf8_string(void) · line 465

Quoted 4 lines
tests/check-qjson.c +1 -2
         {
-            /* invalid */
-            "\"\xF8\x80\x80\x80\"", /* bug: not corrected */
+            "\"\xF8\x80\x80\x80\"",

In this test case, we use an invalid UTF-8 sequence in a JSON string literal (json_in). So "/* invalid */" could be justified; perhaps it's just too laconic.

There are many more invalid sequences in other test cases. I have no idea why I tacked /* invalid */ to this one.

For what it's worth, the comment right above should make it clear enough that the sequence is invalid.

Quoted 2 lines

The "/* bug: not corrected */" comment seems indeed wrong, "json_in" is *input*, there's nothing to correct on it.

Exactly.

Quoted 5 lines

tests/check-qjson.c static void utf8_string(void) · line 466

Quoted 1 line
tests/check-qjson.c +0 -0
             NULL,                   /* bug: rejected */

When the JSON parser rejects the invalid sequence, it's actually correct. So why the "bug" comment? Are we expecting (according to the comment in utf8_string()) U+FFFD?

/*
* Bug markers used here:
* - bug: not corrected
* JSON parser fails to correct invalid sequence(s)
* - bug: rejected
* JSON parser rejects invalid sequence(s)
---> * We may choose to define this as feature
* - bug: want "..."
* JSON parser produces incorrect result, this is the
* correct one, assuming replacement character U+FFFF
* We may choose to reject instead of replace
*/

The comments here take care not to pass judgement on what the JSON parser should do for invalid sequences. Obviously, it should either consistently reject them, or consistently replace them with a suitable replacement character.

If I understand Anthony correctly, his advice is to always reject. That's what I intend to do should I actually get around to fixing the parser.

Quoted 9 lines

tests/check-qjson.c static void utf8_string(void) · line 468

Quoted 2 lines
tests/check-qjson.c +0 -0
             "\"\\u8000\\uFFFF\"",   /* bug: want "\"\\uFFFF\"" */
             "\xF8\x80\x80\x80",

In this test we're trying to format a UTF-8 byte sequence (utf8_in) as a JSON string. The source is invalid. The JSON formatter should either reject it, or emit an U+FFFD in its place. The actual JSON output is probably wrong, hence the "bug" part is OK, but I thought what we expected is not U+FFFF but U+FFFD. Hence that part of the comment is wrong. ... Hm, OK the leading comment has some notes on this as well.

When I wrote this test case, I thought the JSON parser *intentionally* replaced by U+FFFF. Only later I learned that FFFF comes from unintended sign extension %-}

Quoted 7 lines

So what your patch does here is:
- remove the halfway OK comment "/* invalid */" -- I think it wasn't really wrong, but I won't miss it,
- removes an in fact bogus comment,
- (removes a runaway comma).

My eyes are bleeding.

/me hands over tissues

Quoted 1 line

Thanks!

#8 lore raw

PATCH 3/4 check-qjson: Test noncharacters other than U+FFFE, U+FFFF in strings

In reply to Markus Armbruster

These are all broken, too.

A few test cases use noncharacters U+FFFF and U+10FFFF. Risks testing noncharacters some more instead of what they're supposed to test. Use U+FFFD and U+10FFFD instead.

---
 tests/check-qjson.c | 85 +++++++++++++++++++++++++++++++++++++++++++++--------
 1 file changed, 72 insertions(+), 13 deletions(-)
tests/check-qjson.c +72 -13
index 852124a..efec1b2 100644
@@ -158,7 +158,7 @@ static void utf8_string(void)
      * consider using overlong encoding \xC0\x80 for U+0000 ("modified
      * UTF-8").
      *
-     * Test cases are scraped from Markus Kuhn's UTF-8 decoder
+     * Most test cases are scraped from Markus Kuhn's UTF-8 decoder
      * capability and stress test at
      * http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt
      */
@@ -256,11 +256,11 @@ static void utf8_string(void)
             "\xDF\xBF",
             "\"\\u07FF\"",
         },
-        /* 2.2.3  3 bytes U+FFFF */
+        /* 2.2.3  3 bytes U+FFFD */
         {
-            "\"\xEF\xBF\xBF\"",
-            "\xEF\xBF\xBF",
-            "\"\\uFFFF\"",
+            "\"\xEF\xBF\xBD\"",
+            "\xEF\xBF\xBD",
+            "\"\\uFFFD\"",
         },
         /* 2.2.4  4 bytes U+1FFFFF */
         {
@@ -303,10 +303,10 @@ static void utf8_string(void)
             "\"\\uFFFD\"",
         },
         {
-            /* U+10FFFF */
-            "\"\xF4\x8F\xBF\xBF\"",
-            "\xF4\x8F\xBF\xBF",
-            "\"\\u43FF\\uFFFF\"", /* bug: want "\"\\uDBFF\\uDFFF\"" */
+            /* U+10FFFD */
+            "\"\xF4\x8F\xBF\xBD\"",
+            "\xF4\x8F\xBF\xBD",
+            "\"\\u43FF\\uFFFF\"", /* bug: want "\"\\uDBFF\\uDFFD\"" */
         },
         {
             /* U+110000 */
@@ -584,9 +584,9 @@ static void utf8_string(void)
             "\"\\u07FF\"",
         },
         {
-            /* \U+FFFF */
-            "\"\xF0\x8F\xBF\xBF\"",
-            "\xF0\x8F\xBF\xBF",   /* bug: not corrected */
+            /* \U+FFFD */
+            "\"\xF0\x8F\xBF\xBD\"",
+            "\xF0\x8F\xBF\xBD",   /* bug: not corrected */
             "\"\\u03FF\\uFFFF\"", /* bug: want "\"\\uFFFF\"" */
         },
         {
@@ -731,6 +731,7 @@ static void utf8_string(void)
             "\"\\uDBFF\\uDFFF\"", /* bug: want "\"\\uFFFF\\uFFFF\"" */
         },
         /* 5.3  Other illegal code positions */
+        /* BMP noncharacters */
         {
             /* \U+FFFE */
             "\"\xEF\xBF\xBE\"",
@@ -741,7 +742,65 @@ static void utf8_string(void)
             /* \U+FFFF */
             "\"\xEF\xBF\xBF\"",
             "\xEF\xBF\xBF",     /* bug: not corrected */
-            "\"\\uFFFF\"",      /* bug: not corrected */
+            "\"\\uFFFF\"",
+        },
+        {
+            /* U+FDD0 */
+            "\"\xEF\xB7\x90\"",
+            "\xEF\xB7\x90",     /* bug: not corrected */
+            "\"\\uFDD0\"",      /* bug: not corrected */
+        },
+        {
+            /* U+FDEF */
+            "\"\xEF\xB7\xAF\"",
+            "\xEF\xB7\xAF",     /* bug: not corrected */
+            "\"\\uFDEF\"",      /* bug: not corrected */
+        },
+        /* Plane 1 .. 16 noncharacters */
+        {
+            /* U+1FFFE U+1FFFF U+2FFFE U+2FFFF ... U+10FFFE U+10FFFF */
+            "\"\xF0\x9F\xBF\xBE\xF0\x9F\xBF\xBF"
+            "\xF0\xAF\xBF\xBE\xF0\xAF\xBF\xBF"
+            "\xF0\xBF\xBF\xBE\xF0\xBF\xBF\xBF"
+            "\xF1\x8F\xBF\xBE\xF1\x8F\xBF\xBF"
+            "\xF1\x9F\xBF\xBE\xF1\x9F\xBF\xBF"
+            "\xF1\xAF\xBF\xBE\xF1\xAF\xBF\xBF"
+            "\xF1\xBF\xBF\xBE\xF1\xBF\xBF\xBF"
+            "\xF2\x8F\xBF\xBE\xF2\x8F\xBF\xBF"
+            "\xF2\x9F\xBF\xBE\xF2\x9F\xBF\xBF"
+            "\xF2\xAF\xBF\xBE\xF2\xAF\xBF\xBF"
+            "\xF2\xBF\xBF\xBE\xF2\xBF\xBF\xBF"
+            "\xF3\x8F\xBF\xBE\xF3\x8F\xBF\xBF"
+            "\xF3\x9F\xBF\xBE\xF3\x9F\xBF\xBF"
+            "\xF3\xAF\xBF\xBE\xF3\xAF\xBF\xBF"
+            "\xF3\xBF\xBF\xBE\xF3\xBF\xBF\xBF"
+            "\xF4\x8F\xBF\xBE\xF4\x8F\xBF\xBF\"",
+            /* bug: not corrected */
+            "\xF0\x9F\xBF\xBE\xF0\x9F\xBF\xBF"
+            "\xF0\xAF\xBF\xBE\xF0\xAF\xBF\xBF"
+            "\xF0\xBF\xBF\xBE\xF0\xBF\xBF\xBF"
+            "\xF1\x8F\xBF\xBE\xF1\x8F\xBF\xBF"
+            "\xF1\x9F\xBF\xBE\xF1\x9F\xBF\xBF"
+            "\xF1\xAF\xBF\xBE\xF1\xAF\xBF\xBF"
+            "\xF1\xBF\xBF\xBE\xF1\xBF\xBF\xBF"
+            "\xF2\x8F\xBF\xBE\xF2\x8F\xBF\xBF"
+            "\xF2\x9F\xBF\xBE\xF2\x9F\xBF\xBF"
+            "\xF2\xAF\xBF\xBE\xF2\xAF\xBF\xBF"
+            "\xF2\xBF\xBF\xBE\xF2\xBF\xBF\xBF"
+            "\xF3\x8F\xBF\xBE\xF3\x8F\xBF\xBF"
+            "\xF3\x9F\xBF\xBE\xF3\x9F\xBF\xBF"
+            "\xF3\xAF\xBF\xBE\xF3\xAF\xBF\xBF"
+            "\xF3\xBF\xBF\xBE\xF3\xBF\xBF\xBF"
+            "\xF4\x8F\xBF\xBE\xF4\x8F\xBF\xBF",
+            /* bug: not corrected */
+            "\"\\u07FF\\uFFFF\\u07FF\\uFFFF\\u0BFF\\uFFFF\\u0BFF\\uFFFF"
+            "\\u0FFF\\uFFFF\\u0FFF\\uFFFF\\u13FF\\uFFFF\\u13FF\\uFFFF"
+            "\\u17FF\\uFFFF\\u17FF\\uFFFF\\u1BFF\\uFFFF\\u1BFF\\uFFFF"
+            "\\u1FFF\\uFFFF\\u1FFF\\uFFFF\\u23FF\\uFFFF\\u23FF\\uFFFF"
+            "\\u27FF\\uFFFF\\u27FF\\uFFFF\\u2BFF\\uFFFF\\u2BFF\\uFFFF"
+            "\\u2FFF\\uFFFF\\u2FFF\\uFFFF\\u33FF\\uFFFF\\u33FF\\uFFFF"
+            "\\u37FF\\uFFFF\\u37FF\\uFFFF\\u3BFF\\uFFFF\\u3BFF\\uFFFF"
+            "\\u3FFF\\uFFFF\\u3FFF\\uFFFF\\u43FF\\uFFFF\\u43FF\\uFFFF\"",
         },
         {}
     };
Signature · 1 line
1.7.11.7
Reviewed-by on v1 3/4
#9 lore raw

Re: [Qemu-devel] [PATCH 3/4] check-qjson: Test noncharacters other than U+FFFE, U+FFFF in strings

Quoted 1 line · Markus Armbruster

These are all broken, too.

What are "these"? And how are they broken? And how does the patch fix them?

Quoted 9 lines

A few test cases use noncharacters U+FFFF and U+10FFFF. Risks testing noncharacters some more instead of what they're supposed to test. Use U+FFFD and U+10FFFD instead.

---
 tests/check-qjson.c | 85 +++++++++++++++++++++++++++++++++++++++++++++--------
 1 file changed, 72 insertions(+), 13 deletions(-)

I'm confused about the commit message. There are three paragraphs in it (the title, the first paragraph, and the 2nd paragraph). This patch modifies different tests:

Quoted 17 lines
tests/check-qjson.c +1 -1
index 852124a..efec1b2 100644
@@ -158,7 +158,7 @@ static void utf8_string(void)
      * consider using overlong encoding \xC0\x80 for U+0000 ("modified
      * UTF-8").
      *
-     * Test cases are scraped from Markus Kuhn's UTF-8 decoder
+     * Most test cases are scraped from Markus Kuhn's UTF-8 decoder
      * capability and stress test at
      * http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt
      */
@@ -256,11 +256,11 @@ static void utf8_string(void)
             "\xDF\xBF",
             "\"\\u07FF\"",
         },

tests/check-qjson.c static void utf8_string(void) · line 264

Quoted 10 lines
tests/check-qjson.c +4 -4
-        /* 2.2.3  3 bytes U+FFFF */
+        /* 2.2.3  3 bytes U+FFFD */
         {
-            "\"\xEF\xBF\xBF\"",
-            "\xEF\xBF\xBF",
-            "\"\\uFFFF\"",
+            "\"\xEF\xBF\xBD\"",
+            "\xEF\xBF\xBD",
+            "\"\\uFFFD\"",
         },

This is under "2.2 Last possible sequence of a certain length". I guess this is where you say "last possible sequence of a certain length, encoding a character (= non-noncharacter)". OK, p#2.

Quoted 7 lines
tests/check-qjson.c +0 -1
         /* 2.2.4  4 bytes U+1FFFFF */
         {
@@ -303,10 +303,10 @@ static void utf8_string(void)
             "\"\\uFFFD\"",
         },
         {
-            /* U+10FFFF */

tests/check-qjson.c static void utf8_string(void) · line 312

Quoted 10 lines
tests/check-qjson.c +4 -3
-            "\"\xF4\x8F\xBF\xBF\"",
-            "\xF4\x8F\xBF\xBF",
-            "\"\\u43FF\\uFFFF\"", /* bug: want "\"\\uDBFF\\uDFFF\"" */
+            /* U+10FFFD */
+            "\"\xF4\x8F\xBF\xBD\"",
+            "\xF4\x8F\xBF\xBD",
+            "\"\\u43FF\\uFFFF\"", /* bug: want "\"\\uDBFF\\uDFFD\"" */
         },
         {
             /* U+110000 */

Under "2.3 Other boundary conditions". Not a non-character any longer, but also not a boundary condition. At least not the original one. Still covered by the ...FFFD part of the commit message, p#2.

tests/check-qjson.c static void utf8_string(void) · line 592

Quoted 13 lines
tests/check-qjson.c +3 -3
@@ -584,9 +584,9 @@ static void utf8_string(void)
             "\"\\u07FF\"",
         },
         {
-            /* \U+FFFF */
-            "\"\xF0\x8F\xBF\xBF\"",
-            "\xF0\x8F\xBF\xBF",   /* bug: not corrected */
+            /* \U+FFFD */
+            "\"\xF0\x8F\xBF\xBD\"",
+            "\xF0\x8F\xBF\xBD",   /* bug: not corrected */
             "\"\\u03FF\\uFFFF\"", /* bug: want "\"\\uFFFF\"" */
         },
         {

Under "4.2 Maximum overlong sequences". What does that even mean? "In some sense maximum codepoints, all represented as overlong sequences"? P#2.

Quoted 66 lines
tests/check-qjson.c +53 -1
@@ -731,6 +731,7 @@ static void utf8_string(void)
             "\"\\uDBFF\\uDFFF\"", /* bug: want "\"\\uFFFF\\uFFFF\"" */
         },
         /* 5.3  Other illegal code positions */
+        /* BMP noncharacters */
         {
             /* \U+FFFE */
             "\"\xEF\xBF\xBE\"",
@@ -741,7 +742,65 @@ static void utf8_string(void)
             /* \U+FFFF */
             "\"\xEF\xBF\xBF\"",
             "\xEF\xBF\xBF",     /* bug: not corrected */
-            "\"\\uFFFF\"",      /* bug: not corrected */
+            "\"\\uFFFF\"",
+        },
+        {
+            /* U+FDD0 */
+            "\"\xEF\xB7\x90\"",
+            "\xEF\xB7\x90",     /* bug: not corrected */
+            "\"\\uFDD0\"",      /* bug: not corrected */
+        },
+        {
+            /* U+FDEF */
+            "\"\xEF\xB7\xAF\"",
+            "\xEF\xB7\xAF",     /* bug: not corrected */
+            "\"\\uFDEF\"",      /* bug: not corrected */
+        },
+        /* Plane 1 .. 16 noncharacters */
+        {
+            /* U+1FFFE U+1FFFF U+2FFFE U+2FFFF ... U+10FFFE U+10FFFF */
+            "\"\xF0\x9F\xBF\xBE\xF0\x9F\xBF\xBF"
+            "\xF0\xAF\xBF\xBE\xF0\xAF\xBF\xBF"
+            "\xF0\xBF\xBF\xBE\xF0\xBF\xBF\xBF"
+            "\xF1\x8F\xBF\xBE\xF1\x8F\xBF\xBF"
+            "\xF1\x9F\xBF\xBE\xF1\x9F\xBF\xBF"
+            "\xF1\xAF\xBF\xBE\xF1\xAF\xBF\xBF"
+            "\xF1\xBF\xBF\xBE\xF1\xBF\xBF\xBF"
+            "\xF2\x8F\xBF\xBE\xF2\x8F\xBF\xBF"
+            "\xF2\x9F\xBF\xBE\xF2\x9F\xBF\xBF"
+            "\xF2\xAF\xBF\xBE\xF2\xAF\xBF\xBF"
+            "\xF2\xBF\xBF\xBE\xF2\xBF\xBF\xBF"
+            "\xF3\x8F\xBF\xBE\xF3\x8F\xBF\xBF"
+            "\xF3\x9F\xBF\xBE\xF3\x9F\xBF\xBF"
+            "\xF3\xAF\xBF\xBE\xF3\xAF\xBF\xBF"
+            "\xF3\xBF\xBF\xBE\xF3\xBF\xBF\xBF"
+            "\xF4\x8F\xBF\xBE\xF4\x8F\xBF\xBF\"",
+            /* bug: not corrected */
+            "\xF0\x9F\xBF\xBE\xF0\x9F\xBF\xBF"
+            "\xF0\xAF\xBF\xBE\xF0\xAF\xBF\xBF"
+            "\xF0\xBF\xBF\xBE\xF0\xBF\xBF\xBF"
+            "\xF1\x8F\xBF\xBE\xF1\x8F\xBF\xBF"
+            "\xF1\x9F\xBF\xBE\xF1\x9F\xBF\xBF"
+            "\xF1\xAF\xBF\xBE\xF1\xAF\xBF\xBF"
+            "\xF1\xBF\xBF\xBE\xF1\xBF\xBF\xBF"
+            "\xF2\x8F\xBF\xBE\xF2\x8F\xBF\xBF"
+            "\xF2\x9F\xBF\xBE\xF2\x9F\xBF\xBF"
+            "\xF2\xAF\xBF\xBE\xF2\xAF\xBF\xBF"
+            "\xF2\xBF\xBF\xBE\xF2\xBF\xBF\xBF"
+            "\xF3\x8F\xBF\xBE\xF3\x8F\xBF\xBF"
+            "\xF3\x9F\xBF\xBE\xF3\x9F\xBF\xBF"
+            "\xF3\xAF\xBF\xBE\xF3\xAF\xBF\xBF"
+            "\xF3\xBF\xBF\xBE\xF3\xBF\xBF\xBF"
+            "\xF4\x8F\xBF\xBE\xF4\x8F\xBF\xBF",
+            /* bug: not corrected */
+            "\"\\u07FF\\uFFFF\\u07FF\\uFFFF\\u0BFF\\uFFFF\\u0BFF\\uFFFF"

tests/check-qjson.c static void utf8_string(void) · line 806

Quoted 10 lines
tests/check-qjson.c +7 -0
+            "\\u0FFF\\uFFFF\\u0FFF\\uFFFF\\u13FF\\uFFFF\\u13FF\\uFFFF"
+            "\\u17FF\\uFFFF\\u17FF\\uFFFF\\u1BFF\\uFFFF\\u1BFF\\uFFFF"
+            "\\u1FFF\\uFFFF\\u1FFF\\uFFFF\\u23FF\\uFFFF\\u23FF\\uFFFF"
+            "\\u27FF\\uFFFF\\u27FF\\uFFFF\\u2BFF\\uFFFF\\u2BFF\\uFFFF"
+            "\\u2FFF\\uFFFF\\u2FFF\\uFFFF\\u33FF\\uFFFF\\u33FF\\uFFFF"
+            "\\u37FF\\uFFFF\\u37FF\\uFFFF\\u3BFF\\uFFFF\\u3BFF\\uFFFF"
+            "\\u3FFF\\uFFFF\\u3FFF\\uFFFF\\u43FF\\uFFFF\\u43FF\\uFFFF\"",
         },
         {}
     };

This is probably p#0 (the title).

Ah. Have you removed the noncharacters from the other tests, but made up for them at the end with new noncharacter tests?

#10 lore raw

Re: [Qemu-devel] [PATCH 3/4] check-qjson: Test noncharacters other than U+FFFE, U+FFFF in strings

Quoted 4 lines · Laszlo Ersek
Quoted 1 line · Markus Armbruster

These are all broken, too.

What are "these"? And how are they broken? And how does the patch fix them?

"These" refers to the subject: noncharacters other than U+FFFE, U+FFFF.

I agree that I should better explain how they're broken, and what the patch does to fix them. Will fix on respin.

Quoted 36 lines
Quoted 10 lines

A few test cases use noncharacters U+FFFF and U+10FFFF. Risks testing noncharacters some more instead of what they're supposed to test. Use U+FFFD and U+10FFFD instead.

---
 tests/check-qjson.c | 85
+++++++++++++++++++++++++++++++++++++++++++++--------
 1 file changed, 72 insertions(+), 13 deletions(-)

I'm confused about the commit message. There are three paragraphs in it (the title, the first paragraph, and the 2nd paragraph). This patch modifies different tests:

Quoted 21 lines
tests/check-qjson.c +2 -3
index 852124a..efec1b2 100644
@@ -158,7 +158,7 @@ static void utf8_string(void)
      * consider using overlong encoding \xC0\x80 for U+0000 ("modified
      * UTF-8").
      *
-     * Test cases are scraped from Markus Kuhn's UTF-8 decoder
+     * Most test cases are scraped from Markus Kuhn's UTF-8 decoder
      * capability and stress test at
      * http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt
      */
@@ -256,11 +256,11 @@ static void utf8_string(void)
             "\xDF\xBF",
             "\"\\u07FF\"",
         },
-        /* 2.2.3  3 bytes U+FFFF */
+        /* 2.2.3  3 bytes U+FFFD */
         {
-            "\"\xEF\xBF\xBF\"",
Quoted 8 lines

tests/check-qjson.c static void utf8_string(void) · line 264

Quoted 6 lines
tests/check-qjson.c +3 -2
-            "\xEF\xBF\xBF",
-            "\"\\uFFFF\"",
+            "\"\xEF\xBF\xBD\"",
+            "\xEF\xBF\xBD",
+            "\"\\uFFFD\"",
         },

This is under "2.2 Last possible sequence of a certain length". I guess

Which is in turn under "2 Boundary condition test cases".

Quoted 2 lines

this is where you say "last possible sequence of a certain length, encoding a character (= non-noncharacter)". OK, p#2.

Yes.

The test's purpose is testing the upper bound of 3-byte sequences is decoded correctly.

The upper bound is U+FFFF. Since that's a noncharacter, the parser should reject it (or maybe replace), the formatter should replace it. Trouble is it could be misdecoded and then rejected / replaced.

Besides, U+FFFF already gets tested along with the other noncharacters under "5.3 Other illegal code positions".

Next in line is U+FFFE, also a noncharacter, also under 5.3.

Next in line is U+FFFD, which I picked.

But that gets tested under "2.3 Other boundary conditions"! I guess I either drop it there, or make this one U+FFFC.

I think testing U+FFFC here makes sense, because U+FFFD could be misdecoded, then replaced by U+FFFD.

What do you think?

Quoted 13 lines
Quoted 13 lines
tests/check-qjson.c +3 -4
         /* 2.2.4  4 bytes U+1FFFFF */
         {
@@ -303,10 +303,10 @@ static void utf8_string(void)
             "\"\\uFFFD\"",
         },
         {
-            /* U+10FFFF */
-            "\"\xF4\x8F\xBF\xBF\"",
-            "\xF4\x8F\xBF\xBF",
-            "\"\\u43FF\\uFFFF\"", /* bug: want "\"\\uDBFF\\uDFFF\"" */
+            /* U+10FFFD */
+            "\"\xF4\x8F\xBF\xBD\"",
+            "\xF4\x8F\xBF\xBD",
Quoted 8 lines

tests/check-qjson.c static void utf8_string(void) · line 312

Quoted 4 lines
tests/check-qjson.c +1 -0
+            "\"\\u43FF\\uFFFF\"", /* bug: want "\"\\uDBFF\\uDFFD\"" */
         },
         {
             /* U+110000 */

Under "2.3 Other boundary conditions". Not a non-character any longer, but also not a boundary condition. At least not the original one. Still covered by the ...FFFD part of the commit message, p#2.

The test's purpose is testing the upper bound of the Unicode range gets decoded correctly.

The upper bound is U+10FFFF. Since that's a noncharacter... same argument as above.

Next in line is U+10FFFE, also a noncharacter.

Next in line is U+10FFFD, which I picked.

Quoted 8 lines
Quoted 8 lines
tests/check-qjson.c +1 -3
@@ -584,9 +584,9 @@ static void utf8_string(void)
             "\"\\u07FF\"",
         },
         {
-            /* \U+FFFF */
-            "\"\xF0\x8F\xBF\xBF\"",
-            "\xF0\x8F\xBF\xBF",   /* bug: not corrected */
+            /* \U+FFFD */
Quoted 8 lines

tests/check-qjson.c static void utf8_string(void) · line 592

Quoted 5 lines
tests/check-qjson.c +2 -0
+            "\"\xF0\x8F\xBF\xBD\"",
+            "\xF0\x8F\xBF\xBD",   /* bug: not corrected */
             "\"\\u03FF\\uFFFF\"", /* bug: want "\"\\uFFFF\"" */
         },
         {

Under "4.2 Maximum overlong sequences". What does that even mean? "In some sense maximum codepoints, all represented as overlong sequences"? P#2.

The headings are all stolen from the same source as the test cases. Perhaps I should steal more of the explanatory text there as well.

The one for 4.2 is:

Below you see the highest Unicode value that is still resulting in an overlong sequence if represented with the given number of bytes. This is a boundary test for safe UTF-8 decoders. All five characters should be rejected like malformed UTF-8 sequences.

Quoted 79 lines
Quoted 66 lines
tests/check-qjson.c +53 -1
@@ -731,6 +731,7 @@ static void utf8_string(void)
             "\"\\uDBFF\\uDFFF\"", /* bug: want "\"\\uFFFF\\uFFFF\"" */
         },
         /* 5.3  Other illegal code positions */
+        /* BMP noncharacters */
         {
             /* \U+FFFE */
             "\"\xEF\xBF\xBE\"",
@@ -741,7 +742,65 @@ static void utf8_string(void)
             /* \U+FFFF */
             "\"\xEF\xBF\xBF\"",
             "\xEF\xBF\xBF",     /* bug: not corrected */
-            "\"\\uFFFF\"",      /* bug: not corrected */
+            "\"\\uFFFF\"",
+        },
+        {
+            /* U+FDD0 */
+            "\"\xEF\xB7\x90\"",
+            "\xEF\xB7\x90",     /* bug: not corrected */
+            "\"\\uFDD0\"",      /* bug: not corrected */
+        },
+        {
+            /* U+FDEF */
+            "\"\xEF\xB7\xAF\"",
+            "\xEF\xB7\xAF",     /* bug: not corrected */
+            "\"\\uFDEF\"",      /* bug: not corrected */
+        },
+        /* Plane 1 .. 16 noncharacters */
+        {
+            /* U+1FFFE U+1FFFF U+2FFFE U+2FFFF ... U+10FFFE U+10FFFF */
+            "\"\xF0\x9F\xBF\xBE\xF0\x9F\xBF\xBF"
+            "\xF0\xAF\xBF\xBE\xF0\xAF\xBF\xBF"
+            "\xF0\xBF\xBF\xBE\xF0\xBF\xBF\xBF"
+            "\xF1\x8F\xBF\xBE\xF1\x8F\xBF\xBF"
+            "\xF1\x9F\xBF\xBE\xF1\x9F\xBF\xBF"
+            "\xF1\xAF\xBF\xBE\xF1\xAF\xBF\xBF"
+            "\xF1\xBF\xBF\xBE\xF1\xBF\xBF\xBF"
+            "\xF2\x8F\xBF\xBE\xF2\x8F\xBF\xBF"
+            "\xF2\x9F\xBF\xBE\xF2\x9F\xBF\xBF"
+            "\xF2\xAF\xBF\xBE\xF2\xAF\xBF\xBF"
+            "\xF2\xBF\xBF\xBE\xF2\xBF\xBF\xBF"
+            "\xF3\x8F\xBF\xBE\xF3\x8F\xBF\xBF"
+            "\xF3\x9F\xBF\xBE\xF3\x9F\xBF\xBF"
+            "\xF3\xAF\xBF\xBE\xF3\xAF\xBF\xBF"
+            "\xF3\xBF\xBF\xBE\xF3\xBF\xBF\xBF"
+            "\xF4\x8F\xBF\xBE\xF4\x8F\xBF\xBF\"",
+            /* bug: not corrected */
+            "\xF0\x9F\xBF\xBE\xF0\x9F\xBF\xBF"
+            "\xF0\xAF\xBF\xBE\xF0\xAF\xBF\xBF"
+            "\xF0\xBF\xBF\xBE\xF0\xBF\xBF\xBF"
+            "\xF1\x8F\xBF\xBE\xF1\x8F\xBF\xBF"
+            "\xF1\x9F\xBF\xBE\xF1\x9F\xBF\xBF"
+            "\xF1\xAF\xBF\xBE\xF1\xAF\xBF\xBF"
+            "\xF1\xBF\xBF\xBE\xF1\xBF\xBF\xBF"
+            "\xF2\x8F\xBF\xBE\xF2\x8F\xBF\xBF"
+            "\xF2\x9F\xBF\xBE\xF2\x9F\xBF\xBF"
+            "\xF2\xAF\xBF\xBE\xF2\xAF\xBF\xBF"
+            "\xF2\xBF\xBF\xBE\xF2\xBF\xBF\xBF"
+            "\xF3\x8F\xBF\xBE\xF3\x8F\xBF\xBF"
+            "\xF3\x9F\xBF\xBE\xF3\x9F\xBF\xBF"
+            "\xF3\xAF\xBF\xBE\xF3\xAF\xBF\xBF"
+            "\xF3\xBF\xBF\xBE\xF3\xBF\xBF\xBF"
+            "\xF4\x8F\xBF\xBE\xF4\x8F\xBF\xBF",
+            /* bug: not corrected */
+            "\"\\u07FF\\uFFFF\\u07FF\\uFFFF\\u0BFF\\uFFFF\\u0BFF\\uFFFF"

tests/check-qjson.c static void utf8_string(void) · line 806

Quoted 10 lines
tests/check-qjson.c +7 -0
+            "\\u0FFF\\uFFFF\\u0FFF\\uFFFF\\u13FF\\uFFFF\\u13FF\\uFFFF"
+            "\\u17FF\\uFFFF\\u17FF\\uFFFF\\u1BFF\\uFFFF\\u1BFF\\uFFFF"
+            "\\u1FFF\\uFFFF\\u1FFF\\uFFFF\\u23FF\\uFFFF\\u23FF\\uFFFF"
+            "\\u27FF\\uFFFF\\u27FF\\uFFFF\\u2BFF\\uFFFF\\u2BFF\\uFFFF"
+            "\\u2FFF\\uFFFF\\u2FFF\\uFFFF\\u33FF\\uFFFF\\u33FF\\uFFFF"
+            "\\u37FF\\uFFFF\\u37FF\\uFFFF\\u3BFF\\uFFFF\\u3BFF\\uFFFF"
+            "\\u3FFF\\uFFFF\\u3FFF\\uFFFF\\u43FF\\uFFFF\\u43FF\\uFFFF\"",
         },
         {}
     };

This is probably p#0 (the title).

Quoted 2 lines

Ah. Have you removed the noncharacters from the other tests, but made up for them at the end with new noncharacter tests?

I added tests to cover all 66 noncharacters. Then I noticed some duplicate test cases elsewhere, and realized that these don't really fully test what I want to test there.

Quoted 1 line

Thanks!

#11 lore raw

Re: [Qemu-devel] [PATCH 3/4] check-qjson: Test noncharacters other than U+FFFE, U+FFFF in strings

Quoted 78 lines · Markus Armbruster
Quoted 4 lines · Laszlo Ersek
Quoted 1 line · Markus Armbruster

These are all broken, too.

What are "these"? And how are they broken? And how does the patch fix them?

"These" refers to the subject: noncharacters other than U+FFFE, U+FFFF.

I agree that I should better explain how they're broken, and what the patch does to fix them. Will fix on respin.

Quoted 36 lines
Quoted 10 lines

A few test cases use noncharacters U+FFFF and U+10FFFF. Risks testing noncharacters some more instead of what they're supposed to test. Use U+FFFD and U+10FFFD instead.

---
 tests/check-qjson.c | 85
+++++++++++++++++++++++++++++++++++++++++++++--------
 1 file changed, 72 insertions(+), 13 deletions(-)

I'm confused about the commit message. There are three paragraphs in it (the title, the first paragraph, and the 2nd paragraph). This patch modifies different tests:

Quoted 21 lines
tests/check-qjson.c +2 -3
index 852124a..efec1b2 100644
@@ -158,7 +158,7 @@ static void utf8_string(void)
      * consider using overlong encoding \xC0\x80 for U+0000 ("modified
      * UTF-8").
      *
-     * Test cases are scraped from Markus Kuhn's UTF-8 decoder
+     * Most test cases are scraped from Markus Kuhn's UTF-8 decoder
      * capability and stress test at
      * http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt
      */
@@ -256,11 +256,11 @@ static void utf8_string(void)
             "\xDF\xBF",
             "\"\\u07FF\"",
         },
-        /* 2.2.3  3 bytes U+FFFF */
+        /* 2.2.3  3 bytes U+FFFD */
         {
-            "\"\xEF\xBF\xBF\"",
Quoted 8 lines

tests/check-qjson.c static void utf8_string(void) · line 264

Quoted 6 lines
tests/check-qjson.c +3 -2
-            "\xEF\xBF\xBF",
-            "\"\\uFFFF\"",
+            "\"\xEF\xBF\xBD\"",
+            "\xEF\xBF\xBD",
+            "\"\\uFFFD\"",
         },

This is under "2.2 Last possible sequence of a certain length". I guess

Which is in turn under "2 Boundary condition test cases".

Quoted 2 lines

this is where you say "last possible sequence of a certain length, encoding a character (= non-noncharacter)". OK, p#2.

Yes.

The test's purpose is testing the upper bound of 3-byte sequences is decoded correctly.

The upper bound is U+FFFF. Since that's a noncharacter, the parser should reject it (or maybe replace), the formatter should replace it. Trouble is it could be misdecoded and then rejected / replaced.

Besides, U+FFFF already gets tested along with the other noncharacters under "5.3 Other illegal code positions".

Next in line is U+FFFE, also a noncharacter, also under 5.3.

Next in line is U+FFFD, which I picked.

Quoted 7 lines

But that gets tested under "2.3 Other boundary conditions"! I guess I either drop it there, or make this one U+FFFC.

I think testing U+FFFC here makes sense, because U+FFFD could be misdecoded, then replaced by U+FFFD.

What do you think?

I think that we're extending Markus Kuhn's test suite, basically taking random shots at where one specific parser's/formatter's weak spots might be :)

That said, with intelligent fuzzing out of scope / capacity, U+FFFC could be a good pick.

I also think I'm a quite a useless person to ask for thoughts in this area :)

Thanks,
Laszlo

#12 lore raw

PATCH 4/4 qjson: to_json() case QTYPE_QSTRING is buggy, rewrite

In reply to Markus Armbruster

Known bugs in to_json():

* A start byte for a three-byte sequence followed by less than two continuation bytes is split into one-byte sequences.

* Start bytes for sequences longer than three bytes get misinterpreted as start bytes for three-byte sequences. Continuation bytes beyond byte three become one-byte sequences.

This means all characters outside the BMP are decoded incorrectly.

* One-byte sequences with the MSB are put into the JSON string verbatim when char is unsigned, producing invalid UTF-8. When char is signed, they're replaced by "\\uFFFF" instead.

This includes \xFE, \xFF, and stray continuation bytes.

* Overlong sequences are happily accepted, unless screwed up by the bugs above.

* Likewise, sequences encoding surrogate code points or noncharacters.

* Unlike other control characters, ASCII DEL is not escaped. Except in overlong encodings.

My rewrite fixes them as follows:

* Malformed UTF-8 sequences are replaced.

Except the overlong encoding \xC0\x80 of U+0000 is still accepted. Permits embedding NUL characters in C strings. This trick is known as "Modified UTF-8".

* Sequences encoding code points beyond Unicode range are replaced.

* Sequences encoding code points beyond the BMP produce a surrogate pair.

* Sequences encoding surrogate code points are replaced.

* Sequences encoding noncharacters are replaced.

* ASCII DEL is now always escaped.

The replacement character is U+FFFD.

---
 qobject/qjson.c     | 102 +++++++++++--------------
 tests/check-qjson.c | 216 ++++++++++++++++++++++++----------------------------
 2 files changed, 145 insertions(+), 173 deletions(-)
qobject/qjson.c +45 -57
index 83a6b4f..19085a1 100644
@@ -136,68 +136,56 @@ static void to_json(const QObject *obj, QString *str, int pretty, int indent)
     case QTYPE_QSTRING: {
         QString *val = qobject_to_qstring(obj);
         const char *ptr;
+        int cp;
+        char buf[16];
+        char *end;
 
         ptr = qstring_get_str(val);
         qstring_append(str, "\"");
-        while (*ptr) {
-            if ((ptr[0] & 0xE0) == 0xE0 &&
-                (ptr[1] & 0x80) && (ptr[2] & 0x80)) {
-                uint16_t wchar;
-                char escape[7];
-
-                wchar  = (ptr[0] & 0x0F) << 12;
-                wchar |= (ptr[1] & 0x3F) << 6;
-                wchar |= (ptr[2] & 0x3F);
-                ptr += 2;
-
-                snprintf(escape, sizeof(escape), "\\u%04X", wchar);
-                qstring_append(str, escape);
-            } else if ((ptr[0] & 0xE0) == 0xC0 && (ptr[1] & 0x80)) {
-                uint16_t wchar;
-                char escape[7];
-
-                wchar  = (ptr[0] & 0x1F) << 6;
-                wchar |= (ptr[1] & 0x3F);
-                ptr++;
-
-                snprintf(escape, sizeof(escape), "\\u%04X", wchar);
-                qstring_append(str, escape);
-            } else switch (ptr[0]) {
-                case '\"':
-                    qstring_append(str, "\\\"");
-                    break;
-                case '\\':
-                    qstring_append(str, "\\\\");
-                    break;
-                case '\b':
-                    qstring_append(str, "\\b");
-                    break;
-                case '\f':
-                    qstring_append(str, "\\f");
-                    break;
-                case '\n':
-                    qstring_append(str, "\\n");
-                    break;
-                case '\r':
-                    qstring_append(str, "\\r");
-                    break;
-                case '\t':
-                    qstring_append(str, "\\t");
-                    break;
-                default: {
-                    if (ptr[0] <= 0x1F) {
-                        char escape[7];
-                        snprintf(escape, sizeof(escape), "\\u%04X", ptr[0]);
-                        qstring_append(str, escape);
-                    } else {
-                        char buf[2] = { ptr[0], 0 };
-                        qstring_append(str, buf);
-                    }
-                    break;
+
+        for (; *ptr; ptr = end) {
+            cp = mod_utf8_codepoint(ptr, 6, &end);
+            switch (cp) {
+            case '\"':
+                qstring_append(str, "\\\"");
+                break;
+            case '\\':
+                qstring_append(str, "\\\\");
+                break;
+            case '\b':
+                qstring_append(str, "\\b");
+                break;
+            case '\f':
+                qstring_append(str, "\\f");
+                break;
+            case '\n':
+                qstring_append(str, "\\n");
+                break;
+            case '\r':
+                qstring_append(str, "\\r");
+                break;
+            case '\t':
+                qstring_append(str, "\\t");
+                break;
+            default:
+                if (cp < 0) {
+                    cp = 0xFFFD; /* replacement character */
                 }
+                if (cp > 0xFFFF) {
+                    /* beyond BMP; need a surrogate pair */
+                    snprintf(buf, sizeof(buf), "\\u%04X\\u%04X",
+                             0xD800 + ((cp - 0x10000) >> 10),
+                             0xDC00 + ((cp - 0x10000) & 0x3FF));
+                } else if (cp < 0x20 || cp >= 0x7F) {
+                    snprintf(buf, sizeof(buf), "\\u%04X", cp);
+                } else {
+                    buf[0] = cp;
+                    buf[1] = 0;
                 }
-            ptr++;
-        }
+                qstring_append(str, buf);
+            }
+        };
+
         qstring_append(str, "\"");
         break;
     }
tests/check-qjson.c +100 -116

623 lines not on this page · raw message

Signature · 1 line
1.7.11.7
#13 lore raw

Re: [Qemu-devel] [PATCH 4/4] qjson: to_json() case QTYPE_QSTRING is buggy, rewrite

Quoted 3 lines · Markus Armbruster

Known bugs in to_json():

My rewrite fixes them as follows:

I'll try to review this sometime later. Patch review doesn't scale *at all*. I've spent hours on the first 3 patches. You should just be given pull req rights. I'd need 36 hour days.

/me throws his hands up

Laszlo

Reviewed-by on v1 4/4
#14 lore raw

Re: [Qemu-devel] [PATCH 4/4] qjson: to_json() case QTYPE_QSTRING is buggy, rewrite

In reply to Markus Armbruster

comments below

Quoted 62 lines · Markus Armbruster
qobject/qjson.c +3 -48
index 83a6b4f..19085a1 100644
@@ -136,68 +136,56 @@ static void to_json(const QObject *obj, QString *str, int pretty, int indent)
     case QTYPE_QSTRING: {
         QString *val = qobject_to_qstring(obj);
         const char *ptr;
+        int cp;
+        char buf[16];
+        char *end;
 
         ptr = qstring_get_str(val);
         qstring_append(str, "\"");
-        while (*ptr) {
-            if ((ptr[0] & 0xE0) == 0xE0 &&
-                (ptr[1] & 0x80) && (ptr[2] & 0x80)) {
-                uint16_t wchar;
-                char escape[7];
-
-                wchar  = (ptr[0] & 0x0F) << 12;
-                wchar |= (ptr[1] & 0x3F) << 6;
-                wchar |= (ptr[2] & 0x3F);
-                ptr += 2;
-
-                snprintf(escape, sizeof(escape), "\\u%04X", wchar);
-                qstring_append(str, escape);
-            } else if ((ptr[0] & 0xE0) == 0xC0 && (ptr[1] & 0x80)) {
-                uint16_t wchar;
-                char escape[7];
-
-                wchar  = (ptr[0] & 0x1F) << 6;
-                wchar |= (ptr[1] & 0x3F);
-                ptr++;
-
-                snprintf(escape, sizeof(escape), "\\u%04X", wchar);
-                qstring_append(str, escape);
-            } else switch (ptr[0]) {
-                case '\"':
-                    qstring_append(str, "\\\"");
-                    break;
-                case '\\':
-                    qstring_append(str, "\\\\");
-                    break;
-                case '\b':
-                    qstring_append(str, "\\b");
-                    break;
-                case '\f':
-                    qstring_append(str, "\\f");
-                    break;
-                case '\n':
-                    qstring_append(str, "\\n");
-                    break;
-                case '\r':
-                    qstring_append(str, "\\r");
-                    break;
-                case '\t':
-                    qstring_append(str, "\\t");
-                    break;
-                default: {
-                    if (ptr[0] <= 0x1F) {
-                        char escape[7];

qobject/qjson.c static void to_json(const QObject *obj, QString *str, int pretty, int indent) · line 147

Quoted 10 lines
qobject/qjson.c +3 -7
-                        snprintf(escape, sizeof(escape), "\\u%04X", ptr[0]);
-                        qstring_append(str, escape);
-                    } else {
-                        char buf[2] = { ptr[0], 0 };
-                        qstring_append(str, buf);
-                    }
-                    break;
+
+        for (; *ptr; ptr = end) {
+            cp = mod_utf8_codepoint(ptr, 6, &end);

This provides more background: you never call mod_utf8_codepoint() with '\0' at offset 0. So handling that in mod_utf8_codepoint() may not be that important.

If a '\0' is found at offset >= 1, it will correctly trigger the /* continuation byte missing */ branch in mod_utf8_codepoint(). The retval is -1, and *end is left pointing to the NUL byte. (This is consistent with mod_utf8_codepoint()'s docs.)

The -1 (incomplete sequence) produces the replacement character below, and the next time around *ptr is '\0', so we finish the loop. Seems OK.

(
An alternative interface for mod_utf8_codepoint() might be something like:

    size_t alternative(const char *ptr, int *cp, size_t n);

Resembling read() somewhat:
- the return value would be the number of bytes consumed (it can't be negative (= fatal error), because we guarantee progress). 0 is EOF and only possible when "n" is 0.
- "ptr" is the source,
- "cp" is the output code point, -1 if invalid,
- "n" is the bytes available in the source / requested to process at most.

Encountering a \0 in the byte stream would be an error (*cp = -1), but would not terminate parsing per se.

Then the loop would look like:

    processed = 0;
    while (processed < full) {
      int cp;

      rd = alternative(ptr + processed, &cp, full - processed);
      g_assert(rd > 0);

      /* look at cp */

      processed += rd;

}

But of course I'm not suggesting to rewrite the function! )

Quoted 12 lines
qobject/qjson.c +12 -0
+            switch (cp) {
+            case '\"':
+                qstring_append(str, "\\\"");
+                break;
+            case '\\':
+                qstring_append(str, "\\\\");
+                break;
+            case '\b':
+                qstring_append(str, "\\b");
+                break;
+            case '\f':
+                qstring_append(str, "\\f");

qobject/qjson.c static void to_json(const QObject *obj, QString *str, int pretty, int indent) · line 169

Quoted 10 lines
qobject/qjson.c +10 -0
+                break;
+            case '\n':
+                qstring_append(str, "\\n");
+                break;
+            case '\r':
+                qstring_append(str, "\\r");
+                break;
+            case '\t':
+                qstring_append(str, "\\t");
+                break;

The C standard also names \a (alert) and \v (vertical tab); I'm not sure about their JSON notation. (The (cp < 0x20) condition catches them below of course.)

qobject/qjson.c static void to_json(const QObject *obj, QString *str, int pretty, int indent) · line 178

Quoted 9 lines
qobject/qjson.c +8 -0
+            default:
+                if (cp < 0) {
+                    cp = 0xFFFD; /* replacement character */
                 }
+                if (cp > 0xFFFF) {
+                    /* beyond BMP; need a surrogate pair */
+                    snprintf(buf, sizeof(buf), "\\u%04X\\u%04X",
+                             0xD800 + ((cp - 0x10000) >> 10),
+                             0xDC00 + ((cp - 0x10000) & 0x3FF));

Seems like we write 13 bytes into buf, OK. Also cp is never greater than 0x10FFFF, hence the difference is at most 0xFFFFF. The RHS surrogate half can go up to 0xDFFF, the LHS up to 0xD800+0x3FF == 0xDBFF. Good.

Quoted 5 lines
qobject/qjson.c +5 -0
+                } else if (cp < 0x20 || cp >= 0x7F) {
+                    snprintf(buf, sizeof(buf), "\\u%04X", cp);
+                } else {
+                    buf[0] = cp;
+                    buf[1] = 0;

qobject/qjson.c static void to_json(const QObject *obj, QString *str, int pretty, int indent) · line 191

Quoted 10 lines
qobject/qjson.c +4 -2
                 }
-            ptr++;
-        }
+                qstring_append(str, buf);
+            }
+        };
+
         qstring_append(str, "\"");
         break;
     }

Seems OK.

tests/check-qjson.c

Quoted 4 lines
tests/check-qjson.c +0 -0
index efec1b2..595ddc0 100644

I'll trust you on that one :)

#15 lore raw

Re: [Qemu-devel] [PATCH 4/4] qjson: to_json() case QTYPE_QSTRING is buggy, rewrite

Quoted 72 lines · Laszlo Ersek

comments below

Quoted 68 lines · Markus Armbruster
qobject/qjson.c +3 -54
index 83a6b4f..19085a1 100644
@@ -136,68 +136,56 @@ static void to_json(const QObject *obj, QString *str, int pretty, int indent)
     case QTYPE_QSTRING: {
         QString *val = qobject_to_qstring(obj);
         const char *ptr;
+        int cp;
+        char buf[16];
+        char *end;
 
         ptr = qstring_get_str(val);
         qstring_append(str, "\"");
-        while (*ptr) {
-            if ((ptr[0] & 0xE0) == 0xE0 &&
-                (ptr[1] & 0x80) && (ptr[2] & 0x80)) {
-                uint16_t wchar;
-                char escape[7];
-
-                wchar  = (ptr[0] & 0x0F) << 12;
-                wchar |= (ptr[1] & 0x3F) << 6;
-                wchar |= (ptr[2] & 0x3F);
-                ptr += 2;
-
-                snprintf(escape, sizeof(escape), "\\u%04X", wchar);
-                qstring_append(str, escape);
-            } else if ((ptr[0] & 0xE0) == 0xC0 && (ptr[1] & 0x80)) {
-                uint16_t wchar;
-                char escape[7];
-
-                wchar  = (ptr[0] & 0x1F) << 6;
-                wchar |= (ptr[1] & 0x3F);
-                ptr++;
-
-                snprintf(escape, sizeof(escape), "\\u%04X", wchar);
-                qstring_append(str, escape);
-            } else switch (ptr[0]) {
-                case '\"':
-                    qstring_append(str, "\\\"");
-                    break;
-                case '\\':
-                    qstring_append(str, "\\\\");
-                    break;
-                case '\b':
-                    qstring_append(str, "\\b");
-                    break;
-                case '\f':
-                    qstring_append(str, "\\f");
-                    break;
-                case '\n':
-                    qstring_append(str, "\\n");
-                    break;
-                case '\r':
-                    qstring_append(str, "\\r");
-                    break;
-                case '\t':
-                    qstring_append(str, "\\t");
-                    break;
-                default: {
-                    if (ptr[0] <= 0x1F) {
-                        char escape[7];
-                        snprintf(escape, sizeof(escape), "\\u%04X", ptr[0]);
-                        qstring_append(str, escape);
-                    } else {
-                        char buf[2] = { ptr[0], 0 };
-                        qstring_append(str, buf);
-                    }
Quoted 8 lines

qobject/qjson.c static void to_json(const QObject *obj, QString *str, int pretty, int indent) · line 147

Quoted 4 lines
qobject/qjson.c +3 -1
-                    break;
+
+        for (; *ptr; ptr = end) {
+            cp = mod_utf8_codepoint(ptr, 6, &end);

This provides more background: you never call mod_utf8_codepoint() with '\0' at offset 0. So handling that in mod_utf8_codepoint() may not be that important.

Yes, this caller doesn't care. Doesn't mean we shouldn't try to come up with a sane function contract.

Note the use of literal 6. It means "unlimited". Perfectly safe because the string is nul-terminated.

Quoted 36 lines

If a '\0' is found at offset >= 1, it will correctly trigger the /* continuation byte missing */ branch in mod_utf8_codepoint(). The retval is -1, and *end is left pointing to the NUL byte. (This is consistent with mod_utf8_codepoint()'s docs.)

The -1 (incomplete sequence) produces the replacement character below, and the next time around *ptr is '\0', so we finish the loop. Seems OK.

(
An alternative interface for mod_utf8_codepoint() might be something like:

    size_t alternative(const char *ptr, int *cp, size_t n);

Resembling read() somewhat:
- the return value would be the number of bytes consumed (it can't be negative (= fatal error), because we guarantee progress). 0 is EOF and only possible when "n" is 0.
- "ptr" is the source,
- "cp" is the output code point, -1 if invalid,
- "n" is the bytes available in the source / requested to process at most.

Encountering a \0 in the byte stream would be an error (*cp = -1), but would not terminate parsing per se.

Then the loop would look like:

    processed = 0;
    while (processed < full) {
      int cp;

      rd = alternative(ptr + processed, &cp, full - processed);
      g_assert(rd > 0);

      /* look at cp */

      processed += rd;
Quoted 4 lines

}

But of course I'm not suggesting to rewrite the function! )

I'll keep this in mind when deciding how I want to handle '\0'.

Quoted 23 lines
Quoted 12 lines
qobject/qjson.c +12 -0
+            switch (cp) {
+            case '\"':
+                qstring_append(str, "\\\"");
+                break;
+            case '\\':
+                qstring_append(str, "\\\\");
+                break;
+            case '\b':
+                qstring_append(str, "\\b");
+                break;
+            case '\f':
+                qstring_append(str, "\\f");

qobject/qjson.c static void to_json(const QObject *obj, QString *str, int pretty, int indent) · line 169

Quoted 10 lines
qobject/qjson.c +10 -0
+                break;
+            case '\n':
+                qstring_append(str, "\\n");
+                break;
+            case '\r':
+                qstring_append(str, "\\r");
+                break;
+            case '\t':
+                qstring_append(str, "\\t");
+                break;
Quoted 3 lines

The C standard also names \a (alert) and \v (vertical tab); I'm not sure about their JSON notation. (The (cp < 0x20) condition catches them below of course.)

JSON RFC 4627 defines only the seven above plus '\/'. Escaping '/' that way makes no sense for us, so the old code doesn't, and mine doesn't either.

Quoted 10 lines

qobject/qjson.c static void to_json(const QObject *obj, QString *str, int pretty, int indent) · line 178

Quoted 9 lines
qobject/qjson.c +8 -0
+            default:
+                if (cp < 0) {
+                    cp = 0xFFFD; /* replacement character */
                 }
+                if (cp > 0xFFFF) {
+                    /* beyond BMP; need a surrogate pair */
+                    snprintf(buf, sizeof(buf), "\\u%04X\\u%04X",
+                             0xD800 + ((cp - 0x10000) >> 10),
+                             0xDC00 + ((cp - 0x10000) & 0x3FF));
Quoted 3 lines

Seems like we write 13 bytes into buf, OK. Also cp is never greater than 0x10FFFF, hence the difference is at most 0xFFFFF. The RHS surrogate half can go up to 0xDFFF, the LHS up to 0xD800+0x3FF == 0xDBFF. Good.

Exactly.

Quoted 19 lines
Quoted 5 lines
qobject/qjson.c +5 -0
+                } else if (cp < 0x20 || cp >= 0x7F) {
+                    snprintf(buf, sizeof(buf), "\\u%04X", cp);
+                } else {
+                    buf[0] = cp;
+                    buf[1] = 0;

qobject/qjson.c static void to_json(const QObject *obj, QString *str, int pretty, int indent) · line 191

Quoted 10 lines
qobject/qjson.c +4 -2
                 }
-            ptr++;
-        }
+                qstring_append(str, buf);
+            }
+        };
+
         qstring_append(str, "\"");
         break;
     }

Seems OK.

Quoted 6 lines

tests/check-qjson.c

Quoted 4 lines
tests/check-qjson.c +0 -0
index efec1b2..595ddc0 100644

I'll trust you on that one :)

Waah, you don't want another case of bleeding eyes?!?

Quoted 1 line

Thanks!

Tested-by on the v1 series
#16 lore raw
In reply to Markus Armbruster
Quoted 2 lines · Markus Armbruster

This should unbreak "make check" on machines where char is unsigned. Blue, please give it a whirl.

With the patches applied there are no errors, thanks.

Though test-coroutine seems to hang, maybe fallout from recent coroutine changes.

Quoted 20 lines

The JSON parser is still as broken as ever. Left for another day.

Markus Armbruster (4):
unicode: New mod_utf8_codepoint()
check-qjson: Fix up a few bogus comments
check-qjson: Test noncharacters other than U+FFFE, U+FFFF in strings
qjson: to_json() case QTYPE_QSTRING is buggy, rewrite

 include/qemu-common.h |   3 +
 qobject/qjson.c       | 102 ++++++++----------
 tests/check-qjson.c   | 280 +++++++++++++++++++++++++++++---------------------
 util/Makefile.objs    |   1 +
 util/unicode.c        |  96 +++++++++++++++++
 5 files changed, 306 insertions(+), 176 deletions(-)
 create mode 100644 util/unicode.c

--
1.7.11.7

#17 lore raw
Quoted 6 lines · Blue Swirl
Quoted 2 lines · Markus Armbruster

This should unbreak "make check" on machines where char is unsigned. Blue, please give it a whirl.

With the patches applied there are no errors, thanks.

Thanks!

Quoted 2 lines

Though test-coroutine seems to hang, maybe fallout from recent coroutine changes.

I've seen rtc-test hang intermittently; haven't gotten around to digging for roots.

#18 lore raw
In reply to Markus Armbruster
Quoted 2 lines · Markus Armbruster

This should unbreak "make check" on machines where char is unsigned. Blue, please give it a whirl.

Patches no longer apply, please rebase.

Quoted 20 lines

The JSON parser is still as broken as ever. Left for another day.

Markus Armbruster (4):
unicode: New mod_utf8_codepoint()
check-qjson: Fix up a few bogus comments
check-qjson: Test noncharacters other than U+FFFE, U+FFFF in strings
qjson: to_json() case QTYPE_QSTRING is buggy, rewrite

 include/qemu-common.h |   3 +
 qobject/qjson.c       | 102 ++++++++----------
 tests/check-qjson.c   | 280 +++++++++++++++++++++++++++++---------------------
 util/Makefile.objs    |   1 +
 util/unicode.c        |  96 +++++++++++++++++
 5 files changed, 306 insertions(+), 176 deletions(-)
 create mode 100644 util/unicode.c

--
1.7.11.7

#19 lore raw
Quoted 5 lines · Blue Swirl
Quoted 2 lines · Markus Armbruster

This should unbreak "make check" on machines where char is unsigned. Blue, please give it a whirl.

Patches no longer apply, please rebase.

Sent.