From 9eb360329929258fb243cfb5095989ba45391109 Mon Sep 17 00:00:00 2001 From: David Edmondson Date: Mon, 5 Apr 2010 10:33:19 +0100 Subject: [PATCH] notmuch: Correctly terminate text/* parts in JSON output 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 | 32 ++++++++++++++++++++++++-------- notmuch-client.h | 3 +++ notmuch-show.c | 2 +- 3 files changed, 28 insertions(+), 9 deletions(-) diff --git a/json.c b/json.c index 96141433..f90b0fa2 100644 --- a/json.c +++ b/json.c @@ -47,29 +47,39 @@ * 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))); +} diff --git a/notmuch-client.h b/notmuch-client.h index a090589e..d36b9ec1 100644 --- a/notmuch-client.h +++ b/notmuch-client.h @@ -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); diff --git a/notmuch-show.c b/notmuch-show.c index e317d198..76873a1d 100644 --- a/notmuch-show.c +++ b/notmuch-show.c @@ -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); -- 2.43.0