Skip to content

Commit cd59ce1

Browse files
committed
libgccjit: Fix UB in gcc_jit_context_new_array_constructor
1 parent 86c1cca commit cd59ce1

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
@@ -646,6 +646,7 @@ class type : public memento
646646
virtual struct_ *dyn_cast_struct () { return NULL; }
647647
virtual vector_type *dyn_cast_vector_type () { return NULL; }
648648
virtual array_type *dyn_cast_array_type () { return NULL; }
649+
virtual compound_type *dyn_cast_compound_type () { return NULL; }
649650
virtual memento_of_get_aligned *dyn_cast_aligned_type () { return NULL; }
650651

651652
/* Is it typesafe to copy to this type from rtype? */
@@ -846,6 +847,7 @@ class decorated_type : public type
846847
type *is_pointer () final override { return m_other_type->is_pointer (); }
847848
type *is_array () final override { return m_other_type->is_array (); }
848849
struct_ *is_struct () final override { return m_other_type->is_struct (); }
850+
bool is_union () const final override { return m_other_type->is_union (); }
849851
bool is_signed () const final override { return m_other_type->is_signed (); }
850852

851853
protected:
@@ -994,6 +996,10 @@ class memento_of_get_aligned : public decorated_type
994996
return m_other_type->dyn_cast_array_type ();
995997
}
996998

999+
compound_type *dyn_cast_compound_type () final override {
1000+
return m_other_type->dyn_cast_compound_type ();
1001+
}
1002+
9971003
vector_type *dyn_cast_vector_type () final override {
9981004
return m_other_type->dyn_cast_vector_type ();
9991005
}
@@ -1264,6 +1270,8 @@ class compound_type : public type
12641270
type *is_array () final override { return NULL; }
12651271
bool is_signed () const final override { return false; }
12661272

1273+
compound_type *dyn_cast_compound_type () final override { return this; }
1274+
12671275
bool has_known_size () const final override { return m_fields != NULL; }
12681276
void set_loc (location * loc) { m_loc = loc; }
12691277

gcc/jit/libgccjit.cc

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

1497-
compound_type *ct = reinterpret_cast<compound_type *>(type);
1497+
compound_type *ct = type->dyn_cast_compound_type ();
14981498
gcc::jit::recording::fields *fields_struct = ct->get_fields ();
14991499
size_t n_fields = fields_struct->length ();
15001500

@@ -1645,7 +1645,7 @@ gcc_jit_context_new_union_constructor (gcc_jit_context *ctxt,
16451645
"constructor type is not an union: %s",
16461646
type->get_debug_string ());
16471647

1648-
compound_type *ct = reinterpret_cast<compound_type *>(type);
1648+
compound_type *ct = type->dyn_cast_compound_type ();
16491649
gcc::jit::recording::fields *fields_union = ct->get_fields ();
16501650
size_t n_fields = fields_union->length ();
16511651

@@ -1742,7 +1742,7 @@ gcc_jit_context_new_array_constructor (gcc_jit_context *ctxt,
17421742
"'values' NULL with non-zero 'num_values'");
17431743

17441744
gcc::jit::recording::array_type *arr_type =
1745-
reinterpret_cast<gcc::jit::recording::array_type*>(type);
1745+
type->dyn_cast_array_type ();
17461746
size_t n_el = arr_type->num_elements ();
17471747

17481748
RETURN_NULL_IF_FAIL_PRINTF2 (

0 commit comments

Comments
 (0)