diff mbox series

abg-reader: handle empty corpus nodes in xml representation

Message ID 20200113104214.124662-1-maennich@google.com
State Committed
Headers show
Series abg-reader: handle empty corpus nodes in xml representation | expand

Commit Message

Matthias Maennich via libabigail Jan. 1, 2020, midnight UTC
An abi-corpus might be part of the representation, but might (due to
filters like whitelisting) not contain actual symbols to be considered.
In that case, `abidw` produces an empty abi-corpus node.

Valid ways of representing this in XML are

 -  <abi-corpus path='vmlinux' architecture='elf-arm-aarch64'/>

 -  <abi-corpus path='vmlinux' architecture='elf-arm-aarch64'></abi-corpus>

 -  <abi-corpus path='vmlinux' architecture='elf-arm-aarch64'>
    </abi-corpus>

abg-reader could currently only handle the last format and crashed upon
processing the first two ones. The crash happened due to the XMLNode
having no children, but that was assumed. The last case succeeded so
far as this form actually contains a text node (with the newline
character) as a child.

Fix this by handling the case of a node not having children by exiting
early with an empty node.

	* src/abg-reader.cc (read_corpus_from_input): when assigning a
	corpus node, assure the node actually has children.
	* tests/test-abidiff.cc (main): Add test for variants of empty
	xml nodes to the test harness.
	* tests/data/test-abidiff/test-empty-corpus-0.xml: Test input
	containing an empty xml node that closes immediately.
	* tests/data/test-abidiff/test-empty-corpus-0.xml: Test input
	containing an empty xml node that closes immediately with a tag.
	* tests/data/test-abidiff/test-empty-corpus-0.xml: Test input
	containing an empty xml node that closes with a tag on a new line.
	* tests/data/test-abidiff/test-empty-corpus-report.txt:
	Expected test output (empty abidiff) for diffing xml with itself.
	* tests/data/Makefile.am: Add the new test input material above
	to source distribution.

Signed-off-by: Matthias Maennich <maennich@google.com>
---
 src/abg-reader.cc                              |  9 +++++----
 tests/data/Makefile.am                         |  4 ++++
 .../data/test-abidiff/test-empty-corpus-0.xml  |  3 +++
 .../data/test-abidiff/test-empty-corpus-1.xml  |  3 +++
 .../data/test-abidiff/test-empty-corpus-2.xml  |  4 ++++
 .../test-abidiff/test-empty-corpus-report.txt  |  3 +++
 tests/test-abidiff.cc                          | 18 ++++++++++++++++++
 7 files changed, 40 insertions(+), 4 deletions(-)
 create mode 100644 tests/data/test-abidiff/test-empty-corpus-0.xml
 create mode 100644 tests/data/test-abidiff/test-empty-corpus-1.xml
 create mode 100644 tests/data/test-abidiff/test-empty-corpus-2.xml
 create mode 100644 tests/data/test-abidiff/test-empty-corpus-report.txt
diff mbox series

Patch

diff --git a/src/abg-reader.cc b/src/abg-reader.cc
index eef6432650f7..5bf0d9ccdcb9 100644
--- a/src/abg-reader.cc
+++ b/src/abg-reader.cc
@@ -1867,8 +1867,6 @@  read_corpus_from_input(read_context& ctxt)
 	return nil;
 
       call_reader_next = true;
-
-      ctxt.set_corpus_node(node->children);
     }
   else
     {
@@ -1898,10 +1896,13 @@  read_corpus_from_input(read_context& ctxt)
 	XML_NODE_GET_ATTRIBUTE(node, "soname");
       if (soname_str)
 	corp.set_soname(reinterpret_cast<char*>(soname_str.get()));
-
-      ctxt.set_corpus_node(node->children);
     }
 
+  if (!node->children)
+    return nil;
+
+  ctxt.set_corpus_node(node->children);
+
   corpus& corp = *ctxt.get_corpus();
 
   walk_xml_node_to_map_type_ids(ctxt, node);
