Fix/workaround crash pasting MTex

Pasting MTex referenced Texture & Object data-blocks without any
check for their validity.

This isn't ideal as it doesn't handle re-allocated ID's,
it just prevents a crash referencing freed data-blocks.
This commit is contained in:
Campbell Barton 2023-06-02 13:33:09 +10:00
parent 3788f70647
commit 12240499ac

@ -2869,7 +2869,22 @@ static void paste_mtex_copybuf(ID *id)
**mtex = blender::dna::shallow_copy(mtexcopybuf);
id_us_plus((ID *)mtexcopybuf.tex);
/* NOTE(@ideasman42): the simple memory copy has no special handling for ID data-blocks.
* Ideally this would use `BKE_copybuffer_*` API's, however for common using
* copy-pasting between slots, the case a users expects to copy between files
* seems quite niche. So, do primitive ID validation. */
/* WARNING: This isn't a fool-proof solution as it's possible memory locations are reused,
* or that the ID was relocated in memory since it was copied.
* it does however guard against references to dangling pointers. */
if ((*mtex)->tex && (BLI_findindex(&G_MAIN->textures, (*mtex)->tex) == -1)) {
(*mtex)->tex = nullptr;
}
if ((*mtex)->object && (BLI_findindex(&G_MAIN->objects, (*mtex)->object) == -1)) {
(*mtex)->object = nullptr;
}
id_us_plus((ID *)(*mtex)->tex);
id_lib_extern((ID *)(*mtex)->object);
}
}