summarylogtreecommitdiffstats
path: root/001-opengl-avoid-allocate-0-byte.patch
blob: da66ced17253d0e453944155b64f7d334810f277 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
From c3105a9ca5957b58da17e4d86ad82943c4ec3b12 Mon Sep 17 00:00:00 2001
From: tytan652 <tytan652@tytanium.xyz>
Date: Wed, 11 Dec 2024 08:45:40 +0100
Subject: [PATCH] libobs-opengl: Avoid trying to allocate 0 byte on Linux

---
 libobs-opengl/gl-egl-common.c | 51 +++++++++++++++++++----------------
 1 file changed, 28 insertions(+), 23 deletions(-)

diff --git a/libobs-opengl/gl-egl-common.c b/libobs-opengl/gl-egl-common.c
index 8854e9d0c673a7..f6f696f698ca12 100644
--- a/libobs-opengl/gl-egl-common.c
+++ b/libobs-opengl/gl-egl-common.c
@@ -297,26 +297,28 @@ static inline bool is_implicit_dmabuf_modifiers_supported(void)
 static inline bool query_dmabuf_formats(EGLDisplay egl_display, EGLint **formats, EGLint *num_formats)
 {
 	EGLint max_formats = 0;
-	EGLint *format_list = NULL;
 
 	if (!glad_eglQueryDmaBufFormatsEXT(egl_display, 0, NULL, &max_formats)) {
 		blog(LOG_ERROR, "Cannot query the number of formats: %s", gl_egl_error_to_string(eglGetError()));
 		return false;
 	}
 
-	format_list = bzalloc(max_formats * sizeof(EGLint));
-	if (!format_list) {
-		blog(LOG_ERROR, "Unable to allocate memory");
-		return false;
-	}
+	if (max_formats != 0) {
+		EGLint *format_list = bzalloc(max_formats * sizeof(EGLint));
+		if (!format_list) {
+			blog(LOG_ERROR, "Unable to allocate memory");
+			return false;
+		}
 
-	if (!glad_eglQueryDmaBufFormatsEXT(egl_display, max_formats, format_list, &max_formats)) {
-		blog(LOG_ERROR, "Cannot query a list of formats: %s", gl_egl_error_to_string(eglGetError()));
-		bfree(format_list);
-		return false;
+		if (!glad_eglQueryDmaBufFormatsEXT(egl_display, max_formats, format_list, &max_formats)) {
+			blog(LOG_ERROR, "Cannot query a list of formats: %s", gl_egl_error_to_string(eglGetError()));
+			bfree(format_list);
+			return false;
+		}
+
+		*formats = format_list;
 	}
 
-	*formats = format_list;
 	*num_formats = max_formats;
 	return true;
 }
@@ -353,21 +355,24 @@ static inline bool query_dmabuf_modifiers(EGLDisplay egl_display, EGLint drm_for
 		return false;
 	}
 
-	EGLuint64KHR *modifier_list = bzalloc(max_modifiers * sizeof(EGLuint64KHR));
-	EGLBoolean *external_only = NULL;
-	if (!modifier_list) {
-		blog(LOG_ERROR, "Unable to allocate memory");
-		return false;
-	}
+	if (max_modifiers != 0) {
+		EGLuint64KHR *modifier_list = bzalloc(max_modifiers * sizeof(EGLuint64KHR));
+		EGLBoolean *external_only = NULL;
+		if (!modifier_list) {
+			blog(LOG_ERROR, "Unable to allocate memory");
+			return false;
+		}
 
-	if (!glad_eglQueryDmaBufModifiersEXT(egl_display, drm_format, max_modifiers, modifier_list, external_only,
-					     &max_modifiers)) {
-		blog(LOG_ERROR, "Cannot query a list of modifiers: %s", gl_egl_error_to_string(eglGetError()));
-		bfree(modifier_list);
-		return false;
+		if (!glad_eglQueryDmaBufModifiersEXT(egl_display, drm_format, max_modifiers, modifier_list,
+						     external_only, &max_modifiers)) {
+			blog(LOG_ERROR, "Cannot query a list of modifiers: %s", gl_egl_error_to_string(eglGetError()));
+			bfree(modifier_list);
+			return false;
+		}
+
+		*modifiers = modifier_list;
 	}
 
-	*modifiers = modifier_list;
 	*n_modifiers = (EGLuint64KHR)max_modifiers;
 	return true;
 }