This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[PATCH] Disallow VECTOR_CSTs with missing elements
- From: Richard Guenther <rguenther at suse dot de>
- To: gcc-patches at gcc dot gnu dot org
- Date: Thu, 29 Jul 2010 12:39:18 +0200 (CEST)
- Subject: [PATCH] Disallow VECTOR_CSTs with missing elements
The user can happily provide not enough elements for the initializer
of a vector variable which eventually leads to build_vector_from_ctor
constructing a VECTOR_CST with an element count that does not match
TYPE_VECTOR_SUBPARTS. Not a good thing to have in our IL (and nothing
existing code expects).
Thus I am testing the following (luckily at least the C fronted
doesn't have a way of using designated initializers for vector
elements - otherwise we'd have to care about the index values
in the constructor as well).
Richard.
2010-07-29 Richard Guenther <rguenther@suse.de>
* tree.c (build_vector): Assert that the vector constant
has enough elements.
(build_vector_from_ctor): Pad with trailing zeros.
Index: gcc/tree.c
===================================================================
*** gcc/tree.c (revision 162637)
--- gcc/tree.c (working copy)
*************** build_vector (tree type, tree vals)
*** 1318,1323 ****
--- 1318,1324 ----
tree v = make_node (VECTOR_CST);
int over = 0;
tree link;
+ unsigned cnt = 0;
TREE_VECTOR_CST_ELTS (v) = vals;
TREE_TYPE (v) = type;
*************** build_vector (tree type, tree vals)
*** 1326,1331 ****
--- 1327,1333 ----
for (link = vals; link; link = TREE_CHAIN (link))
{
tree value = TREE_VALUE (link);
+ cnt++;
/* Don't crash if we get an address constant. */
if (!CONSTANT_CLASS_P (value))
*************** build_vector (tree type, tree vals)
*** 1334,1339 ****
--- 1336,1343 ----
over |= TREE_OVERFLOW (value);
}
+ gcc_assert (cnt == TYPE_VECTOR_SUBPARTS (type));
+
TREE_OVERFLOW (v) = over;
return v;
}
*************** build_vector_from_ctor (tree type, VEC(c
*** 1350,1355 ****
--- 1354,1362 ----
FOR_EACH_CONSTRUCTOR_VALUE (v, idx, value)
list = tree_cons (NULL_TREE, value, list);
+ for (; idx < TYPE_VECTOR_SUBPARTS (type); ++idx)
+ list = tree_cons (NULL_TREE,
+ fold_convert (TREE_TYPE (type), integer_zero_node), list);
return build_vector (type, nreverse (list));
}