From 5d493e5a4ce6300660debe3e2ad82c21592039ca Mon Sep 17 00:00:00 2001 From: Adam Dickmeiss Date: Tue, 20 Oct 2009 11:14:54 +0200 Subject: [PATCH] ZOOM reject queries that cannot be char converted For queries that cannot be converted to the specified rpnCharset, ZOOM now returns a diagnostic on the client-side ('invalid query' / 10010). This fixes bug #2113. --- include/yaz/query-charset.h | 4 ++++ src/query-charset.c | 53 +++++++++++++++++++++++++++++++------------ src/zoom-c.c | 11 +++++++-- 3 files changed, 51 insertions(+), 17 deletions(-) diff --git a/include/yaz/query-charset.h b/include/yaz/query-charset.h index 2db7385..4c12d7d 100644 --- a/include/yaz/query-charset.h +++ b/include/yaz/query-charset.h @@ -43,6 +43,10 @@ YAZ_EXPORT void yaz_query_charset_convert_apt(Z_AttributesPlusTerm *apt, YAZ_EXPORT void yaz_query_charset_convert_rpnquery(Z_RPNQuery *q, ODR o, yaz_iconv_t cd); + +YAZ_EXPORT int yaz_query_charset_convert_rpnquery_check(Z_RPNQuery *q, + ODR o, yaz_iconv_t cd); + YAZ_END_CDECL #endif diff --git a/src/query-charset.c b/src/query-charset.c index 2aa571b..a875f80 100644 --- a/src/query-charset.c +++ b/src/query-charset.c @@ -13,10 +13,11 @@ #include #include -void yaz_query_charset_convert_buf(char *buf_in, int len_in, - char **buf_out, int *len_out, - ODR o, WRBUF wrbuf, yaz_iconv_t cd) +static int yaz_query_charset_convert_buf(char *buf_in, int len_in, + char **buf_out, int *len_out, + ODR o, WRBUF wrbuf, yaz_iconv_t cd) { + int r = 0; wrbuf_rewind(wrbuf); wrbuf_iconv_write(wrbuf, cd, buf_in, len_in); wrbuf_iconv_reset(wrbuf, cd); @@ -26,56 +27,78 @@ void yaz_query_charset_convert_buf(char *buf_in, int len_in, { /* we assume conversion failed */ *buf_out = buf_in; *len_out = len_in; + r = -1; } else { + /* conversion OK */ *buf_out = (char*) odr_malloc(o, *len_out); memcpy(*buf_out, wrbuf_buf(wrbuf), *len_out); } + return r; } -void yaz_query_charset_convert_term(Z_Term *q, - ODR o, WRBUF wrbuf, yaz_iconv_t cd) +static int yaz_query_charset_convert_term(Z_Term *q, + ODR o, WRBUF wrbuf, yaz_iconv_t cd) { + int r = 0; switch(q->which) { case Z_Term_general: - yaz_query_charset_convert_buf( + r = yaz_query_charset_convert_buf( (char *) q->u.general->buf, q->u.general->len, (char **) &q->u.general->buf, &q->u.general->len, o, wrbuf, cd); break; } + return r; } -void yaz_query_charset_convert_operand(Z_Operand *q, - ODR o, WRBUF wrbuf, yaz_iconv_t cd) +static int yaz_query_charset_convert_operand(Z_Operand *q, + ODR o, WRBUF wrbuf, yaz_iconv_t cd) { + int r = 0; switch(q->which) { case Z_Operand_APT: - yaz_query_charset_convert_term(q->u.attributesPlusTerm->term, - o, wrbuf, cd); + r = yaz_query_charset_convert_term(q->u.attributesPlusTerm->term, + o, wrbuf, cd); break; case Z_Operand_resultSetId: break; case Z_Operand_resultAttr: break; } + return r; } -void yaz_query_charset_convert_structure(Z_RPNStructure *q, - ODR o, WRBUF wrbuf, yaz_iconv_t cd) +static int yaz_query_charset_convert_structure(Z_RPNStructure *q, + ODR o, WRBUF wrbuf, + yaz_iconv_t cd) { + int r = 0; switch(q->which) { case Z_RPNStructure_simple: - yaz_query_charset_convert_operand(q->u.simple, o, wrbuf, cd); + r = yaz_query_charset_convert_operand(q->u.simple, o, wrbuf, cd); break; case Z_RPNStructure_complex: - yaz_query_charset_convert_structure(q->u.complex->s1, o, wrbuf, cd); - yaz_query_charset_convert_structure(q->u.complex->s2, o, wrbuf, cd); + r = yaz_query_charset_convert_structure(q->u.complex->s1, o, wrbuf, cd); + if (r == 0) + r = yaz_query_charset_convert_structure( + q->u.complex->s2, o, wrbuf, cd); break; } + return r; +} + +int yaz_query_charset_convert_rpnquery_check(Z_RPNQuery *q, + ODR o, yaz_iconv_t cd) +{ + int r = 0; + WRBUF wrbuf = wrbuf_alloc(); + r = yaz_query_charset_convert_structure(q->RPNStructure, o, wrbuf, cd); + wrbuf_destroy(wrbuf); + return r; } void yaz_query_charset_convert_rpnquery(Z_RPNQuery *q, diff --git a/src/zoom-c.c b/src/zoom-c.c index e05de20..290fb2f 100644 --- a/src/zoom-c.c +++ b/src/zoom-c.c @@ -1635,12 +1635,19 @@ static zoom_ret ZOOM_connection_send_search(ZOOM_connection c) yaz_iconv_t cd = yaz_iconv_open(cp, "UTF-8"); if (cd) { + int r; search_req->query = yaz_copy_Z_Query(search_req->query, c->odr_out); - yaz_query_charset_convert_rpnquery(search_req->query->u.type_1, - c->odr_out, cd); + r = yaz_query_charset_convert_rpnquery_check( + search_req->query->u.type_1, + c->odr_out, cd); yaz_iconv_close(cd); + if (r) + { /* query could not be char converted */ + set_ZOOM_error(c, ZOOM_ERROR_INVALID_QUERY, 0); + return zoom_complete; + } } } } -- 1.7.10.4