Commit 9b772153 authored by Steven Rostedt (Red Hat)'s avatar Steven Rostedt (Red Hat) Committed by Steven Rostedt

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.czReviewed-by: default avatarPetr Mladek <pmladek@suse.cz>
Signed-off-by: default avatarSteven Rostedt <rostedt@goodmis.org>
parent 820b75f6
...@@ -16,6 +16,19 @@ ...@@ -16,6 +16,19 @@
#include <linux/seq_file.h> #include <linux/seq_file.h>
#include <linux/seq_buf.h> #include <linux/seq_buf.h>
/**
* 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 * seq_buf_print_seq - move the contents of seq_buf into a seq_file
* @m: the seq_file descriptor that is the destination * @m: the seq_file descriptor that is the destination
...@@ -48,7 +61,7 @@ int seq_buf_vprintf(struct seq_buf *s, const char *fmt, va_list args) ...@@ -48,7 +61,7 @@ int seq_buf_vprintf(struct seq_buf *s, const char *fmt, va_list args)
if (s->len < s->size) { if (s->len < s->size) {
len = vsnprintf(s->buffer + s->len, s->size - s->len, fmt, args); 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; s->len += len;
return 0; return 0;
} }
...@@ -137,7 +150,7 @@ int seq_buf_bprintf(struct seq_buf *s, const char *fmt, const u32 *binary) ...@@ -137,7 +150,7 @@ int seq_buf_bprintf(struct seq_buf *s, const char *fmt, const u32 *binary)
if (s->len < s->size) { if (s->len < s->size) {
ret = bstr_printf(s->buffer + s->len, len, fmt, binary); 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; s->len += ret;
return 0; return 0;
} }
...@@ -161,7 +174,7 @@ int seq_buf_puts(struct seq_buf *s, const char *str) ...@@ -161,7 +174,7 @@ int seq_buf_puts(struct seq_buf *s, const char *str)
WARN_ON(s->size == 0); 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); memcpy(s->buffer + s->len, str, len);
s->len += len; s->len += len;
return 0; return 0;
...@@ -183,7 +196,7 @@ int seq_buf_putc(struct seq_buf *s, unsigned char c) ...@@ -183,7 +196,7 @@ int seq_buf_putc(struct seq_buf *s, unsigned char c)
{ {
WARN_ON(s->size == 0); WARN_ON(s->size == 0);
if (s->len + 1 < s->size) { if (seq_buf_can_fit(s, 1)) {
s->buffer[s->len++] = c; s->buffer[s->len++] = c;
return 0; return 0;
} }
...@@ -207,7 +220,7 @@ int seq_buf_putmem(struct seq_buf *s, const void *mem, unsigned int len) ...@@ -207,7 +220,7 @@ int seq_buf_putmem(struct seq_buf *s, const void *mem, unsigned int len)
{ {
WARN_ON(s->size == 0); 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); memcpy(s->buffer + s->len, mem, len);
s->len += len; s->len += len;
return 0; return 0;
......
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