[PATCH] fix a crash when loading malformed GraphML files, closes #1141 This fixes...
authorTamas Nepusz <ntamas@gmail.com>
Thu, 6 Dec 2018 08:02:45 +0000 (09:02 +0100)
committerAndreas Tille <tille@debian.org>
Tue, 15 Jan 2019 14:10:32 +0000 (14:10 +0000)
Thanks to Zhao Liang, Huawei Weiran Labs

Gbp-Pq: Name fix_broken_graph_ml.patch

examples/simple/graphml-malformed.xml [new file with mode: 0644]
examples/simple/graphml.c
src/foreign-graphml.c
tests/foreign.at

diff --git a/examples/simple/graphml-malformed.xml b/examples/simple/graphml-malformed.xml
new file mode 100644 (file)
index 0000000..32a879f
--- /dev/null
@@ -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 &quot;w&quot;</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
index ca7b0083747553e580654e2c5a5cd5ed680c86d2..bcd4586304db912bf5b793457ea6c2d0acc40df8 100644 (file)
@@ -94,5 +94,19 @@ int main(int argc, char **argv) {
   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;
 }
index e7ac529e4a0b79431d6f5574022f4885f4347958..00cc9d5fbd8d362c4611f261485f6f98ff4851f0 100644 (file)
@@ -783,7 +783,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 */
index b1918ea01061914588c2a2718096b50c32f0e76d..0a76ce651304cde7833d507e9d8ce2cdf0d28a75 100644 (file)
@@ -29,7 +29,7 @@ AT_CLEANUP
 AT_SETUP([GraphML (igraph_{read,write}_graph_graphml):])
 AT_KEYWORDS([igraph_read_graph_graphml igraph_write_graph_graphml foreign graphml])
 AT_COMPILE_CHECK([simple/graphml.c], [simple/graphml.out], 
-       [simple/{test.gxl,graphml-hsa05010.xml}])
+       [simple/{test.gxl,graphml-hsa05010.xml,graphml-malformed.xml}])
 AT_CLEANUP
 
 AT_SETUP([Writing Pajek (igraph_write_graph_pajek):])