@@ -8958,6 +8958,8 @@ struct ibf_object_header {
8958
8958
unsigned int frozen : 1 ;
8959
8959
unsigned int internal : 1 ;
8960
8960
};
8961
+ static const size_t ibf_object_header_align =
8962
+ RUBY_ALIGNOF (struct ibf_object_header );
8961
8963
8962
8964
enum ibf_object_class_index {
8963
8965
IBF_OBJECT_CLASS_OBJECT ,
@@ -9011,11 +9013,21 @@ struct ibf_object_symbol {
9011
9013
long str ;
9012
9014
};
9013
9015
9014
- #define IBF_OBJHEADER (offset ) (struct ibf_object_header *)(load->buff + (offset))
9015
- #define IBF_OBJBODY (type , offset ) (type *)(load->buff + IBF_OBJALIGNED(type, offset))
9016
- #define IBF_OBJALIGNED (type , offset ) \
9017
- (((sizeof(struct ibf_object_header) + (offset) - 1) / RUBY_ALIGNOF(type) + 1) * \
9018
- RUBY_ALIGNOF(type))
9016
+ #define IBF_ALIGNED_OFFSET (align , offset ) /* offset > 0 */ \
9017
+ ((((offset) - 1) / (align) + 1) * (align))
9018
+ #define IBF_OBJHEADER (offset ) (const struct ibf_object_header *)\
9019
+ ibf_load_check_offset(load, IBF_ALIGNED_OFFSET(ibf_object_header_align, offset))
9020
+ #define IBF_OBJBODY (type , offset ) (const type *)\
9021
+ ibf_load_check_offset(load, IBF_ALIGNED_OFFSET(RUBY_ALIGNOF(type), offset))
9022
+
9023
+ static const void *
9024
+ ibf_load_check_offset (const struct ibf_load * load , size_t offset )
9025
+ {
9026
+ if (offset >= (size_t )RSTRING_LEN (load -> str )) {
9027
+ rb_raise (rb_eIndexError , "object offset out of range: %" PRIdSIZE , offset );
9028
+ }
9029
+ return load -> buff + offset ;
9030
+ }
9019
9031
9020
9032
NORETURN (static void ibf_dump_object_unsupported (struct ibf_dump * dump , VALUE obj ));
9021
9033
@@ -9057,7 +9069,7 @@ ibf_dump_object_class(struct ibf_dump *dump, VALUE obj)
9057
9069
static VALUE
9058
9070
ibf_load_object_class (const struct ibf_load * load , const struct ibf_object_header * header , ibf_offset_t offset )
9059
9071
{
9060
- enum ibf_object_class_index * cindexp = IBF_OBJBODY (enum ibf_object_class_index , offset );
9072
+ const enum ibf_object_class_index * cindexp = IBF_OBJBODY (enum ibf_object_class_index , offset );
9061
9073
enum ibf_object_class_index cindex = * cindexp ;
9062
9074
9063
9075
switch (cindex ) {
@@ -9083,7 +9095,7 @@ ibf_dump_object_float(struct ibf_dump *dump, VALUE obj)
9083
9095
static VALUE
9084
9096
ibf_load_object_float (const struct ibf_load * load , const struct ibf_object_header * header , ibf_offset_t offset )
9085
9097
{
9086
- double * dblp = IBF_OBJBODY (double , offset );
9098
+ const double * dblp = IBF_OBJBODY (double , offset );
9087
9099
return DBL2NUM (* dblp );
9088
9100
}
9089
9101
@@ -9295,11 +9307,12 @@ ibf_dump_object_data(struct ibf_dump *dump, VALUE obj)
9295
9307
static VALUE
9296
9308
ibf_load_object_data (const struct ibf_load * load , const struct ibf_object_header * header , ibf_offset_t offset )
9297
9309
{
9298
- const enum ibf_object_data_type * typep = IBF_OBJBODY (enum ibf_object_data_type , offset );
9299
- /* const long *lenp = IBF_OBJBODY(long, offset + sizeof(enum ibf_object_data_type)); */
9300
- const char * data = IBF_OBJBODY (char , offset + sizeof (enum ibf_object_data_type ) + sizeof (long ));
9310
+ const long * body = IBF_OBJBODY (long , offset );
9311
+ const enum ibf_object_data_type type = (enum ibf_object_data_type )body [0 ];
9312
+ /* const long len = body[1]; */
9313
+ const char * data = (const char * )& body [2 ];
9301
9314
9302
- switch (* typep ) {
9315
+ switch (type ) {
9303
9316
case IBF_OBJECT_DATA_ENCODING :
9304
9317
{
9305
9318
VALUE encobj = rb_enc_from_encoding (rb_enc_find (data ));
@@ -9392,10 +9405,13 @@ static ibf_offset_t
9392
9405
ibf_dump_object_object (struct ibf_dump * dump , VALUE obj )
9393
9406
{
9394
9407
struct ibf_object_header obj_header ;
9395
- ibf_offset_t current_offset = ibf_dump_pos ( dump ) ;
9408
+ ibf_offset_t current_offset ;
9396
9409
IBF_ZERO (obj_header );
9397
9410
obj_header .type = TYPE (obj );
9398
9411
9412
+ ibf_dump_align (dump , sizeof (ibf_offset_t ));
9413
+ current_offset = ibf_dump_pos (dump );
9414
+
9399
9415
if (SPECIAL_CONST_P (obj )) {
9400
9416
if (RB_TYPE_P (obj , T_SYMBOL ) ||
9401
9417
RB_TYPE_P (obj , T_FLOAT )) {
@@ -9478,8 +9494,13 @@ ibf_load_object(const struct ibf_load *load, VALUE object_index)
9478
9494
fprintf (stderr , "ibf_load_object: type=%#x special=%d frozen=%d internal=%d\n" ,
9479
9495
header -> type , header -> special_const , header -> frozen , header -> internal );
9480
9496
#endif
9497
+ if ((const char * )(header + 1 ) - load -> buff >= RSTRING_LEN (load -> str )) {
9498
+ rb_raise (
6C43
rb_eIndexError , "object offset out of range: %" PRIdSIZE , offset );
9499
+ }
9500
+ offset = (ibf_offset_t )((const char * )(header + 1 ) - load -> buff );
9501
+
9481
9502
if (header -> special_const ) {
9482
- VALUE * vp = IBF_OBJBODY (VALUE , offset );
9503
+ const VALUE * vp = IBF_OBJBODY (VALUE , offset );
9483
9504
#if IBF_ISEQ_DEBUG
9484
9505
fprintf (stderr , "ibf_load_object: vp=%p\n" , vp );
9485
9506
#endif
0 commit comments