linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Steven Rostedt <rostedt@goodmis.org>
To: linux-kernel@vger.kernel.org
Cc: Ingo Molnar <mingo@kernel.org>,
	Andrew Morton <akpm@linux-foundation.org>,
	Yordan Karadzhov <y.karadz@gmail.com>
Subject: [for-next][PATCH 12/13] seq_buf: Add seq_buf_terminate() API
Date: Fri, 19 Mar 2021 14:34:38 -0400	[thread overview]
Message-ID: <20210319183801.808862244@goodmis.org> (raw)
In-Reply-To: 20210319183426.840228082@goodmis.org

From: "Steven Rostedt (VMware)" <rostedt@goodmis.org>

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>
---
 include/linux/seq_buf.h | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)

diff --git a/include/linux/seq_buf.h b/include/linux/seq_buf.h
index 9d6c28cc4d8f..5b31c5147969 100644
--- a/include/linux/seq_buf.h
+++ b/include/linux/seq_buf.h
@@ -71,6 +71,31 @@ static inline unsigned int seq_buf_used(struct seq_buf *s)
 	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
  * @s: the seq_buf handle
-- 
2.30.1



  parent reply	other threads:[~2021-03-19 18:38 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-19 18:34 [for-next][PATCH 00/13] tracing: Updates for 5.13 Steven Rostedt
2021-03-19 18:34 ` [for-next][PATCH 01/13] ring-buffer: Separate out internal use of ring_buffer_event_time_stamp() Steven Rostedt
2021-03-19 18:34 ` [for-next][PATCH 02/13] ring-buffer: Add a event_stamp to cpu_buffer for each level of nesting Steven Rostedt
2021-03-19 18:34 ` [for-next][PATCH 03/13] tracing: Pass buffer of event to trigger operations Steven Rostedt
2021-03-19 18:34 ` [for-next][PATCH 04/13] ring-buffer: Allow ring_buffer_event_time_stamp() to return time stamp of all events Steven Rostedt
2021-03-19 18:34 ` [for-next][PATCH 05/13] tracing: Use a no_filter_buffering_ref to stop using the filter buffer Steven Rostedt
2021-03-19 18:34 ` [for-next][PATCH 06/13] ring-buffer: Add verifier for using ring_buffer_event_time_stamp() Steven Rostedt
2021-03-19 18:34 ` [for-next][PATCH 07/13] tracing: Add tracing_event_time_stamp() API Steven Rostedt
2021-03-19 18:34 ` [for-next][PATCH 08/13] bootconfig: Update prototype of setup_boot_config() Steven Rostedt
2021-03-19 18:34 ` [for-next][PATCH 09/13] tools/latency-collector: Remove unneeded semicolon Steven Rostedt
2021-03-19 18:34 ` [for-next][PATCH 10/13] ftrace: Fix spelling mistake "disabed" -> "disabled" Steven Rostedt
2021-03-19 18:34 ` [for-next][PATCH 11/13] tracing: Add check of trace event print fmts for dereferencing pointers Steven Rostedt
2021-03-19 18:34 ` Steven Rostedt [this message]
2021-03-19 18:34 ` [for-next][PATCH 13/13] tracing: Add a verifier to check string pointers for trace events Steven Rostedt

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210319183801.808862244@goodmis.org \
    --to=rostedt@goodmis.org \
    --cc=akpm@linux-foundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@kernel.org \
    --cc=y.karadz@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).