Fix #114428: clamp setting object active_material_index #120434

Merged
Philipp Oeser merged 4 commits from lichtwerk/blender:114428 into main 2024-04-12 17:35:33 +02:00
1 changed files with 2 additions and 0 deletions

View File

@ -1120,6 +1120,8 @@ static int rna_Object_active_material_index_get(PointerRNA *ptr)
static void rna_Object_active_material_index_set(PointerRNA *ptr, int value)
{
Object *ob = reinterpret_cast<Object *>(ptr->owner_id);
Review

There is an error in the code introduced as mat_nr is zero-based and actcol is 1 based, so the assignment should be mat_nr = ob->actcol - 1 however I think it's better to clamp value once at the beginning of the function.

There is an error in the code introduced as `mat_nr` is zero-based and `actcol` is 1 based, so the assignment should be `mat_nr = ob->actcol - 1` however I think it's better to clamp value once at the beginning of the function.
value = std::max(std::min(value, ob->totcol - 1), 0);

There may be no materials which will assert, suggest

value = (ob->totcol > 1) ? std::clamp(value, 0, ob->totcol - 1) : 0;

There may be no materials which will assert, suggest `value = (ob->totcol > 1) ? std::clamp(value, 0, ob->totcol - 1) : 0;`

std::max(std::min(value, ob->totcol - 1), 0); might be even simpler

`std::max(std::min(value, ob->totcol - 1), 0);` might be even simpler
ob->actcol = value + 1;

std::clamp(value, 0, ob->totcol) seems simpler here

`std::clamp(value, 0, ob->totcol)` seems simpler here

std::clamp(value, 0, ob->totcol) seems simpler here

we should really clamp to ob->totcol -1 here then, no?

> `std::clamp(value, 0, ob->totcol)` seems simpler here we should really clamp to `ob->totcol -1` here then, no?
if (ob->type == OB_MESH) {