Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933071AbbENLjk (ORCPT ); Thu, 14 May 2015 07:39:40 -0400 Received: from szxga03-in.huawei.com ([119.145.14.66]:21315 "EHLO szxga03-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932434AbbENLje (ORCPT ); Thu, 14 May 2015 07:39:34 -0400 From: Wang Long To: , , CC: , , , , , , , , , Subject: [RFC PATCH 07/17] seq_buf: Add seq_buf_can_fit() helper function Date: Thu, 14 May 2015 11:34:54 +0000 Message-ID: <1431603304-162571-8-git-send-email-long.wanglong@huawei.com> X-Mailer: git-send-email 1.8.3.4 In-Reply-To: <1431603304-162571-1-git-send-email-long.wanglong@huawei.com> References: <1431603304-162571-1-git-send-email-long.wanglong@huawei.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.107.197.200] X-CFilter-Loop: Reflected X-Mirapoint-Virus-RAPID-Raw: score=unknown(0), refid=str=0001.0A020203.5554888B.0479,ss=1,re=0.000,recu=0.000,reip=0.000,cl=1,cld=1,fgs=0, ip=0.0.0.0, so=2013-05-26 15:14:31, dmn=2013-03-21 17:37:32 X-Mirapoint-Loop-Id: 2a3363addd72b2aa816e28d85e9bc7cd Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 2958 Lines: 95 From: "Steven Rostedt (Red Hat)" commit 9b77215382b42ef9c5b34293ad3a95332e5b71ef upsteam. Add a seq_buf_can_fit() helper function that removes the possible mistakes of comparing the seq_buf length plus added data compared to the size of the buffer. Link: http://lkml.kernel.org/r/20141118164025.GL23958@pathway.suse.cz Reviewed-by: Petr Mladek [wanglong: backport to 3.10 stable] Signed-off-by: Wang Long Signed-off-by: Steven Rostedt --- kernel/trace/seq_buf.c | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/kernel/trace/seq_buf.c b/kernel/trace/seq_buf.c index ce17f65..6fc9d02 100644 --- a/kernel/trace/seq_buf.c +++ b/kernel/trace/seq_buf.c @@ -17,6 +17,19 @@ #include /** + * seq_buf_can_fit - can the new data fit in the current buffer? + * @s: the seq_buf descriptor + * @len: The length to see if it can fit in the current buffer + * + * Returns true if there's enough unused space in the seq_buf buffer + * to fit the amount of new data according to @len. + */ +static bool seq_buf_can_fit(struct seq_buf *s, size_t len) +{ + return s->len + len < s->size; +} + +/** * seq_buf_print_seq - move the contents of seq_buf into a seq_file * @m: the seq_file descriptor that is the destination * @s: the seq_buf descriptor that is the source. @@ -48,7 +61,7 @@ int seq_buf_vprintf(struct seq_buf *s, const char *fmt, va_list args) if (s->len < s->size) { len = vsnprintf(s->buffer + s->len, s->size - s->len, fmt, args); - if (s->len + len < s->size) { + if (seq_buf_can_fit(s, len)) { s->len += len; return 0; } @@ -137,7 +150,7 @@ int seq_buf_bprintf(struct seq_buf *s, const char *fmt, const u32 *binary) if (s->len < s->size) { ret = bstr_printf(s->buffer + s->len, len, fmt, binary); - if (s->len + ret < s->size) { + if (seq_buf_can_fit(s, ret)) { s->len += ret; return 0; } @@ -161,7 +174,7 @@ int seq_buf_puts(struct seq_buf *s, const char *str) WARN_ON(s->size == 0); - if (s->len + len < s->size) { + if (seq_buf_can_fit(s, len)) { memcpy(s->buffer + s->len, str, len); s->len += len; return 0; @@ -183,7 +196,7 @@ int seq_buf_putc(struct seq_buf *s, unsigned char c) { WARN_ON(s->size == 0); - if (s->len + 1 < s->size) { + if (seq_buf_can_fit(s, 1)) { s->buffer[s->len++] = c; return 0; } @@ -207,7 +220,7 @@ int seq_buf_putmem(struct seq_buf *s, const void *mem, unsigned int len) { WARN_ON(s->size == 0); - if (s->len + len < s->size) { + if (seq_buf_can_fit(s, len)) { memcpy(s->buffer + s->len, mem, len); s->len += len; return 0; -- 1.8.3.4 -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/