X-Git-Url: https://git.notmuchmail.org/git?a=blobdiff_plain;f=util%2Fstring-util.c;h=3e7066cd58ea191a9e647f5a0177f5547f662388;hb=b547830783ee0732696d5c05a00cfc57baba065f;hp=aba9aa836e6522c7c51be56458dfee6fd48618cb;hpb=7c3a995d6b6e066aa68b0adf0433f45cb3812c2a;p=notmuch diff --git a/util/string-util.c b/util/string-util.c index aba9aa83..3e7066cd 100644 --- a/util/string-util.c +++ b/util/string-util.c @@ -37,6 +37,28 @@ strtok_len (char *s, const char *delim, size_t *len) return *len ? s : NULL; } +char * +sanitize_string (const void *ctx, const char *str) +{ + char *out, *loop; + + if (! str) + return NULL; + + out = talloc_strdup (ctx, str); + if (! out) + return NULL; + + for (loop = out; *loop; loop++) { + if (*loop == '\t' || *loop == '\n') + *loop = ' '; + else if ((unsigned char)(*loop) < 32) + *loop = '?'; + } + + return out; +} + static int is_unquoted_terminator (unsigned char c) { @@ -53,10 +75,12 @@ make_boolean_term (void *ctx, const char *prefix, const char *term, int need_quoting = 0; /* Do we need quoting? To be paranoid, we quote anything - * containing a quote, even though it only matters at the + * containing a quote or '(', even though these only matter at the * beginning, and anything containing non-ASCII text. */ + if (! term[0]) + need_quoting = 1; for (in = term; *in && !need_quoting; in++) - if (is_unquoted_terminator (*in) || *in == '"' + if (is_unquoted_terminator (*in) || *in == '"' || *in == '(' || (unsigned char)*in > 127) need_quoting = 1; @@ -127,7 +151,7 @@ parse_boolean_term (void *ctx, const char *str, /* Parse prefix */ str = skip_space (str); const char *pos = strchr (str, ':'); - if (! pos) + if (! pos || pos == str) goto FAIL; *prefix_out = talloc_strndup (ctx, str, pos - str); if (! *prefix_out) {