summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author[email protected] <[email protected]>2012-12-22 08:57:26 +0000
committer[email protected] <[email protected]>2012-12-22 08:57:26 +0000
commit0094caa850c878e6e136a8828e82f51985f1072c (patch)
treeb09f567c75ee35637305c42d078ceda37c869c8d
parent6346ab09d8cee8fe471d50a3e5494dad2da5b682 (diff)
fix rt_kprintf issue, which found by Grissiom.
git-svn-id: https://rt-thread.googlecode.com/svn/trunk@2507 bbd45198-f89e-11dd-88c7-29a3b14d5316
-rw-r--r--src/kservice.c10
1 files changed, 9 insertions, 1 deletions
diff --git a/src/kservice.c b/src/kservice.c
index f7de193e70..4bd6c5530f 100644
--- a/src/kservice.c
+++ b/src/kservice.c
@@ -17,6 +17,7 @@
* 2010-04-15 Bernard remove weak definition on ICCM16C compiler
* 2012-07-18 Arda add the alignment display for signed integer
* 2012-11-23 Bernard fix IAR compiler error.
+ * 2012-12-22 Bernard fix rt_kprintf issue, which found by Grissiom.
*/
#include <rtthread.h>
@@ -1077,7 +1078,14 @@ void rt_kprintf(const char *fmt, ...)
static char rt_log_buf[RT_CONSOLEBUF_SIZE];
va_start(args, fmt);
- length = vsnprintf(rt_log_buf, sizeof(rt_log_buf), fmt, args);
+ /* the return value of vsnprintf is the number of bytes that would be
+ * written to buffer had if the size of the buffer been sufficiently
+ * large excluding the terminating null byte. If the output string
+ * would be larger than the rt_log_buf, we have to adjust the output
+ * length. */
+ length = vsnprintf(rt_log_buf, sizeof(rt_log_buf) - 1, fmt, args);
+ if (length > RT_CONSOLEBUF_SIZE - 1)
+ length = RT_CONSOLEBUF_SIZE - 1;
#ifdef RT_USING_DEVICE
if (_console_device == RT_NULL)
{