Custom Data: make get_layer_index_n always safely handle n overrun.

This function is used to retrieve the index of the n'th layer of
the specified type, if it exists. Currently the way it handles
an n value that is too big is inconsistent: if there are more
layers of different types following the ones of the correct type,
and n isn't too big, it will safely detect that the corresponding
layer is of a different type and return -1. However, if the value
results in a buffer overrun, it causes an assertion or crash.

This means that safe code should currently ensure n is always
correct, making the type check in this function pointless.
Moreover, checking the range in the caller may incur more
overhead than making this code safe.

This changes the assert into a check to ensure consistent behavior.

Pull Request: https://projects.blender.org/blender/blender/pulls/110812
This commit is contained in:
Alexander Gavrilov
2023-08-04 20:55:53 +03:00
committed by Gitea
parent 85bac9d292
commit 9921c3532c

View File

@@ -2589,8 +2589,8 @@ int CustomData_get_layer_index_n(const CustomData *data, const eCustomDataType t
int i = CustomData_get_layer_index(data, type);
if (i != -1) {
BLI_assert(i + n < data->totlayer);
i = (data->layers[i + n].type == type) ? (i + n) : (-1);
/* If the value of n goes past the block of layers of the correct type, return -1. */
i = (i + n < data->totlayer && data->layers[i + n].type == type) ? (i + n) : (-1);
}
return i;