Skip to content
This repository has been archived by the owner on Mar 8, 2020. It is now read-only.

Commit

Permalink
Merge pull request #30 from juanjux/fix/empty_result
Browse files Browse the repository at this point in the history
Don't crash on empty UastFilter result
  • Loading branch information
abeaumont authored Sep 19, 2017
2 parents 9dc8f51 + 62e9dd0 commit f609649
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/uast.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ Nodes *UastFilter(const Uast *ctx, void *node, const char *query) {

// Get array of results
xmlNodeSetPtr result = xpathObj->nodesetval;
if (!result) {
goto error3;
}
xmlNodePtr *results = result->nodeTab;
int size = (result) ? result->nodeNr : 0;

Expand Down
1 change: 1 addition & 0 deletions tests/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ int main() {
TestXmlNewContext);
ADD_TEST(suite, "test failing UastFilter() (bad NodesSetSize)",
TestNodesSetSize);
ADD_TEST(suite, "test empty UastFilter() result", TestEmptyResult);

// run all tests using the CUnit Basic interface
CU_basic_set_mode(CU_BRM_VERBOSE);
Expand Down
10 changes: 10 additions & 0 deletions tests/nodes_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,16 @@ void TestNodeFindError() {
UastFree(ctx);
}

void TestEmptyResult() {
NodeIface iface = IfaceMock();
Uast *ctx = UastNew(iface);
Node module = Node("Module");

CU_ASSERT_FATAL(UastFilter(ctx, &module,
"//Import[@roleImportDeclaration]//alias") == NULL);
UastFree(ctx);
}

void TestXmlNewDoc() {
fail_xmlNewDoc = true;
TestNodeFindError();
Expand Down

0 comments on commit f609649

Please sign in to comment.