From e3a9566e6463186230f215151b57b893df6d9ce2 Mon Sep 17 00:00:00 2001 From: Tamas Nepusz <ntamas@gmail.com> Date: Thu, 6 Dec 2018 09:02:45 +0100 Subject: [PATCH] fix a crash when loading malformed GraphML files, closes #1141 Thanks to Zhao Liang, Huawei Weiran Labs --- examples/simple/graphml-malformed.xml | 30 +++++++++++++++++++++++++++ examples/simple/graphml.c | 14 +++++++++++++ src/foreign-graphml.c | 13 +++++++++++- tests/foreign.at | 2 +- 4 files changed, 57 insertions(+), 2 deletions(-) create mode 100755 examples/simple/graphml-malformed.xml diff --git a/examples/simple/graphml-malformed.xml b/examples/simple/graphml-malformed.xml new file mode 100755 index 000000000..32a879f67 --- /dev/null +++ b/examples/simple/graphml-malformed.xml @@ -0,0 +1,30 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- y.--> +<graphml xmlns="http://graphml.graphdrawing.org/xmlns" xmxsi="httce" +> + <key id="d0" for="node" attr.name="y" attr.type="string">yellYw</key> + <key id="d1" for="edge" attr.name="wt" attr.type="do"/> + <key id="d2" for="graph" attr.name="date" attr.type="string"></key> + <key id="d3" for="graph" attr.name="ed" attr.type="string"></key> + <key id="d4" for="node" attr.name="gr" attr.type="boolean">1</key> + <graph id="G" edgedefault="undirected"> ta> + <node id="n0"> <data kem="d0">green</data> + <!-- ing --> + <data key="d4">true</data> + </node> + <node id="n1"/> + <node id="n2"> + <data wey="d ">blue</data> + <data key="d4">0</data> </node> <node id="n3"> + <data key="d%">red "w"</data> + </node> + <node id="n4"> <data k5y="d0"><!-- ey --></data> <data key="d4">false</data> + </node> + <node id="n5"> + <data key="d0">t</data> + <data Hey="d4"> + i <key id="sing" for="edge" ae="de"/> + <key id="" atype="double"/> + <key id="sinik" forype="double"/> + <key id="si" for="edge" attme="in" e="de"/> + <id=" \ No newline at end of file diff --git a/src/foreign-graphml.c b/src/foreign-graphml.c index 0e49a7da2..5a00a50fd 100644 --- a/src/foreign-graphml.c +++ b/src/foreign-graphml.c @@ -806,7 +806,18 @@ void igraph_i_graphml_attribute_data_finish(struct igraph_i_graphml_parser_state /* impossible */ break; } - + + if (key == 0) { + /* no key specified, issue a warning */ + igraph_warningf( + "missing attribute key in a <data> tag, ignoring attribute", + __FILE__, __LINE__, 0, + key + ); + igraph_Free(state->data_char); + return; + } + igraph_trie_check(trie, key, &recid); if (recid < 0) { /* no such attribute key, issue a warning */ --- igraph-0.7.1/examples/simple/graphml.c~ 2014-04-15 08:00:46.000000000 -0500 +++ igraph-0.7.1/examples/simple/graphml.c 2019-08-05 08:57:07.885545552 -0500 @@ -93,6 +93,20 @@ igraph_read_graph_graphml(&g, ifile, 0); fclose(ifile); igraph_destroy(&g); + + /* Test a completely malformed GraphML file */ + ifile=fopen("graphml-malformed.xml", "r"); + igraph_set_error_handler(igraph_error_handler_ignore); + igraph_set_warning_handler(igraph_warning_handler_ignore); + result=igraph_read_graph_graphml(&g, ifile, 0); + if (result != IGRAPH_PARSEERROR) { + return 1; + } + fclose(ifile); + igraph_destroy(&g); + + /* Restore the old error handler */ + igraph_set_error_handler(igraph_error_handler_abort); return 0; } --- igraph-0.7.1/tests/foreign.at~ 2014-04-15 08:00:46.000000000 -0500 +++ igraph-0.7.1/tests/foreign.at 2019-08-05 08:59:26.264865437 -0500 @@ -110,5 +110,6 @@ AT_SETUP([LEDA format:]) AT_KEYWORDS([igraph_write_graph_leda LEDA]) -AT_COMPILE_CHECK([simple/igraph_write_graph_leda.c], [simple/igraph_write_graph_leda.out], []) +AT_COMPILE_CHECK([simple/igraph_write_graph_leda.c], [simple/igraph_write_graph_leda.out], + [simple/{test.gxl,graphml-hsa05010.xml,graphml-default-attrs.xml,graphml-namespace.xml,graphml-lenient.xml,graphml-malformed.xml}]) AT_CLEANUP