summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZdenek Kabelac <zkabelac@redhat.com>2015-12-03 15:28:01 +0100
committerZdenek Kabelac <zkabelac@redhat.com>2015-12-03 18:01:42 +0100
commit20acc66a237429d81b699b56be57d4d043362098 (patch)
treeb78f87569c86413e349a17b0046eb1a964c479b6
parent20483ead5bbc74b68126dfadec7b5f98f41b3400 (diff)
downloadlvm2-20acc66a237429d81b699b56be57d4d043362098.tar.gz
log: use full buffer size for printf
Pass full buffer size to printf() function - no reason to make buffer 1 char smaller. Also rename locn buffer to message buffer directly since it's not used for anything else. TODO: we may use same buffer also for 'buf[]' since there is no collision - so may safe 1K on stack usage.
-rw-r--r--WHATS_NEW1
-rw-r--r--lib/log/log.c24
2 files changed, 14 insertions, 11 deletions
diff --git a/WHATS_NEW b/WHATS_NEW
index 7ade066bf..c39eae448 100644
--- a/WHATS_NEW
+++ b/WHATS_NEW
@@ -1,5 +1,6 @@
Version 2.02.137 -
=====================================
+ Cleaned logging code for buffer size usage.
Added internal id_read_format_try() function to check and read valid UUID.
Use dm_get_status_mirror() instead of individual parsers.
Add mem pool arg for check_transient_status() target function.
diff --git a/lib/log/log.c b/lib/log/log.c
index c44c3d416..fc52bddec 100644
--- a/lib/log/log.c
+++ b/lib/log/log.c
@@ -264,9 +264,8 @@ void print_log(int level, const char *file, int line, int dm_errno_or_class,
const char *format, ...)
{
va_list ap;
- char buf[1024], locn[4096];
+ char buf[1024], message[4096];
int bufused, n;
- const char *message;
const char *trformat; /* Translated format string */
char *newbuf;
int use_stderr = level & _LOG_STDERR;
@@ -315,17 +314,16 @@ void print_log(int level, const char *file, int line, int dm_errno_or_class,
(_store_errmsg && (level <= _LOG_ERR)) ||
log_once) {
va_start(ap, format);
- n = vsnprintf(locn, sizeof(locn) - 1, trformat, ap);
+ n = vsnprintf(message, sizeof(message), trformat, ap);
va_end(ap);
+ /* When newer glibc returns >= sizeof(locn), we will just log what
+ * has fit into buffer, it's '\0' terminated string */
if (n < 0) {
fprintf(stderr, _("vsnprintf failed: skipping external "
"logging function"));
goto log_it;
}
-
- locn[sizeof(locn) - 1] = '\0';
- message = locn;
}
/* FIXME Avoid pointless use of message buffer when it'll never be read! */
@@ -441,20 +439,24 @@ void print_log(int level, const char *file, int line, int dm_errno_or_class,
_already_logging = 1;
memset(&buf, ' ', sizeof(buf));
bufused = 0;
- if ((n = dm_snprintf(buf, sizeof(buf) - 1,
+ if ((n = dm_snprintf(buf, sizeof(buf),
"%s:%d %s%s", file, line, log_command_name(),
_msg_prefix)) == -1)
goto done;
- bufused += n;
+ bufused += n; /* n does not include '\0' */
va_start(ap, format);
- n = vsnprintf(buf + bufused - 1, sizeof(buf) - bufused - 1,
+ n = vsnprintf(buf + bufused, sizeof(buf) - bufused,
trformat, ap);
va_end(ap);
- bufused += n;
- buf[bufused - 1] = '\n';
+ if (n < 0)
+ goto done;
+
+ bufused += n;
+ if (n >= sizeof(buf))
+ bufused = sizeof(buf) - 1;
done:
buf[bufused] = '\n';
buf[sizeof(buf) - 1] = '\n';