Skip to content

Commit 411811b

Browse files
Bert0nskblaschke
authored andcommitted
Fix silent exception swallowing in C API creation functions
1 parent 494269e commit 411811b

3 files changed

Lines changed: 27 additions & 2 deletions

File tree

src/libprojectM/Logging.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,20 @@ auto Logging::HasCallback() -> bool
5353
}
5454

5555
void Logging::Log(const std::string& message, LogLevel severity)
56+
{
57+
Log(message.c_str(), severity);
58+
}
59+
60+
void Logging::Log(const char* message, LogLevel severity)
5661
{
5762
auto callback = GetLoggingCallback();
5863

59-
if (callback.callbackFunction == nullptr)
64+
if (callback.callbackFunction == nullptr || message == nullptr)
6065
{
6166
return;
6267
}
6368

64-
callback.callbackFunction(message.c_str(), static_cast<int>(severity), callback.userData);
69+
callback.callbackFunction(message, static_cast<int>(severity), callback.userData);
6570
}
6671

6772
auto Logging::GetLoggingCallback() -> UserCallback

src/libprojectM/Logging.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,15 @@ class Logging
9191
*/
9292
PROJECTM_CXX_EXPORT static void Log(const std::string& message, LogLevel severity);
9393

94+
/**
95+
* @brief Passes a log message with the given severity to the active thread or global callback.
96+
* This overload avoids implicit string allocations.
97+
* If no callbacks are registered, this function does nothing.
98+
* @param message Null-terminated C string message
99+
* @param severity LogLevel severity
100+
*/
101+
PROJECTM_CXX_EXPORT static void Log(const char* message, LogLevel severity);
102+
94103
/**
95104
* The default log level used if no log level is set (LogLevel::Information)
96105
*/

src/libprojectM/ProjectMCWrapper.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
#include <projectM-4/parameters.h>
1313
#include <projectM-4/render_opengl.h>
1414

15+
#include <exception>
16+
1517
#include <cstring>
1618
#include <sstream>
1719

@@ -76,6 +78,8 @@ projectm_handle projectm_create()
7678

7779
projectm_handle projectm_create_with_opengl_load_proc(void* (*load_proc)(const char*, void*), void* user_data)
7880
{
81+
using libprojectM::Logging;
82+
7983
try
8084
{
8185
// Init resolver to discover gl function pointers (guarded internally, valid to call multiple times)
@@ -95,8 +99,15 @@ projectm_handle projectm_create_with_opengl_load_proc(void* (*load_proc)(const c
9599
auto* projectMInstance = new libprojectM::projectMWrapper();
96100
return reinterpret_cast<projectm_handle>(projectMInstance);
97101
}
102+
catch (const std::exception& e)
103+
{
104+
LOG_ERROR("projectm_create_with_opengl_load_proc caught exception:");
105+
LOG_ERROR(e.what());
106+
return nullptr;
107+
}
98108
catch (...)
99109
{
110+
LOG_ERROR("projectm_create_with_opengl_load_proc caught unknown exception");
100111
return nullptr;
101112
}
102113
}

0 commit comments

Comments
 (0)