aboutsummaryrefslogtreecommitdiffhomepage
path: root/json.c
diff options
context:
space:
mode:
authorGravatar David Edmondson <dme@dme.org>2010-04-05 10:33:19 +0100
committerGravatar Carl Worth <cworth@cworth.org>2010-04-05 10:57:23 -0700
commit9eb360329929258fb243cfb5095989ba45391109 (patch)
tree3315907a0b75a377e48a4ea44fd237307681122f /json.c
parentbb5211684654b7cf54f842990a733a64fe01d612 (diff)
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.
Diffstat (limited to 'json.c')
-rw-r--r--json.c32
1 files changed, 24 insertions, 8 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)));
+}