This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

C++ PATCH for abi_tag sanity checking


One of the EDG guys pointed out to me that we weren't doing any sanity checking on the arguments to the abi_tag attribute. This patch adds checks to require that the arguments be strings containing valid identifiers, so they work appropriately in mangled names.

Tested x86_64-pc-linux-gnu, applying to trunk.

commit 3c9202343aca0b0a9d74fee4e6843000f3a612cf
Author: Jason Merrill <jason@redhat.com>
Date:   Fri Jan 30 07:45:02 2015 -0500

    	* tree.c (handle_abi_tag_attribute): Diagnose invalid arguments.

diff --git a/gcc/cp/tree.c b/gcc/cp/tree.c
index afb57a3..c51e42d 100644
--- a/gcc/cp/tree.c
+++ b/gcc/cp/tree.c
@@ -3501,6 +3501,50 @@ static tree
 handle_abi_tag_attribute (tree* node, tree name, tree args,
 			  int flags, bool* no_add_attrs)
 {
+  for (tree arg = args; arg; arg = TREE_CHAIN (arg))
+    {
+      tree elt = TREE_VALUE (arg);
+      if (TREE_CODE (elt) != STRING_CST
+	  || (!same_type_ignoring_top_level_qualifiers_p
+	      (strip_array_types (TREE_TYPE (elt)),
+	       char_type_node)))
+	{
+	  error ("arguments to the %qE attribute must be narrow string "
+		 "literals", name);
+	  goto fail;
+	}
+      const char *begin = TREE_STRING_POINTER (elt);
+      const char *end = begin + TREE_STRING_LENGTH (elt);
+      for (const char *p = begin; p != end; ++p)
+	{
+	  char c = *p;
+	  if (p == begin)
+	    {
+	      if (!ISALPHA (c) && c != '_')
+		{
+		  error ("arguments to the %qE attribute must contain valid "
+			 "identifiers", name);
+		  inform (input_location, "%<%c%> is not a valid first "
+			  "character for an identifier", c);
+		  goto fail;
+		}
+	    }
+	  else if (p == end - 1)
+	    gcc_assert (c == 0);
+	  else
+	    {
+	      if (!ISALNUM (c) && c != '_')
+		{
+		  error ("arguments to the %qE attribute must contain valid "
+			 "identifiers", name);
+		  inform (input_location, "%<%c%> is not a valid character "
+			  "in an identifier", c);
+		  goto fail;
+		}
+	    }
+	}
+    }
+
   if (TYPE_P (*node))
     {
       if (!OVERLOAD_TYPE_P (*node))
diff --git a/gcc/testsuite/g++.dg/abi/abi-tag13.C b/gcc/testsuite/g++.dg/abi/abi-tag13.C
new file mode 100644
index 0000000..34e8da3
--- /dev/null
+++ b/gcc/testsuite/g++.dg/abi/abi-tag13.C
@@ -0,0 +1,5 @@
+const char *foo = "bar";
+void __attribute((abi_tag(foo))) f1() {}  // { dg-error "abi_tag" }
+void __attribute((abi_tag(L"foo"))) f2(); // { dg-error "abi_tag" }
+void __attribute((abi_tag("3foo"))) f3(); // { dg-error "abi_tag" }
+void __attribute((abi_tag(1))) f5();	  // { dg-error "abi_tag" }

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]