From 87ca9b11fd146605fc3b54fcafbcad9869487295 Mon Sep 17 00:00:00 2001 From: Adam Dickmeiss Date: Mon, 1 Nov 2004 20:13:02 +0000 Subject: [PATCH] Fixed bug #148: CQL parser may leak. CQL nodes now allocated using NMEM instead of xmalloc. For this reason, few prototypes were changed in include/yaz/cql.h. --- NEWS | 8 +++++-- include/yaz/cql.h | 19 +++++++--------- src/cql.y | 26 +++++++++++++--------- src/cqltransform.c | 8 ++++--- src/cqlutil.c | 62 +++++++++++++++++++++++----------------------------- 5 files changed, 62 insertions(+), 61 deletions(-) diff --git a/NEWS b/NEWS index 79b5056..053b98d 100644 --- a/NEWS +++ b/NEWS @@ -1,6 +1,10 @@ Possible compatibility problems with earlier versions marked with '*'. ---- 2.0.27 2004/10/XX +--- 2.0.27 2004/11/01 + +Fixed bug #148: CQL parser may leak. + +* A few prototypes were changed in include/yaz/cql.h. Fixed bug #176: Dont throw diagnostics on empty SRU args. @@ -11,7 +15,7 @@ Fixed bug #172: RPM builds does not enable SSL. Doxyfile.in part of dist. Doxyfile generated by configure. Configure aborts if any of --with-xml2, --with-openssl, --enable-tcpd -ares given and the given component does not exist. +are given and the corresponding component does not exist. Extend CCL documentation in YAZ reference. Describe r=r. diff --git a/include/yaz/cql.h b/include/yaz/cql.h index 73c242e..827693b 100644 --- a/include/yaz/cql.h +++ b/include/yaz/cql.h @@ -1,4 +1,4 @@ -/* $Id: cql.h,v 1.9 2004-10-03 22:34:07 adam Exp $ +/* $Id: cql.h,v 1.10 2004-11-01 20:13:02 adam Exp $ Copyright (C) 2002-2004 Index Data Aps @@ -14,7 +14,7 @@ See the file LICENSE. #ifndef CQL_H_INCLUDED #define CQL_H_INCLUDED #include -#include +#include YAZ_BEGIN_CDECL @@ -141,10 +141,8 @@ void cql_node_print(struct cql_node *cn); * This function creates a search clause node (st). */ YAZ_EXPORT -struct cql_node *cql_node_mk_sc(const char *index, - const char *relation, - const char *term); - +struct cql_node *cql_node_mk_sc(NMEM nmem, const char *index, + const char *relation, const char *term); /** * This function applies a prefix+uri to "unresolved" index and relation @@ -154,15 +152,14 @@ struct cql_node *cql_node_mk_sc(const char *index, * is NULL. */ YAZ_EXPORT -struct cql_node *cql_apply_prefix(struct cql_node *cn, - const char *prefix, - const char *uri); +struct cql_node *cql_apply_prefix(NMEM nmem, struct cql_node *cn, + const char *prefix, const char *uri); /** * This function creates a boolean node. */ YAZ_EXPORT -struct cql_node *cql_node_mk_boolean(const char *op); +struct cql_node *cql_node_mk_boolean(NMEM nmem, const char *op); /** * Destroys a node and its children. @@ -174,7 +171,7 @@ void cql_node_destroy(struct cql_node *cn); * Duplicate a node (returns a copy of supplied node) . */ YAZ_EXPORT -struct cql_node *cql_node_dup (struct cql_node *cp); +struct cql_node *cql_node_dup (NMEM nmem, struct cql_node *cp); /** * This function returns the parse tree of the most recently parsed diff --git a/src/cql.y b/src/cql.y index 1206f21..1086549 100644 --- a/src/cql.y +++ b/src/cql.y @@ -1,4 +1,4 @@ -/* $Id: cql.y,v 1.7 2004-10-15 00:19:00 adam Exp $ +/* $Id: cql.y,v 1.8 2004-11-01 20:13:02 adam Exp $ Copyright (C) 2002-2004 Index Data Aps @@ -64,7 +64,8 @@ See the file LICENSE. %% top: { - $$.rel = cql_node_mk_sc("cql.serverChoice", "scr", 0); + $$.rel = cql_node_mk_sc(((CQL_parser) parm)->nmem, + "cql.serverChoice", "scr", 0); ((CQL_parser) parm)->top = 0; } cqlQuery1 { cql_node_destroy($$.rel); @@ -85,7 +86,8 @@ cqlQuery: cqlQuery boolean modifiers { $$.rel = $0.rel; } searchClause { - struct cql_node *cn = cql_node_mk_boolean($2.buf); + struct cql_node *cn = cql_node_mk_boolean(((CQL_parser) parm)->nmem, + $2.buf); cn->u.boolean.modifiers = $3.cql; cn->u.boolean.left = $1.cql; @@ -104,13 +106,13 @@ searchClause: } | searchTerm { - struct cql_node *st = cql_node_dup ($0.rel); - st->u.st.term = xstrdup($1.buf); + struct cql_node *st = cql_node_dup (((CQL_parser) parm)->nmem, $0.rel); + st->u.st.term = nmem_strdup(((CQL_parser)parm)->nmem, $1.buf); $$.cql = st; } | index relation modifiers { - $$.rel = cql_node_mk_sc($1.buf, $2.buf, 0); + $$.rel = cql_node_mk_sc(((CQL_parser) parm)->nmem, $1.buf, $2.buf, 0); $$.rel->u.st.modifiers = $3.cql; } searchClause { $$.cql = $5.cql; @@ -119,12 +121,14 @@ searchClause: | '>' searchTerm '=' searchTerm { $$.rel = $0.rel; } cqlQuery { - $$.cql = cql_apply_prefix($6.cql, $2.buf, $4.buf); + $$.cql = cql_apply_prefix(((CQL_parser) parm)->nmem, + $6.cql, $2.buf, $4.buf); } | '>' searchTerm { $$.rel = $0.rel; } cqlQuery { - $$.cql = cql_apply_prefix($4.cql, 0, $2.buf); + $$.cql = cql_apply_prefix(((CQL_parser) parm)->nmem, + $4.cql, 0, $2.buf); } ; @@ -136,7 +140,8 @@ boolean: modifiers: modifiers '/' searchTerm { - struct cql_node *mod = cql_node_mk_sc($3.buf, "=", 0); + struct cql_node *mod = cql_node_mk_sc(((CQL_parser)parm)->nmem, + $3.buf, "=", 0); mod->u.st.modifiers = $1.cql; $$.cql = mod; @@ -144,7 +149,8 @@ modifiers: modifiers '/' searchTerm | modifiers '/' searchTerm mrelation searchTerm { - struct cql_node *mod = cql_node_mk_sc($3.buf, $4.buf, $5.buf); + struct cql_node *mod = cql_node_mk_sc(((CQL_parser)parm)->nmem, + $3.buf, $4.buf, $5.buf); mod->u.st.modifiers = $1.cql; $$.cql = mod; diff --git a/src/cqltransform.c b/src/cqltransform.c index 5cca9a6..57284be 100644 --- a/src/cqltransform.c +++ b/src/cqltransform.c @@ -1,4 +1,4 @@ -/* $Id: cqltransform.c,v 1.11 2004-10-03 22:34:07 adam Exp $ +/* $Id: cqltransform.c,v 1.12 2004-11-01 20:13:02 adam Exp $ Copyright (C) 2002-2004 Index Data Aps @@ -455,6 +455,7 @@ int cql_transform(cql_transform_t ct, void *client_data) { struct cql_prop_entry *e; + NMEM nmem = nmem_create(); ct->error = 0; if (ct->addinfo) @@ -464,11 +465,12 @@ int cql_transform(cql_transform_t ct, for (e = ct->entry; e ; e = e->next) { if (!memcmp(e->pattern, "set.", 4)) - cql_apply_prefix(cn, e->pattern+4, e->value); + cql_apply_prefix(nmem, cn, e->pattern+4, e->value); else if (!strcmp(e->pattern, "set")) - cql_apply_prefix(cn, 0, e->value); + cql_apply_prefix(nmem, cn, 0, e->value); } cql_transform_r (ct, cn, pr, client_data); + nmem_destroy(nmem); return ct->error; } diff --git a/src/cqlutil.c b/src/cqlutil.c index 2061325..577844e 100644 --- a/src/cqlutil.c +++ b/src/cqlutil.c @@ -1,4 +1,4 @@ -/* $Id: cqlutil.c,v 1.5 2004-10-03 22:34:07 adam Exp $ +/* $Id: cqlutil.c,v 1.6 2004-11-01 20:13:02 adam Exp $ Copyright (C) 2002-2004 Index Data Aps @@ -15,7 +15,6 @@ See the file LICENSE for details. #include #include -#include #include void cql_fputs(const char *buf, void *client_data) @@ -24,7 +23,7 @@ void cql_fputs(const char *buf, void *client_data) fputs(buf, f); } -struct cql_node *cql_node_dup (struct cql_node *cp) +struct cql_node *cql_node_dup (NMEM nmem, struct cql_node *cp) { struct cql_node *cn = 0; @@ -33,51 +32,52 @@ struct cql_node *cql_node_dup (struct cql_node *cp) switch (cp->which) { case CQL_NODE_ST: - cn = cql_node_mk_sc(cp->u.st.index, + cn = cql_node_mk_sc(nmem, cp->u.st.index, cp->u.st.relation, cp->u.st.term); - cn->u.st.modifiers = cql_node_dup(cp->u.st.modifiers); + cn->u.st.modifiers = cql_node_dup(nmem, cp->u.st.modifiers); cn->u.st.index_uri = cp->u.st.index_uri ? - xstrdup(cp->u.st.index_uri) : 0; + nmem_strdup(nmem, cp->u.st.index_uri) : 0; cn->u.st.relation_uri = cp->u.st.relation_uri ? - xstrdup(cp->u.st.relation_uri) : 0; + nmem_strdup(nmem, cp->u.st.relation_uri) : 0; break; case CQL_NODE_BOOL: - cn = cql_node_mk_boolean(cp->u.boolean.value); - cn->u.boolean.left = cql_node_dup(cp->u.boolean.left); - cn->u.boolean.right = cql_node_dup(cp->u.boolean.right); + cn = cql_node_mk_boolean(nmem, cp->u.boolean.value); + cn->u.boolean.left = cql_node_dup(nmem, cp->u.boolean.left); + cn->u.boolean.right = cql_node_dup(nmem, cp->u.boolean.right); } return cn; } -struct cql_node *cql_node_mk_sc(const char *index, +struct cql_node *cql_node_mk_sc(NMEM nmem, + const char *index, const char *relation, const char *term) { - struct cql_node *p = (struct cql_node *) xmalloc(sizeof(*p)); + struct cql_node *p = (struct cql_node *) nmem_malloc(nmem, sizeof(*p)); p->which = CQL_NODE_ST; p->u.st.index = 0; if (index) - p->u.st.index = xstrdup(index); + p->u.st.index = nmem_strdup(nmem, index); p->u.st.index_uri = 0; p->u.st.term = 0; if (term) - p->u.st.term = xstrdup(term); + p->u.st.term = nmem_strdup(nmem, term); p->u.st.relation = 0; if (relation) - p->u.st.relation = xstrdup(relation); + p->u.st.relation = nmem_strdup(nmem, relation); p->u.st.relation_uri = 0; p->u.st.modifiers = 0; return p; } -struct cql_node *cql_node_mk_boolean(const char *op) +struct cql_node *cql_node_mk_boolean(NMEM nmem, const char *op) { - struct cql_node *p = (struct cql_node *) xmalloc(sizeof(*p)); + struct cql_node *p = (struct cql_node *) nmem_malloc(nmem, sizeof(*p)); p->which = CQL_NODE_BOOL; p->u.boolean.value = 0; if (op) - p->u.boolean.value = xstrdup(op); + p->u.boolean.value = nmem_strdup(nmem, op); p->u.boolean.left = 0; p->u.boolean.right = 0; p->u.boolean.modifiers = 0; @@ -89,7 +89,8 @@ const char *cql_uri() return "info:srw/cql-context-set/1/cql-v1.1"; } -struct cql_node *cql_apply_prefix(struct cql_node *n, const char *prefix, +struct cql_node *cql_apply_prefix(NMEM nmem, + struct cql_node *n, const char *prefix, const char *uri) { if (n->which == CQL_NODE_ST) @@ -101,14 +102,13 @@ struct cql_node *cql_apply_prefix(struct cql_node *n, const char *prefix, strlen(prefix) == (size_t) (cp - n->u.st.index) && !memcmp(n->u.st.index, prefix, strlen(prefix))) { - char *nval = xstrdup(cp+1); - n->u.st.index_uri = xstrdup(uri); - xfree (n->u.st.index); + char *nval = nmem_strdup(nmem, cp+1); + n->u.st.index_uri = nmem_strdup(nmem, uri); n->u.st.index = nval; } else if (!prefix && !cp) { - n->u.st.index_uri = xstrdup(uri); + n->u.st.index_uri = nmem_strdup(nmem, uri); } } if (!n->u.st.relation_uri && n->u.st.relation) @@ -118,17 +118,16 @@ struct cql_node *cql_apply_prefix(struct cql_node *n, const char *prefix, strlen(prefix) == (size_t)(cp - n->u.st.relation) && !memcmp(n->u.st.relation, prefix, strlen(prefix))) { - char *nval = xstrdup(cp+1); - n->u.st.relation_uri = xstrdup(uri); - xfree (n->u.st.relation); + char *nval = nmem_strdup(nmem, cp+1); + n->u.st.relation_uri = nmem_strdup(nmem, uri); n->u.st.relation = nval; } } } else if (n->which == CQL_NODE_BOOL) { - cql_apply_prefix(n->u.boolean.left, prefix, uri); - cql_apply_prefix(n->u.boolean.right, prefix, uri); + cql_apply_prefix(nmem, n->u.boolean.left, prefix, uri); + cql_apply_prefix(nmem, n->u.boolean.right, prefix, uri); } return n; } @@ -140,18 +139,11 @@ void cql_node_destroy(struct cql_node *cn) switch (cn->which) { case CQL_NODE_ST: - xfree (cn->u.st.index); - xfree (cn->u.st.relation); - xfree (cn->u.st.term); - xfree (cn->u.st.index_uri); - xfree (cn->u.st.relation_uri); cql_node_destroy(cn->u.st.modifiers); break; case CQL_NODE_BOOL: - xfree (cn->u.boolean.value); cql_node_destroy(cn->u.boolean.left); cql_node_destroy(cn->u.boolean.right); cql_node_destroy(cn->u.boolean.modifiers); } - xfree (cn); } -- 1.7.10.4