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.
This commit is contained in:
David Edmondson 2010-04-05 10:33:19 +01:00 committed by Carl Worth
parent bb52116846
commit 9eb3603299
3 changed files with 28 additions and 9 deletions

32
json.c
View file

@ -47,29 +47,39 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE. * THE SOFTWARE.
*/ */
char * 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; const char *ptr;
char *ptr2; char *ptr2;
char *out; char *out;
int len = 0; size_t loop;
size_t required;
if (!str) if (len == 0)
return NULL; 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 == '\\') 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; ptr = str;
ptr2 = out; ptr2 = out;
*ptr2++ = '\"'; *ptr2++ = '\"';
while (*ptr) { for (loop = 0; loop < len; loop++) {
if (*ptr > 31 && *ptr != '\"' && *ptr != '\\') { if (*ptr > 31 && *ptr != '\"' && *ptr != '\\') {
*ptr2++ = *ptr++; *ptr2++ = *ptr++;
} else { } else {
@ -91,3 +101,9 @@ json_quote_str(const void *ctx, const char *str)
return out; return out;
} }
char *
json_quote_str(const void *ctx, const char *str)
{
return (json_quote_chararray (ctx, str, strlen (str)));
}

View file

@ -132,6 +132,9 @@ show_message_body (const char *filename,
notmuch_status_t notmuch_status_t
show_one_part (const char *filename, int part); show_one_part (const char *filename, int part);
char *
json_quote_chararray (const void *ctx, const char *str, const size_t len);
char * char *
json_quote_str (const void *ctx, const char *str); json_quote_str (const void *ctx, const char *str);

View file

@ -326,7 +326,7 @@ format_part_json (GMimeObject *part, int *part_count)
show_part_content (part, stream_memory); show_part_content (part, stream_memory);
part_content = g_mime_stream_mem_get_byte_array (GMIME_STREAM_MEM (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); fputs ("}", stdout);