Return-Path: X-Original-To: notmuch@notmuchmail.org Delivered-To: notmuch@notmuchmail.org Received: from localhost (localhost [127.0.0.1]) by olra.theworths.org (Postfix) with ESMTP id 53133431FB6 for ; Mon, 31 Dec 2012 04:02:00 -0800 (PST) X-Virus-Scanned: Debian amavisd-new at olra.theworths.org X-Spam-Flag: NO X-Spam-Score: -1.098 X-Spam-Level: X-Spam-Status: No, score=-1.098 tagged_above=-999 required=5 tests=[DKIM_ADSP_CUSTOM_MED=0.001, FREEMAIL_FROM=0.001, NML_ADSP_CUSTOM_MED=1.2, RCVD_IN_DNSWL_MED=-2.3] autolearn=disabled Received: from olra.theworths.org ([127.0.0.1]) by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id P1wJhFx4PT9u for ; Mon, 31 Dec 2012 04:01:56 -0800 (PST) Received: from mail2.qmul.ac.uk (mail2.qmul.ac.uk [138.37.6.6]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by olra.theworths.org (Postfix) with ESMTPS id 5BA84431FAF for ; Mon, 31 Dec 2012 04:01:56 -0800 (PST) Received: from smtp.qmul.ac.uk ([138.37.6.40]) by mail2.qmul.ac.uk with esmtp (Exim 4.71) (envelope-from ) id 1Tpe3t-0005zv-BZ; Mon, 31 Dec 2012 12:01:45 +0000 Received: from 188.31.19.240.threembb.co.uk ([188.31.19.240] helo=localhost) by smtp.qmul.ac.uk with esmtpsa (TLSv1:AES128-SHA:128) (Exim 4.69) (envelope-from ) id 1Tpe3s-0007gY-Df; Mon, 31 Dec 2012 12:01:45 +0000 From: Mark Walters To: Austin Clements , notmuch@notmuchmail.org Subject: Re: [PATCH v4 2/5] util: Function to parse boolean term queries In-Reply-To: <1356936162-2589-3-git-send-email-amdragon@mit.edu> References: <1356936162-2589-1-git-send-email-amdragon@mit.edu> <1356936162-2589-3-git-send-email-amdragon@mit.edu> User-Agent: Notmuch/0.14+236~g1d0044f (http://notmuchmail.org) Emacs/23.4.1 (x86_64-pc-linux-gnu) Date: Mon, 31 Dec 2012 12:01:47 +0000 Message-ID: <8738ymv39w.fsf@qmul.ac.uk> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Sender-Host-Address: 188.31.19.240 X-QM-SPAM-Info: Sender has good ham record. :) X-QM-Body-MD5: 5f63b2a618e032e83994c2ba766fe734 (of first 20000 bytes) X-SpamAssassin-Score: -2.3 X-SpamAssassin-SpamBar: -- X-SpamAssassin-Report: The QM spam filters have analysed this message to determine if it is spam. We require at least 5.0 points to mark a message as spam. This message scored -2.3 points. Summary of the scoring: * -2.3 RCVD_IN_DNSWL_MED RBL: Sender listed at http://www.dnswl.org/, * medium trust * [138.37.6.40 listed in list.dnswl.org] * 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider * (markwalters1009[at]gmail.com) * -0.0 T_RP_MATCHES_RCVD Envelope sender domain matches handover relay * domain X-QM-Scan-Virus: ClamAV says the message is clean Cc: tomi.ollila@iki.fi X-BeenThere: notmuch@notmuchmail.org X-Mailman-Version: 2.1.13 Precedence: list List-Id: "Use and development of the notmuch mail system." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 31 Dec 2012 12:02:00 -0000 On Mon, 31 Dec 2012, Austin Clements wrote: > This parses the subset of Xapian's boolean term quoting rules that are > used by make_boolean_term. This is provided as a generic string > utility, but will be used shortly in notmuch restore to parse and > optimize for ID queries. This looks good to me with one concern. Do you need to check that the three talloc allocations in parse_boolean_term succeed? I think they could fail in an OOM type situation which I think would cause a seg fault. I guess failures in restore are much less important than in dump, and I don't know how careful notmuch is in general. Best wishes Mark > util/string-util.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++ > util/string-util.h | 15 ++++++++++++ > 2 files changed, 82 insertions(+) > > diff --git a/util/string-util.c b/util/string-util.c > index e4bea21..52c7781 100644 > --- a/util/string-util.c > +++ b/util/string-util.c > @@ -22,6 +22,8 @@ > #include "string-util.h" > #include "talloc.h" > > +#include > + > char * > strtok_len (char *s, const char *delim, size_t *len) > { > @@ -96,3 +98,68 @@ make_boolean_term (void *ctx, const char *prefix, const char *term, > > return 0; > } > + > +static const char* > +skip_space (const char *str) > +{ > + while (*str && isspace (*str)) > + ++str; > + return str; > +} > + > +int > +parse_boolean_term (void *ctx, const char *str, > + char **prefix_out, char **term_out) > +{ > + *prefix_out = *term_out = NULL; > + > + /* Parse prefix */ > + str = skip_space (str); > + const char *pos = strchr (str, ':'); > + if (! pos) > + goto FAIL; > + *prefix_out = talloc_strndup (ctx, str, pos - str); > + ++pos; > + > + /* Implement de-quoting compatible with make_boolean_term. */ > + if (*pos == '"') { > + char *out = talloc_array (ctx, char, strlen (pos)); > + int closed = 0; > + *term_out = out; > + /* Skip the opening quote, find the closing quote, and > + * un-double doubled internal quotes. */ > + for (++pos; *pos; ) { > + if (*pos == '"') { > + ++pos; > + if (*pos != '"') { > + /* Found the closing quote. */ > + closed = 1; > + pos = skip_space (pos); > + break; > + } > + } > + *out++ = *pos++; > + } > + /* Did the term terminate without a closing quote or is there > + * trailing text after the closing quote? */ > + if (!closed || *pos) > + goto FAIL; > + *out = '\0'; > + } else { > + const char *start = pos; > + /* Check for text after the boolean term. */ > + while (*pos > ' ' && *pos != ')') > + ++pos; > + if (*skip_space (pos)) > + goto FAIL; > + /* No trailing text; dup the string so the caller can free > + * it. */ > + *term_out = talloc_strndup (ctx, start, pos - start); > + } > + return 0; > + > + FAIL: > + talloc_free (*prefix_out); > + talloc_free (*term_out); > + return 1; > +} > diff --git a/util/string-util.h b/util/string-util.h > index b8844a3..8b9fe50 100644 > --- a/util/string-util.h > +++ b/util/string-util.h > @@ -33,4 +33,19 @@ char *strtok_len (char *s, const char *delim, size_t *len); > int make_boolean_term (void *talloc_ctx, const char *prefix, const char *term, > char **buf, size_t *len); > > +/* Parse a boolean term query consisting of a prefix, a colon, and a > + * term that may be quoted as described for make_boolean_term. If the > + * term is not quoted, then it ends at the first whitespace or close > + * parenthesis. str may containing leading or trailing whitespace, > + * but anything else is considered a parse error. This is compatible > + * with anything produced by make_boolean_term, and supports a subset > + * of the quoting styles supported by Xapian (and hence notmuch). > + * *prefix_out and *term_out will be talloc'd with context ctx. > + * > + * Return: 0 on success, non-zero on parse error. > + */ > +int > +parse_boolean_term (void *ctx, const char *str, > + char **prefix_out, char **term_out); > + > #endif > -- > 1.7.10.4