Patchwork [-,C++] Avoid crashing on erroneous static_assert usage

login
register
mail settings
Submitter Jason Merrill
Date Aug. 6, 2012, 3:31 p.m.
Message ID <t51nl8q81idou6naye73tpcg.1344267058623@email.android.com>
Download mbox | patch
Permalink /patch/175387/
State New
Headers show

Comments

Jason Merrill - Aug. 6, 2012, 3:31 p.m.
Ok.

-------- Original Message --------
 From: Dodji Seketeli <dodji@redhat.com>
 Sent: Mon, Aug 6, 2012 10:49 AM
 To: Jason Merrill <jason@redhat.com>
 CC: GCC Patches <gcc-patches@gcc.gnu.org>
 Subject: [PATCH - C++] Avoid crashing on erroneous static_assert usage

When working on something else, I noticed that failing to provide the
second argument to the static_assert operator would lead to an ICE.

Fixed thus, and tested against trunk on x86_64-unknown-linux-gnu.

gcc/cp/

	* semantics.c (finish_static_assert): Don't crash on erroneous
	message or condition.

gcc/testsuite/

	* g++.dg/cpp0x/static_assert8.C: New test.
---
 gcc/cp/semantics.c                          |    6 ++++++
 gcc/testsuite/g++.dg/cpp0x/static_assert8.C |    7 +++++++
 2 files changed, 13 insertions(+), 0 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/cpp0x/static_assert8.C

Patch

diff --git a/gcc/cp/semantics.c b/gcc/cp/semantics.c
index b27e8ab..230e967 100644
--- a/gcc/cp/semantics.c
+++ b/gcc/cp/semantics.c
@@ -5099,6 +5099,12 @@  void
 finish_static_assert (tree condition, tree message, location_t location, 
                       bool member_p)
 {
+  if (message == NULL_TREE
+      || message == error_mark_node
+      || condition == NULL_TREE
+      || condition == error_mark_node)
+    return;
+
   if (check_for_bare_parameter_packs (condition))
     condition = error_mark_node;
 
diff --git a/gcc/testsuite/g++.dg/cpp0x/static_assert8.C b/gcc/testsuite/g++.dg/cpp0x/static_assert8.C
new file mode 100644
index 0000000..ea23afb
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/static_assert8.C
@@ -0,0 +1,7 @@ 
+// { dg-do compile { target c++11 } }
+
+static_assert (1 == 0); // { dg-error "expected (string-literal|',') before" }
+
+static_assert (1 == 0,); // { dg-error "expected string-literal before '\\)'" }
+
+static_assert (1 == 0, "oops"); // { dg-error "static assertion failed" }