mirror of
https://github.com/Fishwaldo/Star64_linux.git
synced 2025-06-25 16:11:45 +00:00
seq_buf: Add seq_buf_terminate() API
In the case that the seq_buf buffer needs to be printed directly, add a way to make sure that the buffer is safe to read by forcing a nul terminating character at the end of the string, or the last byte of the buffer if the string has overflowed. Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
This commit is contained in:
parent
5013f454a3
commit
f2616c772c
1 changed files with 25 additions and 0 deletions
|
@ -71,6 +71,31 @@ static inline unsigned int seq_buf_used(struct seq_buf *s)
|
||||||
return min(s->len, s->size);
|
return min(s->len, s->size);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* seq_buf_terminate - Make sure buffer is nul terminated
|
||||||
|
* @s: the seq_buf descriptor to terminate.
|
||||||
|
*
|
||||||
|
* This makes sure that the buffer in @s is nul terminated and
|
||||||
|
* safe to read as a string.
|
||||||
|
*
|
||||||
|
* Note, if this is called when the buffer has overflowed, then
|
||||||
|
* the last byte of the buffer is zeroed, and the len will still
|
||||||
|
* point passed it.
|
||||||
|
*
|
||||||
|
* After this function is called, s->buffer is safe to use
|
||||||
|
* in string operations.
|
||||||
|
*/
|
||||||
|
static inline void seq_buf_terminate(struct seq_buf *s)
|
||||||
|
{
|
||||||
|
if (WARN_ON(s->size == 0))
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (seq_buf_buffer_left(s))
|
||||||
|
s->buffer[s->len] = 0;
|
||||||
|
else
|
||||||
|
s->buffer[s->size - 1] = 0;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* seq_buf_get_buf - get buffer to write arbitrary data to
|
* seq_buf_get_buf - get buffer to write arbitrary data to
|
||||||
* @s: the seq_buf handle
|
* @s: the seq_buf handle
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue