aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChris Robinson <[email protected]>2019-04-09 22:11:11 -0700
committerChris Robinson <[email protected]>2019-04-09 22:13:21 -0700
commit55a3f38405c6c5e5510a01a28ca0b30648a43b37 (patch)
tree2cc34d52202a1dc82af40600880c3ac57d8cb412
parent460a01443c925013f3f9dadeab5d07373ac5c894 (diff)
Don't use a fixed size buffer for printed messages
-rw-r--r--Alc/helpers.cpp23
-rw-r--r--OpenAL32/alError.cpp32
2 files changed, 31 insertions, 24 deletions
diff --git a/Alc/helpers.cpp b/Alc/helpers.cpp
index 29b613cc..d9133842 100644
--- a/Alc/helpers.cpp
+++ b/Alc/helpers.cpp
@@ -358,13 +358,22 @@ void *GetSymbol(void *handle, const char *name)
void al_print(const char *type, const char *prefix, const char *func, const char *fmt, ...)
{
- char str[1024];
- va_list ap;
-
- va_start(ap, fmt);
- vsnprintf(str, sizeof(str), fmt, ap);
- va_end(ap);
- str[sizeof(str)-1] = 0;
+ al::vector<char> dynmsg;
+ char stcmsg[256];
+ char *str{stcmsg};
+
+ va_list args, args2;
+ va_start(args, fmt);
+ va_copy(args2, args);
+ int msglen{std::vsnprintf(str, sizeof(stcmsg), fmt, args)};
+ if(UNLIKELY(msglen >= 0 && static_cast<size_t>(msglen) >= sizeof(stcmsg)))
+ {
+ dynmsg.resize(static_cast<size_t>(msglen) + 1u);
+ str = dynmsg.data();
+ msglen = std::vsnprintf(str, dynmsg.size(), fmt, args2);
+ }
+ va_end(args2);
+ va_end(args);
std::wstring wstr{utf8_to_wstr(str)};
fprintf(gLogFile, "AL lib: %s %s%s: %ls", type, prefix, func, wstr.c_str());
diff --git a/OpenAL32/alError.cpp b/OpenAL32/alError.cpp
index a02d09c9..2c2a80eb 100644
--- a/OpenAL32/alError.cpp
+++ b/OpenAL32/alError.cpp
@@ -22,6 +22,7 @@
#include <csignal>
#include <cstdarg>
+#include <cstdio>
#ifdef HAVE_WINDOWS_H
#define WIN32_LEAN_AND_MEAN
@@ -36,28 +37,25 @@ ALboolean TrapALError = AL_FALSE;
void alSetError(ALCcontext *context, ALenum errorCode, const char *msg, ...)
{
- char message[1024]{};
+ auto message = al::vector<char>(256);
- va_list args;
+ va_list args, args2;
va_start(args, msg);
- int msglen{vsnprintf(message, sizeof(message), msg, args)};
- va_end(args);
-
- if(msglen < 0 || static_cast<size_t>(msglen) >= sizeof(message))
- {
- message[sizeof(message)-1] = 0;
- msglen = static_cast<int>(strlen(message));
- }
- if(msglen > 0)
- msg = message;
- else
+ va_copy(args2, args);
+ int msglen{std::vsnprintf(message.data(), message.size(), msg, args)};
+ if(msglen >= 0 && static_cast<size_t>(msglen) >= message.size())
{
- msg = "<internal error constructing message>";
- msglen = static_cast<int>(strlen(msg));
+ message.resize(static_cast<size_t>(msglen) + 1u);
+ msglen = std::vsnprintf(message.data(), message.size(), msg, args2);
}
+ va_end(args2);
+ va_end(args);
+
+ if(msglen >= 0) msg = message.data();
+ else msg = "<internal error constructing message>";
+ msglen = static_cast<int>(strlen(msg));
- WARN("Error generated on context %p, code 0x%04x, \"%s\"\n",
- context, errorCode, message);
+ WARN("Error generated on context %p, code 0x%04x, \"%s\"\n", context, errorCode, msg);
if(TrapALError)
{
#ifdef _WIN32