diff options
author | Marek Polacek <polacek@redhat.com> | 2024-02-09 12:03:50 -0500 |
---|---|---|
committer | Marek Polacek <polacek@redhat.com> | 2024-02-09 16:40:54 -0500 |
commit | f29f7f86935e29786bf9f976ec99d7639b381b14 (patch) | |
tree | b5c5b33af1b627e104ec6ee250ca2a4b844d2b86 /gcc/cp/semantics.cc | |
parent | 3a3e0f1b46a3ad71ebeedc419393e3a36f1ce6db (diff) | |
download | gcc-f29f7f86935e29786bf9f976ec99d7639b381b14.zip gcc-f29f7f86935e29786bf9f976ec99d7639b381b14.tar.gz gcc-f29f7f86935e29786bf9f976ec99d7639b381b14.tar.bz2 |
c++: fix ICE with __type_pack_element [PR113834]
Here we crash on this invalid code because we seem to infinitely recurse
and end up with __type_pack_element with index that doesn't tree_fits_shwi_p
which then crashes on tree_to_shwi.
Thanks to Jakub for suggesting a nicer fix than my original one.
PR c++/113834
gcc/cp/ChangeLog:
* semantics.cc (finish_type_pack_element): Perform range checking
before tree_to_shwi.
gcc/testsuite/ChangeLog:
* g++.dg/ext/type_pack_element4.C: New test.
Diffstat (limited to 'gcc/cp/semantics.cc')
-rw-r--r-- | gcc/cp/semantics.cc | 7 |
1 files changed, 3 insertions, 4 deletions
diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc index 3299e27..5784017 100644 --- a/gcc/cp/semantics.cc +++ b/gcc/cp/semantics.cc @@ -4650,20 +4650,19 @@ finish_type_pack_element (tree idx, tree types, tsubst_flags_t complain) error ("%<__type_pack_element%> index is not an integral constant"); return error_mark_node; } - HOST_WIDE_INT val = tree_to_shwi (idx); - if (val < 0) + if (tree_int_cst_sgn (idx) < 0) { if (complain & tf_error) error ("%<__type_pack_element%> index is negative"); return error_mark_node; } - if (val >= TREE_VEC_LENGTH (types)) + if (wi::to_widest (idx) >= TREE_VEC_LENGTH (types)) { if (complain & tf_error) error ("%<__type_pack_element%> index is out of range"); return error_mark_node; } - return TREE_VEC_ELT (types, val); + return TREE_VEC_ELT (types, tree_to_shwi (idx)); } /* Implement the __direct_bases keyword: Return the direct base classes |