diff --git a/tests/data/Makefile.am b/tests/data/Makefile.am
index 16026545fff4..adc95002d26e 100644
--- a/tests/data/Makefile.am
+++ b/tests/data/Makefile.am
@@ -84,6 +84,10 @@  test-abidiff/test-PR18791-v1.so.abi     \
 test-abidiff/test-PR24552-report0.txt	\
 test-abidiff/test-PR24552-v0.abi	\
 test-abidiff/test-PR24552-v1.abi	\
+test-abidiff/test-empty-corpus-0.xml		\
+test-abidiff/test-empty-corpus-1.xml		\
+test-abidiff/test-empty-corpus-2.xml		\
+test-abidiff/test-empty-corpus-report.txt	\
 \
 test-abidiff-exit/test1-voffset-change-report0.txt \
 test-abidiff-exit/test1-voffset-change-report1.txt \
diff --git a/tests/data/test-abidiff/test-empty-corpus-0.xml b/tests/data/test-abidiff/test-empty-corpus-0.xml
new file mode 100644
index 000000000000..aa8059bf956b
--- /dev/null
+++ b/tests/data/test-abidiff/test-empty-corpus-0.xml
@@ -0,0 +1,3 @@ 
+<abi-corpus-group architecture='elf-arm-aarch64'>
+  <abi-corpus path='vmlinux' architecture='elf-arm-aarch64'/>
+</abi-corpus-group>
diff --git a/tests/data/test-abidiff/test-empty-corpus-1.xml b/tests/data/test-abidiff/test-empty-corpus-1.xml
new file mode 100644
index 000000000000..6adf18b9c097
--- /dev/null
+++ b/tests/data/test-abidiff/test-empty-corpus-1.xml
@@ -0,0 +1,3 @@ 
+<abi-corpus-group architecture='elf-arm-aarch64'>
+  <abi-corpus path='vmlinux' architecture='elf-arm-aarch64'></abi-corpus>
+</abi-corpus-group>
diff --git a/tests/data/test-abidiff/test-empty-corpus-2.xml b/tests/data/test-abidiff/test-empty-corpus-2.xml
new file mode 100644
index 000000000000..5e025ff61648
--- /dev/null
+++ b/tests/data/test-abidiff/test-empty-corpus-2.xml
@@ -0,0 +1,4 @@ 
+<abi-corpus-group architecture='elf-arm-aarch64'>
+  <abi-corpus path='vmlinux' architecture='elf-arm-aarch64'>
+  </abi-corpus>
+</abi-corpus-group>
diff --git a/tests/data/test-abidiff/test-empty-corpus-report.txt b/tests/data/test-abidiff/test-empty-corpus-report.txt
new file mode 100644
index 000000000000..a9d032e74e35
--- /dev/null
+++ b/tests/data/test-abidiff/test-empty-corpus-report.txt
@@ -0,0 +1,3 @@ 
+Functions changes summary: 0 Removed, 0 Changed, 0 Added function
+Variables changes summary: 0 Removed, 0 Changed, 0 Added variable
+
diff --git a/tests/test-abidiff.cc b/tests/test-abidiff.cc
index d3118338157c..80e9c0ae5ee6 100644
--- a/tests/test-abidiff.cc
+++ b/tests/test-abidiff.cc
@@ -107,6 +107,24 @@  static InOutSpec specs[] =
     "data/test-abidiff/test-PR24552-report0.txt",
     "output/test-abidiff/test-PR24552-report0.txt"
   },
+  {
+    "data/test-abidiff/test-empty-corpus-0.xml",
+    "data/test-abidiff/test-empty-corpus-0.xml",
+    "data/test-abidiff/test-empty-corpus-report.txt",
+    "output/test-abidiff/test-empty-corpus-report.txt"
+  },
+  {
+    "data/test-abidiff/test-empty-corpus-1.xml",
+    "data/test-abidiff/test-empty-corpus-1.xml",
+    "data/test-abidiff/test-empty-corpus-report.txt",
+    "output/test-abidiff/test-empty-corpus-report.txt"
+  },
+  {
+    "data/test-abidiff/test-empty-corpus-2.xml",
+    "data/test-abidiff/test-empty-corpus-2.xml",
+    "data/test-abidiff/test-empty-corpus-report.txt",
+    "output/test-abidiff/test-empty-corpus-report.txt"
+  },
   // This should be the last entry.
   {0, 0, 0, 0}
 };