Commit aee55b05 authored by Milan Broz's avatar Milan Broz

Use fixed buffer in log function.

And unify EOL for error and verbose messages.
parent e58883c1
......@@ -50,7 +50,7 @@
#define MAX_SECTOR_SIZE 4096 /* min page size among all platforms */
#define DEFAULT_DISK_ALIGNMENT 1048576 /* 1MiB */
#define DEFAULT_MEM_ALIGNMENT 4096
#define MAX_ERROR_LENGTH 512
#define LOG_MAX_LEN 4096
#define at_least(a, b) ({ __typeof__(a) __at_least = (a); (__at_least >= (b))?__at_least:(b); })
......
......@@ -154,12 +154,16 @@ void logger(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 (_debug_level)
......@@ -171,7 +175,6 @@ void logger(struct crypt_device *cd, int level, const char *file,
}
va_end(argp);
free(target);
}
static const char *mdata_device_path(struct crypt_device *cd)
......
......@@ -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)))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment