From c074943dfd914fafd25f59cd43e26b4e8ac499f0 Mon Sep 17 00:00:00 2001 From: Julian Eisel Date: Fri, 14 Aug 2020 16:38:45 +0200 Subject: [PATCH] Fix undefined behavior with --debug-xr Mistake in cb578ca1048d3. Before that, the extension vector was static, to make sure the extension name strings wouldn't get destructed when leaving the function. I didn't think that was an issue and couldn't recreate one, because until the previous commit we wouldn't actually add any extensions to the vector on Windows (the system I tested with). Use C++17's `std::string_view` now, which avoids the string copies `std::string` creates for itself and thus its destruction when leaving the local scope. --- intern/ghost/intern/GHOST_XrContext.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/intern/ghost/intern/GHOST_XrContext.cpp b/intern/ghost/intern/GHOST_XrContext.cpp index 0d8d42a72f7..3dbf539f0c7 100644 --- a/intern/ghost/intern/GHOST_XrContext.cpp +++ b/intern/ghost/intern/GHOST_XrContext.cpp @@ -23,6 +23,7 @@ #include #include #include +#include #include "GHOST_Types.h" #include "GHOST_XrException.h" @@ -348,7 +349,7 @@ static bool openxr_layer_is_available(const std::vector la } static bool openxr_extension_is_available(const std::vector extensions_info, - const std::string &extension_name) + const std::string_view &extension_name) { for (const XrExtensionProperties &ext_info : extensions_info) { if (ext_info.extensionName == extension_name) { @@ -405,7 +406,7 @@ void GHOST_XrContext::getExtensionsToEnable( const std::vector &graphics_binding_types, std::vector &r_ext_names) { - std::vector try_ext; + std::vector try_ext; /* Try enabling debug extension. */ if (isDebugMode()) { @@ -422,9 +423,9 @@ void GHOST_XrContext::getExtensionsToEnable( r_ext_names.push_back(gpu_binding); } - for (const std::string &ext : try_ext) { + for (const std::string_view &ext : try_ext) { if (openxr_extension_is_available(m_oxr->extensions, ext)) { - r_ext_names.push_back(ext.c_str()); + r_ext_names.push_back(ext.data()); } } }