KhronosGroup / SPIRV-Cross

SPIRV-Cross is a practical tool and library for performing reflection on SPIR-V and disassembling SPIR-V back to high level languages.
Apache License 2.0
1.96k stars 549 forks source link

[MSVC] Invalid error output in projects which have _HAS_EXCEPTIONS=0 #2273

Closed DmitryTsyganov closed 4 months ago

DmitryTsyganov commented 5 months ago

Hi!

The issue was noticed in a project that contains SPIRV-Cross as a static library and has _HAS_EXCEPTIONS set to 0. Currently, Microsoft STL doesn't copy the exception message if _HAS_EXCEPTIONS=0 (see https://github.com/microsoft/STL/issues/2114). Because of that passing a temporary string object to std::exception constructor would lead to an invalid pointer being saved. This issue affects SPIRV-Cross (if SPIRV_CROSS_EXCEPTIONS_TO_ASSERTIONS is not used) because CompilerError only has one version of a constructor that accepts const std::string&, but I only found const char* being passed to it in SPIRV-Cross codebase, so a temporary std::string is created on each SPIRV_CROSS_THROW invocation.

Here's a minimal example to reproduce (Tested in Visual Studio Community 17.7.4).

#undef _HAS_EXCEPTIONS
#define _HAS_EXCEPTIONS 0

#include <iostream>

#include "spirv_cross_error_handling.hpp"

using namespace SPIRV_CROSS_NAMESPACE;

int main()
{
    try
    {
        SPIRV_CROSS_THROW("test exception message");
    }
    catch (const std::exception& e)
    {
        std::cout << "Exception message: " << e.what() << std::endl;
    }

    return 0;
}