Fix #124995: Crashes in rendering code due to invalid material indices #125309

Merged
Falk David merged 2 commits from filedescriptor/blender:fix-124995 into main 2024-07-23 14:35:43 +02:00
4 changed files with 7 additions and 3 deletions

View File

@ -304,6 +304,7 @@ void gpencil_material_resources_get(GPENCIL_MaterialPool *first_pool,
GPUUniformBuf **r_ubo_mat)
{
GPENCIL_MaterialPool *matpool = first_pool;
BLI_assert(mat_id >= 0);
int pool_id = mat_id / GPENCIL_MATERIAL_BUFFER_LEN;
for (int i = 0; i < pool_id; i++) {
matpool = matpool->next;

View File

@ -790,7 +790,10 @@ static GPENCIL_tObject *grease_pencil_object_cache_populate(GPENCIL_PrivateData
visible_strokes.foreach_index([&](const int stroke_i, const int pos) {
const IndexRange points = points_by_curve[stroke_i];
const int material_index = stroke_materials[stroke_i];
/* The material index is allowed to be negative as it's stored as a generic attribure. We
filedescriptor marked this conversation as resolved Outdated

I'd say "can be negative". "shouldn't" makes it sound like it must be prevented (where and by whom?), but in fact any user can write to the material_index attribute, so the code just has to check before using the values.

I'd say "can be negative". _"shouldn't"_ makes it sound like it must be prevented (where and by whom?), but in fact any user can write to the `material_index` attribute, so the code just has to check before using the values.

Updated the comment.

Updated the comment.
* clamp it here to avoid crashing in the rendering code. Any stroke with a material < 0 will
* use the first material in the first material slot.*/
const int material_index = std::max(stroke_materials[stroke_i], 0);
const MaterialGPencilStyle *gp_style = BKE_gpencil_material_settings(ob, material_index + 1);
const bool hide_material = (gp_style->flag & GP_MATERIAL_HIDE) != 0;

View File

@ -697,7 +697,7 @@ bke::CurvesGeometry create_curves_outline(const bke::greasepencil::Drawing &draw
const VArray<int8_t> src_end_caps = *src_attributes.lookup_or_default<int8_t>(
"end_cap", bke::AttrDomain::Curve, GP_STROKE_CAP_ROUND);
const VArray<int> src_material_index = *src_attributes.lookup_or_default(
"material_index", bke::AttrDomain::Curve, -1);
"material_index", bke::AttrDomain::Curve, 0);
/* Transform positions. */
Array<float3> transformed_positions(src_positions.size());

View File

@ -888,7 +888,7 @@ IndexMask retrieve_visible_strokes(Object &object,
/* Get all the strokes that have their material visible. */
const VArray<int> materials = *attributes.lookup_or_default<int>(
"material_index", bke::AttrDomain::Curve, -1);
"material_index", bke::AttrDomain::Curve, 0);
return IndexMask::from_predicate(
curves_range, GrainSize(4096), memory, [&](const int64_t curve_i) {
const int material_index = materials[curve_i];