From 5e04d5afa51d563d79e9c3e57eaf5dd8116e1152 Mon Sep 17 00:00:00 2001
From: Virgile Prevosto <virgile.prevosto@m4x.org>
Date: Mon, 21 Mar 2022 18:20:15 +0100
Subject: [PATCH] [kernel] don't forget to clean up when struct comparison
 fails during linking

---
 src/kernel_internals/typing/mergecil.ml       |  1 +
 tests/syntax/oracle/struct_linking.res.oracle | 27 +++++++++++++++++++
 tests/syntax/struct_linking.i                 | 13 +++++++++
 tests/syntax/struct_linking_2.i               | 11 ++++++++
 4 files changed, 52 insertions(+)
 create mode 100644 tests/syntax/oracle/struct_linking.res.oracle
 create mode 100644 tests/syntax/struct_linking.i
 create mode 100644 tests/syntax/struct_linking_2.i

diff --git a/src/kernel_internals/typing/mergecil.ml b/src/kernel_internals/typing/mergecil.ml
index a120eb377d9..d962659e2cc 100644
--- a/src/kernel_internals/typing/mergecil.ml
+++ b/src/kernel_internals/typing/mergecil.ml
@@ -1212,6 +1212,7 @@ and matchCompInfo (oldfidx: int) (oldci: compinfo)
              aggregate_name oldci.cname aggregate_name ci.cname
              old_len len
          in
+         undo ();
          raise (Failure msg)
        end;
        (* We check the fields but watch for Failure. We only do the check when
diff --git a/tests/syntax/oracle/struct_linking.res.oracle b/tests/syntax/oracle/struct_linking.res.oracle
new file mode 100644
index 00000000000..25104361657
--- /dev/null
+++ b/tests/syntax/oracle/struct_linking.res.oracle
@@ -0,0 +1,27 @@
+[kernel] Parsing struct_linking.i (no preprocessing)
+[kernel] Parsing struct_linking_2.i (no preprocessing)
+/* Generated by Frama-C */
+struct Foo {
+   double z ;
+   int t ;
+   char u ;
+};
+struct Foo_0 {
+   int x ;
+   double y ;
+};
+void g(void)
+{
+  struct Foo g_0 = {.z = (double)0, .t = 0, .u = (char)0};
+  g_0.z = 36.0;
+  return;
+}
+
+void f(void)
+{
+  struct Foo_0 f_0 = {.x = 0, .y = 0.};
+  f_0.y = 42.0;
+  return;
+}
+
+
diff --git a/tests/syntax/struct_linking.i b/tests/syntax/struct_linking.i
new file mode 100644
index 00000000000..4da3e10d04b
--- /dev/null
+++ b/tests/syntax/struct_linking.i
@@ -0,0 +1,13 @@
+/* run.config
+OPT: %{deps:@PTEST_NAME@_2.i} -print
+*/
+struct Foo {
+  double z;
+  int t;
+  char u;
+};
+
+void g () {
+  struct Foo g = { 0 };
+  g.z = 36.0;
+}
diff --git a/tests/syntax/struct_linking_2.i b/tests/syntax/struct_linking_2.i
new file mode 100644
index 00000000000..6a3ba3c651e
--- /dev/null
+++ b/tests/syntax/struct_linking_2.i
@@ -0,0 +1,11 @@
+/* run.config
+DONTRUN: main entry of test is in struct_linking.i
+*/
+struct Foo {
+int x; double y;
+};
+
+void f() {
+  struct Foo f = { 0 };
+  f.y = 42.0;
+}
-- 
GitLab