Skip to content

Commit 91e90fb

Browse files
committed
libgccjit: Fix UB in gcc_jit_context_new_array_constructor
1 parent 8cdf7b8 commit 91e90fb

File tree

3 files changed

+12
-4
lines changed

3 files changed

+12
-4
lines changed

gcc/jit/jit-recording.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1293,7 +1293,7 @@ recording::context::new_ctor (recording::location *loc,
12931293
result->m_values.reserve (num_values, false);
12941294
result->m_fields.reserve (num_values, false);
12951295

1296-
compound_type *ct = reinterpret_cast<compound_type *>(type);
1296+
compound_type *ct = type->dyn_cast_compound_type ();
12971297
recording::fields *fields = ct->get_fields ();
12981298

12991299
/* The entry point checks that num_values is not greater than

gcc/jit/jit-recording.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,7 @@ class type : public memento
638638
virtual struct_ *dyn_cast_struct () { return NULL; }
639639
virtual vector_type *dyn_cast_vector_type () { return NULL; }
640640
virtual array_type *dyn_cast_array_type () { return NULL; }
641+
virtual compound_type *dyn_cast_compound_type () { return NULL; }
641642
virtual memento_of_get_aligned *dyn_cast_aligned_type () { return NULL; }
642643

643644
/* Is it typesafe to copy to this type from rtype? */
@@ -838,6 +839,7 @@ class decorated_type : public type
838839
type *is_pointer () final override { return m_other_type->is_pointer (); }
839840
type *is_array () final override { return m_other_type->is_array (); }
840841
struct_ *is_struct () final override { return m_other_type->is_struct (); }
842+
bool is_union () const final override { return m_other_type->is_union (); }
841843
bool is_signed () const final override { return m_other_type->is_signed (); }
842844

843845
protected:
@@ -986,6 +988,10 @@ class memento_of_get_aligned : public decorated_type
986988
return m_other_type->dyn_cast_array_type ();
987989
}
988990

991+
compound_type *dyn_cast_compound_type () final override {
992+
return m_other_type->dyn_cast_compound_type ();
993+
}
994+
989995
vector_type *dyn_cast_vector_type () final override {
990996
return m_other_type->dyn_cast_vector_type ();
991997
}
@@ -1256,6 +1262,8 @@ class compound_type : public type
12561262
type *is_array () final override { return NULL; }
12571263
bool is_signed () const final override { return false; }
12581264

1265+
compound_type *dyn_cast_compound_type () final override { return this; }
1266+
12591267
bool has_known_size () const final override { return m_fields != NULL; }
12601268
void set_loc (location * loc) { m_loc = loc; }
12611269

gcc/jit/libgccjit.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1484,7 +1484,7 @@ gcc_jit_context_new_struct_constructor (gcc_jit_context *ctxt,
14841484
"constructor type is not a struct: %s",
14851485
type->get_debug_string ());
14861486

1487-
compound_type *ct = reinterpret_cast<compound_type *>(type);
1487+
compound_type *ct = type->dyn_cast_compound_type ();
14881488
gcc::jit::recording::fields *fields_struct = ct->get_fields ();
14891489
size_t n_fields = fields_struct->length ();
14901490

@@ -1635,7 +1635,7 @@ gcc_jit_context_new_union_constructor (gcc_jit_context *ctxt,
16351635
"constructor type is not an union: %s",
16361636
type->get_debug_string ());
16371637

1638-
compound_type *ct = reinterpret_cast<compound_type *>(type);
1638+
compound_type *ct = type->dyn_cast_compound_type ();
16391639
gcc::jit::recording::fields *fields_union = ct->get_fields ();
16401640
size_t n_fields = fields_union->length ();
16411641

@@ -1732,7 +1732,7 @@ gcc_jit_context_new_array_constructor (gcc_jit_context *ctxt,
17321732
"'values' NULL with non-zero 'num_values'");
17331733

17341734
gcc::jit::recording::array_type *arr_type =
1735-
reinterpret_cast<gcc::jit::recording::array_type*>(type);
1735+
type->dyn_cast_array_type ();
17361736
size_t n_el = arr_type->num_elements ();
17371737

17381738
RETURN_NULL_IF_FAIL_PRINTF2 (

0 commit comments

Comments
 (0)