From a0e4e3734126a7e76a871fa671be952de5c13dd8 Mon Sep 17 00:00:00 2001 From: tytan652 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; }