]> git.notmuchmail.org Git - notmuch/commitdiff
Fix json_quote_str to handle non-ASCII characters
authorGregor Hoffleit <gregor@hoffleit.de>
Thu, 4 Mar 2010 10:40:03 +0000 (11:40 +0100)
committerCarl Worth <cworth@cworth.org>
Tue, 13 Apr 2010 15:30:40 +0000 (08:30 -0700)
The current code in json_quote_str() only accepts strict printable ASCII
code points (i.e. 32-127), all other code points are dropped from the
JSON output. The code is attempting to drop only non-printable ASCII
characters, but doing a signed comparison of the byte value is also
dropping characters with values >= 128.

This patch uses an unsigned comparison to accept code points 32-255.

Reviewed-by: Carl Worth <cworth@cworth.org> (with some additional
details for commit message).

json.c

diff --git a/json.c b/json.c
index f90b0fa245324204347070d870ac22257e0d6f95..1d0c169449657c49970390ebf76cf98ce11be2e8 100644 (file)
--- a/json.c
+++ b/json.c
@@ -63,7 +63,7 @@ json_quote_chararray(const void *ctx, const char *str, const size_t len)
     for (loop = 0, required = 0, ptr = str;
         loop < len;
         loop++, required++, ptr++) {
-       if (*ptr < 32 || *ptr == '\"' || *ptr == '\\')
+       if ((unsigned char)(*ptr) < 32 || *ptr == '\"' || *ptr == '\\')
            required++;
     }
 
@@ -80,7 +80,7 @@ json_quote_chararray(const void *ctx, const char *str, const size_t len)
 
     *ptr2++ = '\"';
     for (loop = 0; loop < len; loop++) {
-           if (*ptr > 31 && *ptr != '\"' && *ptr != '\\') {
+       if ((unsigned char)(*ptr) > 31 && *ptr != '\"' && *ptr != '\\') {
                *ptr2++ = *ptr++;
            } else {
                *ptr2++ = '\\';