Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix variable arguments start/end #549

Merged
merged 6 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions include/sundials/priv/sundials_errors_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,12 @@ static inline void SUNHandleErrWithFmtMsg(int line, const char* func,
msglen = (size_t)vsnprintf(NULL, (size_t)0, msgfmt, values); /* determine size
of buffer
needed */
msg = (char*)malloc(msglen + 1);
va_end(values);
msg = (char*)malloc(msglen + 1);
va_start(values, sunctx);
vsnprintf(msg, msglen + 1, msgfmt, values);
SUNHandleErrWithMsg(line, func, file, msg, code, sunctx);
va_end(values);
SUNHandleErrWithMsg(line, func, file, msg, code, sunctx);
free(msg);
}

Expand Down
8 changes: 4 additions & 4 deletions src/sundials/sundials_errors.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,23 +94,23 @@ void SUNGlobalFallbackErrHandler(int line, const char* func, const char* file,
char* log_msg = NULL;
char* file_and_line = NULL;

va_start(ap, err_code);

file_and_line = sunCombineFileAndLine(__LINE__, __FILE__);
va_start(ap, err_code);
sunCreateLogMessage(SUN_LOGLEVEL_ERROR, 0, file_and_line,
__func__, "The SUNDIALS SUNContext was corrupt or NULL when an error occurred. As such, error messages have been printed to stderr.",
ap, &log_msg);
va_end(ap);
fprintf(stderr, "%s", log_msg);
free(log_msg);
free(file_and_line);

file_and_line = sunCombineFileAndLine(line, file);
if (msgfmt == NULL) { msgfmt = SUNGetErrMsg(err_code); }
va_start(ap, err_code);
sunCreateLogMessage(SUN_LOGLEVEL_ERROR, 0, file_and_line, func, msgfmt, ap,
&log_msg);
va_end(ap);
fprintf(stderr, "%s", log_msg);
free(log_msg);
free(file_and_line);

va_end(ap);
}
11 changes: 6 additions & 5 deletions src/sundials/sundials_logger.c
Original file line number Diff line number Diff line change
Expand Up @@ -333,12 +333,12 @@ SUNErrCode SUNLogger_QueueMsg(SUNLogger logger, SUNLogLevel lvl,
return retval;
}

va_list args;
va_start(args, msg_txt);

if (logger->queuemsg)
{
va_list args;
va_start(args, msg_txt);
retval = logger->queuemsg(logger, lvl, scope, label, msg_txt, args);
va_end(args);
}
else
{
Expand All @@ -347,7 +347,10 @@ SUNErrCode SUNLogger_QueueMsg(SUNLogger logger, SUNLogLevel lvl,
if (sunLoggerIsOutputRank(logger, &rank))
{
char* log_msg = NULL;
va_list args;
va_start(args, msg_txt);
sunCreateLogMessage(lvl, rank, scope, label, msg_txt, args, &log_msg);
va_end(args);

switch (lvl)
{
Expand All @@ -372,8 +375,6 @@ SUNErrCode SUNLogger_QueueMsg(SUNLogger logger, SUNLogLevel lvl,
free(log_msg);
}
}

va_end(args);
}
#else
/* silence warnings when all logging is disabled */
Expand Down
15 changes: 9 additions & 6 deletions test/unit_tests/sundials/test_sundials_errors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,22 +171,25 @@ class SUNContextErrFunctionTests : public testing::Test
SUNContext sunctx;
};

void firstHandler(int line, const char* func, const char* file, const char* msg,
SUNErrCode err_code, void* err_user_data, SUNContext sunctx)
static void firstHandler(int line, const char* func, const char* file,
const char* msg, SUNErrCode err_code,
void* err_user_data, SUNContext sunctx)
{
std::vector<int>* order = static_cast<std::vector<int>*>(err_user_data);
order->push_back(0);
}

void secondHandler(int line, const char* func, const char* file, const char* msg,
SUNErrCode err_code, void* err_user_data, SUNContext sunctx)
static void secondHandler(int line, const char* func, const char* file,
const char* msg, SUNErrCode err_code,
void* err_user_data, SUNContext sunctx)
{
std::vector<int>* order = static_cast<std::vector<int>*>(err_user_data);
order->push_back(1);
}

void thirdHandler(int line, const char* func, const char* file, const char* msg,
SUNErrCode err_code, void* err_user_data, SUNContext sunctx)
static void thirdHandler(int line, const char* func, const char* file,
const char* msg, SUNErrCode err_code,
void* err_user_data, SUNContext sunctx)
{
std::vector<int>* order = static_cast<std::vector<int>*>(err_user_data);
order->push_back(2);
Expand Down
Loading