Page MenuHome

Fix T81697: Property search crash with python handlers
ClosedPublic

Authored by Hans Goudey (HooglyBoogly) on Oct 14 2020, 11:57 PM.

Details

Summary

Previously I used CTX_copy to create a mutable copy of the context in
order to set its area and region fields to temporary variables. This was
a tradeoff to avoid casting away const for bContext.

However, it looks like bpy.context is set to this new temporary value.
This is fine for a single wm_draw_update pass, but in the next main
loop, bpy.context is still set to this value, which was freed at the
end of property_search_all_tabs. Maybe there is a way to reset the
bpy.context variable ath the end of the function, but this patch
contains an alternate solution: just don't copy the context. It looks
like this was the only use of CTX_copy anyway, maybe for good reason.

If there's a simple way to reset bpy.context at the end of the all
tab search, I would be happy to do that instead if it was preferred, but
I haven't found it if it exists.

Diff Detail

Repository
rB Blender

Event Timeline

Hans Goudey (HooglyBoogly) requested review of this revision.Oct 14 2020, 11:57 PM
Hans Goudey (HooglyBoogly) created this revision.

While this patch seems fine, I'd like to check on supporting this in the Python API as it should be possible to support copying the context as is done currently.

Checked on supporting this with the Python API, however I'm not so keen on having to link Python into every place we want to override the context.

This could be wrapped into a context backup-restore function, however that's outside the scope of this patch.

For reference.

1diff --git a/source/blender/editors/space_buttons/CMakeLists.txt b/source/blender/editors/space_buttons/CMakeLists.txt
2index ce0787dbdb9..606de7d186b 100644
3--- a/source/blender/editors/space_buttons/CMakeLists.txt
4+++ b/source/blender/editors/space_buttons/CMakeLists.txt
5@@ -23,6 +23,7 @@ set(INC
6 ../../gpu
7 ../../makesdna
8 ../../makesrna
9+ ../../python
10 ../../windowmanager
11 ../../../../intern/glew-mx
12 ../../../../intern/guardedalloc
13@@ -44,11 +45,17 @@ if(WITH_INTERNATIONAL)
14 add_definitions(-DWITH_INTERNATIONAL)
15 endif()
16
17-
18 if(WITH_FREESTYLE)
19 add_definitions(-DWITH_FREESTYLE)
20 endif()
21
22+if(WITH_PYTHON)
23+ add_definitions(-DWITH_PYTHON)
24+ list(APPEND LIB
25+ bf_python
26+ )
27+endif()
28+
29 if(WITH_EXPERIMENTAL_FEATURES)
30 add_definitions(-DWITH_PARTICLE_NODES)
31 add_definitions(-DWITH_HAIR_NODES)
32diff --git a/source/blender/editors/space_buttons/space_buttons.c b/source/blender/editors/space_buttons/space_buttons.c
33index 14817a8ce23..eb326fd1622 100644
34--- a/source/blender/editors/space_buttons/space_buttons.c
35+++ b/source/blender/editors/space_buttons/space_buttons.c
36@@ -52,6 +52,10 @@
37 #include "UI_interface.h"
38 #include "UI_resources.h"
39
40+#ifdef WITH_PYTHON
41+# include "BPY_extern.h"
42+#endif
43+
44 #include "buttons_intern.h" /* own include */
45
46 /* -------------------------------------------------------------------- */
47@@ -402,6 +406,10 @@ static void property_search_all_tabs(const bContext *C,
48 ScrArea area_copy = *CTX_wm_area(C);
49 ARegion *region_copy = BKE_area_region_copy(area_copy.type, main_region);
50 bContext *C_copy = CTX_copy(C);
51+
52+#ifdef WITH_PYTHON
53+ BPY_context_set(C_copy);
54+#endif
55 CTX_wm_area_set(C_copy, &area_copy);
56 CTX_wm_region_set(C_copy, region_copy);
57 SpaceProperties sbuts_copy = *sbuts;
58@@ -437,6 +445,9 @@ static void property_search_all_tabs(const bContext *C,
59 MEM_freeN(region_copy);
60 buttons_free((SpaceLink *)&sbuts_copy);
61 MEM_freeN(C_copy);
62+#ifdef WITH_PYTHON
63+ BPY_context_set((bContext *)C);
64+#endif
65 }
66
67 /**

This revision is now accepted and ready to land.Oct 15 2020, 10:05 AM

The patch itself is fine. But I did notice something separate that I missed earlier.

source/blender/editors/space_buttons/space_buttons.c
403

Eeeh, this shallow copy troubles me, didn't notice that before. What if something modifies data the area points to (hard to tell since we call many general properties-space functions and a draw callback)?
I think we should try to sandbox the off-screen environment.

Logic to copy the SpaceProperties data can also easily get out of sync with buttons_duplicate, so maybe that should be called? I notice there's some special handling for the search data here, but that's not a big issue.

So, may be better to just temporarily override the spacedata list and call ED_area_data_copy() for a deep copy.

Also another point that could break: Code may assume that an area is always inside a screen (or window in case of top-bar/status-bar). So far this was always true I think, now not anymore. Good to be aware of.