binpac: Fix array bounds checking

For arrays that are fields within a record, the bounds check was based
on a pointer to the start of the record rather than the start of the
array field.
This commit is contained in:
Jon Siwek 2018-08-28 10:23:24 -05:00 committed by Tim Wojtulewicz
parent de87adf398
commit 46e2490cb0

View file

@ -320,16 +320,18 @@ void ArrayType::GenArrayLength(Output *out_cc, Env *env, const DataPtr& data)
env->RValue(arraylength_var()));
}
out_cc->println("if ( t_begin_of_data + %s > t_end_of_data || "
"t_begin_of_data + %s < t_begin_of_data )",
array_size.c_str(), array_size.c_str());
const char* array_ptr_expr = data.ptr_expr();
out_cc->println("if ( %s + %s > %s || %s + %s < %s )",
array_ptr_expr, array_size.c_str(), env->RValue(end_of_data),
array_ptr_expr, array_size.c_str(), array_ptr_expr);
out_cc->inc_indent();
out_cc->println("throw binpac::ExceptionOutOfBound(\"%s\",",
data_id_str_.c_str());
out_cc->println(" %s, (%s) - (%s));",
array_size.c_str(),
env->RValue(end_of_data),
env->RValue(begin_of_data));
array_ptr_expr);
out_cc->dec_indent();
}
else if ( attr_restofdata_ )