Skip to content

Commit

Permalink
Maintenance: Remove snprintf wrappers (#623)
Browse files Browse the repository at this point in the history
Remove wrappers that were originally part of a C90 workaround and
are no longer needed now that C99 is required
  • Loading branch information
gardner48 authored Dec 17, 2024
1 parent 4a53449 commit 525c71b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 27 deletions.
8 changes: 4 additions & 4 deletions src/sundials/sundials_logger.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ void sunCreateLogMessage(SUNLogLevel lvl, int rank, const char* scope,
else if (lvl == SUN_LOGLEVEL_INFO) { prefix = "INFO"; }
else if (lvl == SUN_LOGLEVEL_ERROR) { prefix = "ERROR"; }

msg_length = sunsnprintf(NULL, 0, "[%s][rank %d][%s][%s] %s\n", prefix, rank,
scope, label, formatted_txt);
msg_length = snprintf(NULL, 0, "[%s][rank %d][%s][%s] %s\n", prefix, rank,
scope, label, formatted_txt);
*log_msg = (char*)malloc(msg_length + 1);
sunsnprintf(*log_msg, msg_length + 1, "[%s][rank %d][%s][%s] %s\n", prefix,
rank, scope, label, formatted_txt);
snprintf(*log_msg, msg_length + 1, "[%s][rank %d][%s][%s] %s\n", prefix, rank,
scope, label, formatted_txt);
free(formatted_txt);
}

Expand Down
31 changes: 8 additions & 23 deletions src/sundials/sundials_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,6 @@ static inline char* sunCombineFileAndLine(int line, const char* file)
return file_and_line;
}

static inline int sunvsnprintf(char* buffer, size_t bufsz, const char* format,
va_list vlist)
{
int size = 0;
va_list tmp;
va_copy(tmp, vlist);
size = vsnprintf(buffer, bufsz, format, tmp);
va_end(tmp);
return size;
}

static inline int sunsnprintf(char* buffer, size_t bufsz, const char* format, ...)
{
int size = 0;
va_list args;
va_start(args, format);
size = sunvsnprintf(buffer, bufsz, format, args);
va_end(args);
return size;
}

/*
* Implementation of the GNU extension function vasprintf which
* is itself an analog for vsprintf, except it allocates a string
Expand All @@ -63,15 +42,21 @@ static inline int sunvasnprintf(char** str, const char* fmt, va_list args)
int size = 0;

/* compute string length */
size = sunvsnprintf(NULL, 0, fmt, args);
va_list tmp1;
va_copy(tmp1, args);
size = vsnprintf(NULL, 0, fmt, tmp1);
va_end(tmp1);

if (size < 0) { return -1; }

/* add one to size for the null terminator*/
*str = (char*)malloc(size + 1);
if (NULL == *str) { return -1; }

size = vsnprintf(*str, size + 1, fmt, args);
va_list tmp2;
va_copy(tmp2, args);
size = vsnprintf(*str, size + 1, fmt, tmp2);
va_end(tmp2);

return size;
}
Expand Down

0 comments on commit 525c71b

Please sign in to comment.