From 995841624c0d8ce08c4371dd24f489de45ebc1c8 Mon Sep 17 00:00:00 2001 From: Alexander Alekhin Date: Tue, 8 Jun 2021 08:39:06 +0000 Subject: [PATCH 1/3] highgui(gtk): fix NULL ptr checks --- modules/highgui/src/window_gtk.cpp | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/modules/highgui/src/window_gtk.cpp b/modules/highgui/src/window_gtk.cpp index 9bc6257f98..17307ea7f9 100644 --- a/modules/highgui/src/window_gtk.cpp +++ b/modules/highgui/src/window_gtk.cpp @@ -1771,17 +1771,19 @@ static gboolean icvOnClose( GtkWidget* widget, GdkEvent* /*event*/, gpointer use static gboolean icvOnMouse( GtkWidget *widget, GdkEvent *event, gpointer user_data ) { // TODO move this logic to CvImageWidget + // TODO add try-catch wrappers into all callbacks CvWindow* window = (CvWindow*)user_data; + if (!window || !widget || + window->signature != CV_WINDOW_MAGIC_VAL || + window->widget != widget || + !window->on_mouse) + return FALSE; + CvPoint2D32f pt32f = {-1., -1.}; CvPoint pt = {-1,-1}; int cv_event = -1, state = 0, flags = 0; CvImageWidget * image_widget = CV_IMAGE_WIDGET( widget ); - if( window->signature != CV_WINDOW_MAGIC_VAL || - window->widget != widget || !window->widget || - !window->on_mouse /*|| !image_widget->original_image*/) - return FALSE; - if( event->type == GDK_MOTION_NOTIFY ) { GdkEventMotion* event_motion = (GdkEventMotion*)event; @@ -1874,8 +1876,10 @@ static gboolean icvOnMouse( GtkWidget *widget, GdkEvent *event, gpointer user_da pt = cvPointFrom32f( pt32f ); } - if((unsigned)pt.x < (unsigned)(image_widget->original_image->width) && - (unsigned)pt.y < (unsigned)(image_widget->original_image->height) ) + if (!image_widget->original_image/*OpenGL*/ || ( + (unsigned)pt.x < (unsigned)(image_widget->original_image->width) && + (unsigned)pt.y < (unsigned)(image_widget->original_image->height) + )) { flags |= BIT_MAP(state, GDK_SHIFT_MASK, CV_EVENT_FLAG_SHIFTKEY) | BIT_MAP(state, GDK_CONTROL_MASK, CV_EVENT_FLAG_CTRLKEY) | From 5f80f43ff50cde29adee80f381046668c6a6707f Mon Sep 17 00:00:00 2001 From: Alexander Alekhin Date: Wed, 9 Jun 2021 07:31:38 +0000 Subject: [PATCH 2/3] core: fix nSize initialization in cvIplImage() --- modules/core/include/opencv2/core/types_c.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/modules/core/include/opencv2/core/types_c.h b/modules/core/include/opencv2/core/types_c.h index eddbe7d664..fda0f05838 100644 --- a/modules/core/include/opencv2/core/types_c.h +++ b/modules/core/include/opencv2/core/types_c.h @@ -358,7 +358,11 @@ _IplImage needed for correct deallocation */ #if defined(CV__ENABLE_C_API_CTORS) && defined(__cplusplus) - _IplImage() {} + _IplImage() + { + memset(this, 0, sizeof(*this)); // valid for POD structure + nSize = sizeof(IplImage); + } _IplImage(const cv::Mat& m) { *this = cvIplImage(m); } #endif } From bd261040884679a3d90b12bb36e61d84433992ad Mon Sep 17 00:00:00 2001 From: Alexander Alekhin Date: Wed, 9 Jun 2021 09:51:07 +0000 Subject: [PATCH 3/3] python(loader): add workaround to detect and patch sys.path[0] --- modules/python/package/cv2/__init__.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/modules/python/package/cv2/__init__.py b/modules/python/package/cv2/__init__.py index de70872839..f8e631382c 100644 --- a/modules/python/package/cv2/__init__.py +++ b/modules/python/package/cv2/__init__.py @@ -68,8 +68,20 @@ def bootstrap(): if DEBUG: print('OpenCV loader: PYTHON_EXTENSIONS_PATHS={}'.format(str(l_vars['PYTHON_EXTENSIONS_PATHS']))) if DEBUG: print('OpenCV loader: BINARIES_PATHS={}'.format(str(l_vars['BINARIES_PATHS']))) + applySysPathWorkaround = False + if hasattr(sys, 'OpenCV_REPLACE_SYS_PATH_0'): + applySysPathWorkaround = True + else: + try: + BASE_DIR = os.path.dirname(LOADER_DIR) + if sys.path[0] == BASE_DIR or os.path.realpath(sys.path[0]) == BASE_DIR: + applySysPathWorkaround = True + except: + if DEBUG: print('OpenCV loader: exception during checking workaround for sys.path[0]') + pass # applySysPathWorkaround is False + for p in reversed(l_vars['PYTHON_EXTENSIONS_PATHS']): - sys.path.insert(1, p) + sys.path.insert(1 if not applySysPathWorkaround else 0, p) if os.name == 'nt': if sys.version_info[:2] >= (3, 8): # https://github.com/python/cpython/pull/12302