Use fixed buffer in log function.

And unify EOL for error and verbose messages.
This commit is contained in:
Milan Broz
2018-04-26 10:00:31 +02:00
parent e58883c183
commit aee55b0595
3 changed files with 15 additions and 11 deletions

View File

@@ -76,17 +76,23 @@ void check_signal(int *r)
*r = -EINTR;
}
#define LOG_MAX_LEN 4096
__attribute__((format(printf, 5, 6)))
void clogger(struct crypt_device *cd, int level, const char *file, int line,
const char *format, ...)
{
va_list argp;
char *target = NULL;
char target[LOG_MAX_LEN + 2];
va_start(argp, format);
if (vasprintf(&target, format, argp) > 0) {
if (vsnprintf(&target[0], LOG_MAX_LEN, format, argp) > 0) {
if (level >= 0) {
/* All verbose and error messages in tools end with EOL. */
if (level == CRYPT_LOG_VERBOSE || level == CRYPT_LOG_ERROR)
strncat(target, "\n", LOG_MAX_LEN);
crypt_log(cd, level, target);
#ifdef CRYPT_DEBUG
} else if (opt_debug)
@@ -98,11 +104,6 @@ void clogger(struct crypt_device *cd, int level, const char *file, int line,
}
va_end(argp);
free(target);
/* All verbose and error messages in tools end with EOL. */
if (level == CRYPT_LOG_VERBOSE || level == CRYPT_LOG_ERROR)
crypt_log(cd, level, "\n");
}
void tool_log(int level, const char *msg, void *usrptr __attribute__((unused)))