aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSteven Rostedt (Red Hat) <rostedt@goodmis.org>2014-11-14 16:18:14 -0500
committerSteven Rostedt <rostedt@goodmis.org>2014-11-19 22:01:17 -0500
commit9b77215382b42ef9c5b34293ad3a95332e5b71ef (patch)
treec27b128e42421c1d901213c50584e886a0c09af1
parent820b75f63d0152dbb9ff4accf274408592d613f2 (diff)
seq_buf: Add seq_buf_can_fit() helper function
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 <pmladek@suse.cz> Signed-off-by: Steven Rostedt <rostedt@goodmis.org>
-rw-r--r--kernel/trace/seq_buf.c23
1 files changed, 18 insertions, 5 deletions
diff --git a/kernel/trace/seq_buf.c b/kernel/trace/seq_buf.c
index ce17f65268ed..6fc9d021cbef 100644
--- a/kernel/trace/seq_buf.c
+++ b/kernel/trace/seq_buf.c
@@ -17,6 +17,19 @@
17#include <linux/seq_buf.h> 17#include <linux/seq_buf.h>
18 18
19/** 19/**
20 * seq_buf_can_fit - can the new data fit in the current buffer?
21 * @s: the seq_buf descriptor
22 * @len: The length to see if it can fit in the current buffer
23 *
24 * Returns true if there's enough unused space in the seq_buf buffer
25 * to fit the amount of new data according to @len.
26 */
27static bool seq_buf_can_fit(struct seq_buf *s, size_t len)
28{
29 return s->len + len < s->size;
30}
31
32/**
20 * seq_buf_print_seq - move the contents of seq_buf into a seq_file 33 * seq_buf_print_seq - move the contents of seq_buf into a seq_file
21 * @m: the seq_file descriptor that is the destination 34 * @m: the seq_file descriptor that is the destination
22 * @s: the seq_buf descriptor that is the source. 35 * @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)
48 61
49 if (s->len < s->size) { 62 if (s->len < s->size) {
50 len = vsnprintf(s->buffer + s->len, s->size - s->len, fmt, args); 63 len = vsnprintf(s->buffer + s->len, s->size - s->len, fmt, args);
51 if (s->len + len < s->size) { 64 if (seq_buf_can_fit(s, len)) {
52 s->len += len; 65 s->len += len;
53 return 0; 66 return 0;
54 } 67 }
@@ -137,7 +150,7 @@ int seq_buf_bprintf(struct seq_buf *s, const char *fmt, const u32 *binary)
137 150
138 if (s->len < s->size) { 151 if (s->len < s->size) {
139 ret = bstr_printf(s->buffer + s->len, len, fmt, binary); 152 ret = bstr_printf(s->buffer + s->len, len, fmt, binary);
140 if (s->len + ret < s->size) { 153 if (seq_buf_can_fit(s, ret)) {
141 s->len += ret; 154 s->len += ret;
142 return 0; 155 return 0;
143 } 156 }
@@ -161,7 +174,7 @@ int seq_buf_puts(struct seq_buf *s, const char *str)
161 174
162 WARN_ON(s->size == 0); 175 WARN_ON(s->size == 0);
163 176
164 if (s->len + len < s->size) { 177 if (seq_buf_can_fit(s, len)) {
165 memcpy(s->buffer + s->len, str, len); 178 memcpy(s->buffer + s->len, str, len);
166 s->len += len; 179 s->len += len;
167 return 0; 180 return 0;
@@ -183,7 +196,7 @@ int seq_buf_putc(struct seq_buf *s, unsigned char c)
183{ 196{
184 WARN_ON(s->size == 0); 197 WARN_ON(s->size == 0);
185 198
186 if (s->len + 1 < s->size) { 199 if (seq_buf_can_fit(s, 1)) {
187 s->buffer[s->len++] = c; 200 s->buffer[s->len++] = c;
188 return 0; 201 return 0;
189 } 202 }
@@ -207,7 +220,7 @@ int seq_buf_putmem(struct seq_buf *s, const void *mem, unsigned int len)
207{ 220{
208 WARN_ON(s->size == 0); 221 WARN_ON(s->size == 0);
209 222
210 if (s->len + len < s->size) { 223 if (seq_buf_can_fit(s, len)) {
211 memcpy(s->buffer + s->len, mem, len); 224 memcpy(s->buffer + s->len, mem, len);
212 s->len += len; 225 s->len += len;
213 return 0; 226 return 0;