From 3906c5fd4e50c35e9d892550d90b7256901bf337 Mon Sep 17 00:00:00 2001 From: Simon Fels Date: Fri, 6 Jul 2018 10:18:06 +0200 Subject: [PATCH] cmds: secure against possible nullptrs from EGL and GL --- src/anbox/cmds/system_info.cpp | 24 +++++++++++++++++------- src/anbox/graphics/gl_extensions.h | 4 ++++ 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/anbox/cmds/system_info.cpp b/src/anbox/cmds/system_info.cpp index e97815b..a47f0b7 100644 --- a/src/anbox/cmds/system_info.cpp +++ b/src/anbox/cmds/system_info.cpp @@ -174,9 +174,18 @@ class SystemInformation { auto display = s_egl.eglGetDisplay(0); if (display != EGL_NO_DISPLAY) { s_egl.eglInitialize(display, nullptr, nullptr); - graphics_info_.egl_vendor = s_egl.eglQueryString(display, EGL_VENDOR); - graphics_info_.egl_version = s_egl.eglQueryString(display, EGL_VERSION); - graphics_info_.egl_extensions = anbox::utils::string_split(s_egl.eglQueryString(display, EGL_EXTENSIONS), ' '); + + auto egl_safe_get_string = [](EGLint item) { + auto str = s_gles2.glGetString(item); + if (!str) + return std::string("n/a"); + return std::string(reinterpret_cast(str)); + }; + + graphics_info_.egl_vendor = egl_safe_get_string(EGL_VENDOR); + graphics_info_.egl_version = egl_safe_get_string(EGL_VERSION); + const auto egl_extensions = egl_safe_get_string(EGL_EXTENSIONS); + graphics_info_.egl_extensions = anbox::utils::string_split(egl_extensions, ' '); GLint config_attribs[] = {EGL_SURFACE_TYPE, EGL_PBUFFER_BIT, EGL_RENDERABLE_TYPE, EGL_OPENGL_ES_BIT, @@ -192,16 +201,17 @@ class SystemInformation { // glGetString will return null below which we handle correctly. s_egl.eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, context); - auto safe_get_string = [](GLint item) { + auto gl_safe_get_string = [](GLint item) { auto str = s_gles2.glGetString(item); if (!str) return std::string("n/a"); return std::string(reinterpret_cast(str)); }; - graphics_info_.gles2_vendor = safe_get_string(GL_VENDOR); - graphics_info_.gles2_version = safe_get_string(GL_VERSION); - graphics_info_.gles2_extensions = anbox::utils::string_split(safe_get_string(GL_EXTENSIONS), ' '); + graphics_info_.gles2_vendor = gl_safe_get_string(GL_VENDOR); + graphics_info_.gles2_version = gl_safe_get_string(GL_VERSION); + const auto gl_extensions = gl_safe_get_string(GL_EXTENSIONS); + graphics_info_.gles2_extensions = anbox::utils::string_split(gl_extensions, ' '); s_egl.eglMakeCurrent(display, nullptr, nullptr, nullptr); s_egl.eglDestroyContext(display, context); diff --git a/src/anbox/graphics/gl_extensions.h b/src/anbox/graphics/gl_extensions.h index 7b5738f..1b91e5f 100644 --- a/src/anbox/graphics/gl_extensions.h +++ b/src/anbox/graphics/gl_extensions.h @@ -31,6 +31,9 @@ class GLExtensions { } bool support(char const* ext) const { + if (!ext) + throw std::invalid_argument("Invalid extension name"); + char const* ext_ptr = extensions; size_t const len = strlen(ext); while ((ext_ptr = strstr(ext_ptr, ext)) != nullptr) { @@ -38,6 +41,7 @@ class GLExtensions { break; ext_ptr += len; } + return ext_ptr != nullptr; }