]> git.notmuchmail.org Git - notmuch/commitdiff
notmuch: Correctly terminate text/* parts in JSON output
authorDavid Edmondson <dme@dme.org>
Mon, 5 Apr 2010 09:33:19 +0000 (10:33 +0100)
committerCarl Worth <cworth@cworth.org>
Mon, 5 Apr 2010 17:57:23 +0000 (10:57 -0700)
Text parts returned by `g_mime_stream_mem_get_byte_array()' are not
NULL terminated strings - add `json_quote_chararray()' to handle them
correctly.

json.c
notmuch-client.h
notmuch-show.c

diff --git a/json.c b/json.c
index 96141433d578b037aa9dbd2217ccb6ff40ecad04..f90b0fa245324204347070d870ac22257e0d6f95 100644 (file)
--- a/json.c
+++ b/json.c
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  * THE SOFTWARE.
  */
+
 char *
-json_quote_str(const void *ctx, const char *str)
+json_quote_chararray(const void *ctx, const char *str, const size_t len)
 {
     const char *ptr;
     char *ptr2;
     char *out;
-    int len = 0;
+    size_t loop;
+    size_t required;
 
-    if (!str)
-       return NULL;
+    if (len == 0)
+       return (char *)"\"\"";
 
-    for (ptr = str; *ptr; len++, ptr++) {
+    for (loop = 0, required = 0, ptr = str;
+        loop < len;
+        loop++, required++, ptr++) {
        if (*ptr < 32 || *ptr == '\"' || *ptr == '\\')
-           len++;
+           required++;
     }
 
-    out = talloc_array (ctx, char, len + 3);
+    /*
+     * + 3 for:
+     * - leading quotation mark,
+     * - trailing quotation mark,
+     * - trailing NULL.
+     */
+    out = talloc_array (ctx, char, required + 3);
 
     ptr = str;
     ptr2 = out;
 
     *ptr2++ = '\"';
-    while (*ptr) {
+    for (loop = 0; loop < len; loop++) {
            if (*ptr > 31 && *ptr != '\"' && *ptr != '\\') {
                *ptr2++ = *ptr++;
            } else {
@@ -91,3 +101,9 @@ json_quote_str(const void *ctx, const char *str)
 
     return out;
 }
+
+char *
+json_quote_str(const void *ctx, const char *str)
+{
+    return (json_quote_chararray (ctx, str, strlen (str)));
+}
index a090589ef6ef28f350d1364f488b1b8f711bc0bf..d36b9ec16ce4aba89c782489b0ad91057fb34c8a 100644 (file)
@@ -132,6 +132,9 @@ show_message_body (const char *filename,
 notmuch_status_t
 show_one_part (const char *filename, int part);
 
+char *
+json_quote_chararray (const void *ctx, const char *str, const size_t len);
+
 char *
 json_quote_str (const void *ctx, const char *str);
 
index e317d19855e9eed69483b051a04de4c7749ac62d..76873a1d9036337868811659c99bb105ad371dfe 100644 (file)
@@ -326,7 +326,7 @@ format_part_json (GMimeObject *part, int *part_count)
        show_part_content (part, stream_memory);
        part_content = g_mime_stream_mem_get_byte_array (GMIME_STREAM_MEM (stream_memory));
 
-       printf (", \"content\": %s", json_quote_str (ctx, (char *) part_content->data));
+       printf (", \"content\": %s", json_quote_chararray (ctx, (char *) part_content->data, part_content->len));
     }
 
     fputs ("}", stdout);