Skip to content

Commit

Permalink
Solr response decoder fixes YAZ-955 (#94)
Browse files Browse the repository at this point in the history
WRBUf leaked and two places where XML text was not encoded properly.
  • Loading branch information
adamdickmeiss authored Jan 9, 2023
1 parent 244f96c commit 49b983c
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 12 deletions.
22 changes: 10 additions & 12 deletions src/solr.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,8 @@ static void extract_text_node(xmlNodePtr node, WRBUF wrbuf)
{
xmlNodePtr child;
for (child = node->children; child ; child = child->next)
{
if (child->type == XML_TEXT_NODE)
wrbuf_puts(wrbuf, (const char *) child->content);
}
wrbuf_xmlputs(wrbuf, (const char *) child->content);
}

static int match_xml_node_attribute(
Expand Down Expand Up @@ -202,8 +200,9 @@ static int yaz_solr_decode_facet_counts(ODR o, xmlNodePtr root,
static void yaz_solr_decode_suggestion_values(xmlNodePtr listPptr, WRBUF wrbuf)
{
xmlNodePtr node;
for (node = listPptr; node; node= node->next)
if (!strcmp((char*) node->name, "lst"))
for (node = listPptr; node; node = node->next)
if (node->type == XML_ELEMENT_NODE
&& !strcmp((const char*) node->name, "lst"))
{
xmlNodePtr child;
for (child = node->children; child; child= child->next)
Expand All @@ -221,7 +220,7 @@ static void yaz_solr_decode_suggestion_values(xmlNodePtr listPptr, WRBUF wrbuf)
static void yaz_solr_decode_suggestion_lst(xmlNodePtr lstPtr, WRBUF wrbuf)
{
xmlNodePtr node;
for (node = lstPtr; node; node= node->next)
for (node = lstPtr; node; node = node->next)
if (match_xml_node_attribute(node, "arr", "name", "suggestion"))
yaz_solr_decode_suggestion_values(node->children, wrbuf);
}
Expand All @@ -231,13 +230,15 @@ static void yaz_solr_decode_misspelled(xmlNodePtr lstPtr, WRBUF wrbuf)
xmlNodePtr node;
for (node = lstPtr; node; node= node->next)
{
if (!strcmp((const char*) node->name, "lst"))
if (node->type == XML_ELEMENT_NODE)
{
const char *misspelled =
yaz_element_attribute_value_get(node, "lst", "name");
if (misspelled)
{
wrbuf_printf(wrbuf, "<misspelled term=\"%s\">\n", misspelled);
wrbuf_printf(wrbuf, "<misspelled term=\"");
wrbuf_xmlputs(wrbuf, misspelled);
wrbuf_printf(wrbuf, "\">\n");
yaz_solr_decode_suggestion_lst(node->children, wrbuf);
wrbuf_puts(wrbuf, "</misspelled>\n");
}
Expand All @@ -251,13 +252,10 @@ static int yaz_solr_decode_spellcheck(ODR o, xmlNodePtr spellcheckPtr,
xmlNodePtr ptr;
WRBUF wrbuf = wrbuf_alloc();
for (ptr = spellcheckPtr->children; ptr; ptr = ptr->next)
{
if (match_xml_node_attribute(ptr, "lst", "name", "suggestions"))
{
yaz_solr_decode_misspelled(ptr->children, wrbuf);
}
}
sr->suggestions = odr_strdup(o, wrbuf_cstr(wrbuf));
wrbuf_destroy(wrbuf);
return 0;
}

Expand Down
67 changes: 67 additions & 0 deletions test/test_solr.c
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,69 @@ void tst_decoding(void)
}
odr_reset(odr);


YAZ_CHECK(check_response(
odr,
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
"<response>\n"
" <lst name=\"responseHeader\">\n"
" <int name=\"status\">0</int>\n"
" <int name=\"QTime\">1</int>\n"
" <lst name=\"params\">"
" <str name=\"start\">0</str>\n"
" <str name=\"q\">@attr 1=title solr</str>\n"
" <str name=\"rows\">0</str>\n"
" </lst>"
" </lst>\n"
" <result name=\"response\" numFound=\"0\" start=\"0\"/>\n"
" <lst name=\"spellcheck\">\n"
" <lst name=\"suggestions\">\n"
" <lst name=\"author\">\n"
" <int name=\"numFound\">1</int>\n"
" <arr name=\"suggestion\">\n"
" <lst>\n"
" <str name=\"word\">w1</str>\n"
" <int name=\"freq\">1</int>\n"
" </lst>\n"
" <foo/>\n"
" <lst>\n"
" <str name=\"word\">w2</str>\n"
" </lst>\n"
" </arr>\n"
" </lst>\n"
" <lst name=\"The &lt; title\">\n"
" <arr name=\"suggestion\">\n"
" <lst>\n"
" <str name=\"word\">a&amp;b</str>\n"
" </lst>\n"
" </arr>\n"
" </lst>\n"
" </lst>\n"
" </lst>"
"</response>\n", &response));
if (response)
{
YAZ_CHECK_EQ(*response->numberOfRecords, 0);
YAZ_CHECK_EQ(response->num_records, 0);
YAZ_CHECK(response->records == 0);
YAZ_CHECK_EQ(response->num_diagnostics, 0);
YAZ_CHECK(response->diagnostics == 0);
YAZ_CHECK(response->nextRecordPosition == 0);
YAZ_CHECK(response->facetList == 0);
YAZ_CHECK(strcmp(response->suggestions,
"<misspelled term=\"author\">\n"
"<suggestion>w1</suggestion>\n"
"<suggestion>w2</suggestion>\n"
"</misspelled>\n"
"<misspelled term=\"The &lt; title\">\n"
"<suggestion>a&amp;b</suggestion>\n"
"</misspelled>\n") == 0);
}
odr_reset(odr);




YAZ_CHECK(
check_response(
odr,
Expand Down Expand Up @@ -299,6 +362,10 @@ void tst_decoding(void)
" <lst name=\"keyword\">\n"
" <int name=\"Soleil\">37</int>\n"
" <int name=\"Sun\">26</int>\n"
" <lst>\n"
" <int name=\"no name property , ignored\">3</int>\n"
" </lst>\n"
" <foreign>x</foreign>\n"
" </lst>\n"
" <lst name=\"empty1\">\n"
" </lst>\n"
Expand Down

0 comments on commit 49b983c

Please sign in to comment.