Skip to contentSkip to conversation

seq_buf: add seq_buf_strlen(), seq_buf_init_append(), seq_buf_puts_trunc()

PATCH Superseded Bill Wendling 3 participants 7 messages Sep 16 – Sep 17 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 seq_buf: add seq_buf_strlen(), seq_buf_init_append(), seq_buf_puts_trunc()

Several strlcat() call sites being converted to seq_buf in this series need behavior seq_buf doesn't currently provide, either directly or without introducing subtle bugs:

- seq_buf_used() reports the full buffer size when the buffer is completely filled, even though seq_buf_str() then overwrites the final byte with a NUL terminator, leaving only size - 1 bytes of actual content. Callers that need the true length of the NUL-terminated string have to fall back to strlen(seq_buf_str(s)). Add seq_buf_strlen(), which mirrors seq_buf_str()'s NUL-termination logic but returns the resulting string's length directly.

- seq_buf_init() always clears the buffer it's given via seq_buf_clear(). Code migrating from strlcat(buf, ...), which appends to whatever @buf already contains, can't use seq_buf_init() without silently discarding that existing content. Add seq_buf_init_append(), which preserves it and positions the seq_buf to append after it.

- seq_buf_puts() (like seq_buf_printf() and friends) writes nothing at all if the string doesn't fully fit, whereas strlcat() always copies as much of the source as there is room for. Converting a strlcat() call site that relied on that partial-copy behavior to plain seq_buf_puts() can silently drop content that used to survive truncated. Add seq_buf_puts_trunc(), which keeps the leading bytes of the string that fit.

---
 include/linux/seq_buf.h | 60 +++++++++++++++++++++++++++++++++++++++++
 lib/seq_buf.c           | 35 ++++++++++++++++++++++++
 2 files changed, 95 insertions(+)
include/linux/seq_buf.h +60 -0
index 9f2839e73f8a..a552bcaab07f 100644
@@ -30,6 +30,10 @@ struct seq_buf {
 		.size = SIZE,				\
 	}
 
+/**
+ * seq_buf_clear - reset the seq_buf to be read / appended from the beginning
+ * @s: the seq_buf handle
+ */
 static inline void seq_buf_clear(struct seq_buf *s)
 {
 	s->len = 0;
@@ -37,6 +41,14 @@ static inline void seq_buf_clear(struct seq_buf *s)
 		s->buffer[0] = '\0';
 }
 
+/**
+ * seq_buf_init - initialize a seq_buf
+ * @s: the seq_buf handle
+ * @buf: pointer to the buffer
+ * @size: total size of @buf
+ *
+ * The contents of the buffer are ignored.
+ */
 static inline void
 seq_buf_init(struct seq_buf *s, char *buf, unsigned int size)
 {
@@ -45,6 +57,26 @@ seq_buf_init(struct seq_buf *s, char *buf, unsigned int size)
 	seq_buf_clear(s);
 }
 
+/**
+ * seq_buf_init_append - initialize a seq_buf over a buffer that may
+ *			 already hold NUL-terminated content
+ * @s: the seq_buf handle
+ * @buf: pointer to the (possibly non-empty) buffer
+ * @size: total size of @buf
+ *
+ * Unlike seq_buf_init(), which always clears @buf, this preserves
+ * whatever NUL-terminated content @buf already holds and positions
+ * @s to append after it. Useful for converting code that used to
+ * append to an existing buffer with strlcat()/scnprintf() and friends.
+ */
+static inline void
+seq_buf_init_append(struct seq_buf *s, char *buf, unsigned int size)
+{
+	s->buffer = buf;
+	s->size = size;
+	s->len = strnlen(buf, size);
+}
+
 /*
  * seq_buf have a buffer that might overflow. When this happens
  * len is set to be greater than size.
@@ -108,6 +140,33 @@ static inline const char *seq_buf_str(struct seq_buf *s)
 	return s->buffer;
 }
 
+/**
+ * seq_buf_strlen - get the length of the NUL-terminated string in seq_buf
+ * @s: the seq_buf handle
+ *
+ * Like seq_buf_str(), this makes sure that the buffer in @s is
+ * NUL-terminated, and returns the length of the resulting string.
+ * Unlike seq_buf_used(), the returned length is always correct, even
+ * when the buffer is completely full: in that case seq_buf_used()
+ * reports @s->size, but the last byte was overwritten with the
+ * trailing NUL, so only @s->size - 1 bytes of content remain.
+ *
+ * Returns: the length of the NUL-terminated string in @s->buffer.
+ */
+static inline size_t seq_buf_strlen(struct seq_buf *s)
+{
+	if (WARN_ON(s->size == 0))
+		return 0;
+
+	if (seq_buf_buffer_left(s)) {
+		s->buffer[s->len] = 0;
+		return s->len;
+	}
+
+	s->buffer[s->size - 1] = 0;
+	return s->size - 1;
+}
+
 /**
  * seq_buf_get_buf - get buffer to write arbitrary data to
  * @s: the seq_buf handle
@@ -179,6 +238,7 @@ extern int seq_buf_putmem(struct seq_buf *s, const void *mem, unsigned int len);
 extern int seq_buf_putmem_hex(struct seq_buf *s, const void *mem,
 			      unsigned int len);
 extern int seq_buf_path(struct seq_buf *s, const struct path *path, const char *esc);
+extern size_t seq_buf_puts_trunc(struct seq_buf *s, const char *str);
 extern int seq_buf_hex_dump(struct seq_buf *s, const char *prefix_str,
 			    int prefix_type, int rowsize, int groupsize,
 			    const void *buf, size_t len, bool ascii);
lib/seq_buf.c +35 -0
index a92093f346da..4d56ac71fafe 100644
@@ -376,6 +376,41 @@ int seq_buf_to_user(struct seq_buf *s, char __user *ubuf, size_t start, int cnt)
 	return cnt - ret;
 }
 
+/**
+ * seq_buf_puts_trunc - append as much of a string as fits, keeping any of it
+ * @s: the seq_buf handle
+ * @str: the string to append
+ *
+ * seq_buf_puts() writes nothing at all if @str doesn't fully fit,
+ * unlike strlcat()/strscpy(), which copy as much of the source as
+ * there is room for. That all-or-nothing behavior is usually what's
+ * wanted for building diagnostic/trace text, but it's the wrong
+ * choice when converting code that relied on strlcat()'s always-copy-
+ * what-fits truncation to avoid losing content that was already
+ * appended. This copies the leading bytes of @str that fit, reserving
+ * room for the NUL terminator later added by seq_buf_str().
+ *
+ * Unlike seq_buf_puts(), this does NOT NUL-terminate @s->buffer as it
+ * goes (it copies raw bytes via seq_buf_putmem(), not @str's own
+ * terminator). Callers MUST call seq_buf_str() or seq_buf_strlen()
+ * before using @s->buffer as a C string.
+ *
+ * Returns: the number of bytes copied from @str.
+ */
+size_t seq_buf_puts_trunc(struct seq_buf *s, const char *str)
+{
+	size_t left = seq_buf_buffer_left(s);
+	size_t len;
+
+	if (left <= 1)
+		return 0;
+
+	len = strnlen(str, left - 1);
+	seq_buf_putmem(s, str, len);
+
+	return len;
+}
+
 /**
  * seq_buf_hex_dump - print formatted hex dump into the sequence buffer
  * @s: seq_buf descriptor
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
#1 lore raw

PATCH v2 1/1 seq_buf: add seq_buf_strlen(), seq_buf_init_append(), seq_buf_puts_trunc()

Converting some strlcat() call sites seq_buf need behavior seq_buf doesn't currently provide, either directly or without introducing subtle bugs:

- seq_buf_strlen(): seq_buf_used() reports the full buffer size when the buffer is completely filled, even though seq_buf_str() then overwrites the final byte with a NUL terminator, leaving only size - 1 bytes of actual content. Callers that need the true length of the NUL-terminated string have to fall back to strlen(seq_buf_str(s)). seq_buf_strlen() mirrors seq_buf_str()'s NUL-termination logic but returns the resulting string's length directly.

- seq_buf_init_append(): seq_buf_init() always clears the buffer it's given via seq_buf_clear(). Code migrating from strlcat(buf, ...), which appends to whatever @buf already contains, can't use seq_buf_init() without silently discarding that existing content. seq_buf_init_append() preserves it and positions the seq_buf to append after it.

- seq_buf_puts_trunc(): seq_buf_puts() (like seq_buf_printf() and friends) writes nothing at all if the string doesn't fully fit, whereas strlcat() always copies as much of the source as there is room for. Converting a strlcat() call site that relied on that partial-copy behavior to plain seq_buf_puts() can silently drop content that used to survive truncated. seq_buf_puts_trunc() keeps the leading bytes of the string that fit.

---
v2: Reword the commit message to be clearer and not refer to a series of
patches

---
 include/linux/seq_buf.h | 60 +++++++++++++++++++++++++++++++++++++++++
 lib/seq_buf.c           | 35 ++++++++++++++++++++++++
 2 files changed, 95 insertions(+)
include/linux/seq_buf.h +60 -0
index 9f2839e73f8a..a552bcaab07f 100644
@@ -30,6 +30,10 @@ struct seq_buf {
 		.size = SIZE,				\
 	}
 
+/**
+ * seq_buf_clear - reset the seq_buf to be read / appended from the beginning
+ * @s: the seq_buf handle
+ */
 static inline void seq_buf_clear(struct seq_buf *s)
 {
 	s->len = 0;
@@ -37,6 +41,14 @@ static inline void seq_buf_clear(struct seq_buf *s)
 		s->buffer[0] = '\0';
 }
 
+/**
+ * seq_buf_init - initialize a seq_buf
+ * @s: the seq_buf handle
+ * @buf: pointer to the buffer
+ * @size: total size of @buf
+ *
+ * The contents of the buffer are ignored.
+ */
 static inline void
 seq_buf_init(struct seq_buf *s, char *buf, unsigned int size)
 {
@@ -45,6 +57,26 @@ seq_buf_init(struct seq_buf *s, char *buf, unsigned int size)
 	seq_buf_clear(s);
 }
 
+/**
+ * seq_buf_init_append - initialize a seq_buf over a buffer that may
+ *			 already hold NUL-terminated content
+ * @s: the seq_buf handle
+ * @buf: pointer to the (possibly non-empty) buffer
+ * @size: total size of @buf
+ *
+ * Unlike seq_buf_init(), which always clears @buf, this preserves
+ * whatever NUL-terminated content @buf already holds and positions
+ * @s to append after it. Useful for converting code that used to
+ * append to an existing buffer with strlcat()/scnprintf() and friends.
+ */
+static inline void
+seq_buf_init_append(struct seq_buf *s, char *buf, unsigned int size)
+{
+	s->buffer = buf;
+	s->size = size;
+	s->len = strnlen(buf, size);
+}
+
 /*
  * seq_buf have a buffer that might overflow. When this happens
  * len is set to be greater than size.
@@ -108,6 +140,33 @@ static inline const char *seq_buf_str(struct seq_buf *s)
 	return s->buffer;
 }
 
+/**
+ * seq_buf_strlen - get the length of the NUL-terminated string in seq_buf
+ * @s: the seq_buf handle
+ *
+ * Like seq_buf_str(), this makes sure that the buffer in @s is
+ * NUL-terminated, and returns the length of the resulting string.
+ * Unlike seq_buf_used(), the returned length is always correct, even
+ * when the buffer is completely full: in that case seq_buf_used()
+ * reports @s->size, but the last byte was overwritten with the
+ * trailing NUL, so only @s->size - 1 bytes of content remain.
+ *
+ * Returns: the length of the NUL-terminated string in @s->buffer.
+ */
+static inline size_t seq_buf_strlen(struct seq_buf *s)
+{
+	if (WARN_ON(s->size == 0))
+		return 0;
+
+	if (seq_buf_buffer_left(s)) {
+		s->buffer[s->len] = 0;
+		return s->len;
+	}
+
+	s->buffer[s->size - 1] = 0;
+	return s->size - 1;
+}
+
 /**
  * seq_buf_get_buf - get buffer to write arbitrary data to
  * @s: the seq_buf handle
@@ -179,6 +238,7 @@ extern int seq_buf_putmem(struct seq_buf *s, const void *mem, unsigned int len);
 extern int seq_buf_putmem_hex(struct seq_buf *s, const void *mem,
 			      unsigned int len);
 extern int seq_buf_path(struct seq_buf *s, const struct path *path, const char *esc);
+extern size_t seq_buf_puts_trunc(struct seq_buf *s, const char *str);
 extern int seq_buf_hex_dump(struct seq_buf *s, const char *prefix_str,
 			    int prefix_type, int rowsize, int groupsize,
 			    const void *buf, size_t len, bool ascii);
lib/seq_buf.c +35 -0
index a92093f346da..4d56ac71fafe 100644
@@ -376,6 +376,41 @@ int seq_buf_to_user(struct seq_buf *s, char __user *ubuf, size_t start, int cnt)
 	return cnt - ret;
 }
 
+/**
+ * seq_buf_puts_trunc - append as much of a string as fits, keeping any of it
+ * @s: the seq_buf handle
+ * @str: the string to append
+ *
+ * seq_buf_puts() writes nothing at all if @str doesn't fully fit,
+ * unlike strlcat()/strscpy(), which copy as much of the source as
+ * there is room for. That all-or-nothing behavior is usually what's
+ * wanted for building diagnostic/trace text, but it's the wrong
+ * choice when converting code that relied on strlcat()'s always-copy-
+ * what-fits truncation to avoid losing content that was already
+ * appended. This copies the leading bytes of @str that fit, reserving
+ * room for the NUL terminator later added by seq_buf_str().
+ *
+ * Unlike seq_buf_puts(), this does NOT NUL-terminate @s->buffer as it
+ * goes (it copies raw bytes via seq_buf_putmem(), not @str's own
+ * terminator). Callers MUST call seq_buf_str() or seq_buf_strlen()
+ * before using @s->buffer as a C string.
+ *
+ * Returns: the number of bytes copied from @str.
+ */
+size_t seq_buf_puts_trunc(struct seq_buf *s, const char *str)
+{
+	size_t left = seq_buf_buffer_left(s);
+	size_t len;
+
+	if (left <= 1)
+		return 0;
+
+	len = strnlen(str, left - 1);
+	seq_buf_putmem(s, str, len);
+
+	return len;
+}
+
 /**
  * seq_buf_hex_dump - print formatted hex dump into the sequence buffer
  * @s: seq_buf descriptor
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
maintainer
#2 lore raw
Quoted 25 lines · On Wed, 16 Sep 2026 22:21:25 +0000 Bill Wendling <[email protected]> wrote:

Converting some strlcat() call sites seq_buf need behavior seq_buf doesn't currently provide, either directly or without introducing subtle bugs:

- seq_buf_strlen(): seq_buf_used() reports the full buffer size when the buffer is completely filled, even though seq_buf_str() then overwrites the final byte with a NUL terminator, leaving only size - 1 bytes of actual content. Callers that need the true length of the NUL-terminated string have to fall back to strlen(seq_buf_str(s)). seq_buf_strlen() mirrors seq_buf_str()'s NUL-termination logic but returns the resulting string's length directly.

- seq_buf_init_append(): seq_buf_init() always clears the buffer it's given via seq_buf_clear(). Code migrating from strlcat(buf, ...), which appends to whatever @buf already contains, can't use seq_buf_init() without silently discarding that existing content. seq_buf_init_append() preserves it and positions the seq_buf to append after it.

- seq_buf_puts_trunc(): seq_buf_puts() (like seq_buf_printf() and friends) writes nothing at all if the string doesn't fully fit, whereas strlcat() always copies as much of the source as there is room for. Converting a strlcat() call site that relied on that partial-copy behavior to plain seq_buf_puts() can silently drop content that used to survive truncated. seq_buf_puts_trunc() keeps the leading bytes of the string that fit.

Quoted 5 lines

...

 include/linux/seq_buf.h | 60 +++++++++++++++++++++++++++++++++++++++++
 lib/seq_buf.c           | 35 ++++++++++++++++++++++++
 2 files changed, 95 insertions(+)

This seems a lot of code for enabling some strlcat removals. How many is "some"? If "3" then perhaps do something different at those callsites?

Sashiko had a couple of comments:

	https://sashiko.dev/#/patchset/[email protected]

Should these new functions be added to lib/tests/seq_buf_kunit.c?

#3 lore raw
Quoted 30 lines · Andrew Morton
Quoted 27 lines · On Wed, 16 Sep 2026 22:21:25 +0000 Bill Wendling <[email protected]> wrote:

Converting some strlcat() call sites seq_buf need behavior seq_buf doesn't currently provide, either directly or without introducing subtle bugs:

- seq_buf_strlen(): seq_buf_used() reports the full buffer size when the buffer is completely filled, even though seq_buf_str() then overwrites the final byte with a NUL terminator, leaving only size - 1 bytes of actual content. Callers that need the true length of the NUL-terminated string have to fall back to strlen(seq_buf_str(s)). seq_buf_strlen() mirrors seq_buf_str()'s NUL-termination logic but returns the resulting string's length directly.

- seq_buf_init_append(): seq_buf_init() always clears the buffer it's given via seq_buf_clear(). Code migrating from strlcat(buf, ...), which appends to whatever @buf already contains, can't use seq_buf_init() without silently discarding that existing content. seq_buf_init_append() preserves it and positions the seq_buf to append after it.

- seq_buf_puts_trunc(): seq_buf_puts() (like seq_buf_printf() and friends) writes nothing at all if the string doesn't fully fit, whereas strlcat() always copies as much of the source as there is room for. Converting a strlcat() call site that relied on that partial-copy behavior to plain seq_buf_puts() can silently drop content that used to survive truncated. seq_buf_puts_trunc() keeps the leading bytes of the string that fit.

...

Quoted 8 lines
Quoted 3 lines
 include/linux/seq_buf.h | 60 +++++++++++++++++++++++++++++++++++++++++
 lib/seq_buf.c           | 35 ++++++++++++++++++++++++
 2 files changed, 95 insertions(+)

This seems a lot of code for enabling some strlcat removals. How many is "some"? If "3" then perhaps do something different at those callsites?

In the series of patches I have in the wings (I sent one version, but it was a bit scattered and need to be resent), there are roughly 7 patches which use them, some more than once. While the impetus for these new functions are to help streamline the removal of the remaining strlcat, I believe Kees suggested these in order to keep the mathematics involved in keeping the seq_bufs correct was behind the API, rather than relying upon each user to get the math "correct". (Kees, please correct me if I'm wrong.)

Quoted 3 lines

Sashiko had a couple of comments:

        https://sashiko.dev/#/patchset/[email protected]

I'll look into them.

Quoted 1 line

Should these new functions be added to lib/tests/seq_buf_kunit.c?

Yes. I'll send a followup with the Sashiko comments addressed and the KUnit tests added.

Share and enjoy!
-bw

#4 lore raw

PATCH v3 1/1 seq_buf: add seq_buf_strlen(), seq_buf_init_append(), seq_buf_puts_trunc()

In reply to Bill Wendling

Converting some strlcat() call sites seq_buf need behavior seq_buf doesn't currently provide, either directly or without introducing subtle bugs:

- seq_buf_strlen(): seq_buf_used() reports the full buffer size when the buffer is completely filled, even though seq_buf_str() then overwrites the final byte with a NUL terminator, leaving only size - 1 bytes of actual content. Callers that need the true length of the NUL-terminated string have to fall back to strlen(seq_buf_str(s)). seq_buf_strlen() mirrors seq_buf_str()'s NUL-termination logic but returns the resulting string's length directly.

- seq_buf_init_append(): seq_buf_init() always clears the buffer it's given via seq_buf_clear(). Code migrating from strlcat(buf, ...), which appends to whatever @buf already contains, can't use seq_buf_init() without silently discarding that existing content. seq_buf_init_append() preserves it and positions the seq_buf to append after it.

- seq_buf_puts_trunc(): seq_buf_puts() (like seq_buf_printf() and friends) writes nothing at all if the string doesn't fully fit, whereas strlcat() always copies as much of the source as there is room for. Converting a strlcat() call site that relied on that partial-copy behavior to plain seq_buf_puts() can silently drop content that used to survive truncated. seq_buf_puts_trunc() keeps the leading bytes of the string that fit.

---
v2: Reword the commit message to be clearer and not refer to a series of
patches
v3: Add EXPORT_SYMBOL_GPL, make sure to overflow when truncating, correct
some logic discovered by the KUnit tests, and add KUnit tests.

---
 include/linux/seq_buf.h   | 60 +++++++++++++++++++++++++++++++++++++++
 lib/seq_buf.c             | 33 +++++++++++++++++++++
 lib/tests/seq_buf_kunit.c | 48 +++++++++++++++++++++++++++++++
 3 files changed, 141 insertions(+)
include/linux/seq_buf.h +60 -0
index 9f2839e73f8a..a552bcaab07f 100644
@@ -30,6 +30,10 @@ struct seq_buf {
 		.size = SIZE,				\
 	}
 
+/**
+ * seq_buf_clear - reset the seq_buf to be read / appended from the beginning
+ * @s: the seq_buf handle
+ */
 static inline void seq_buf_clear(struct seq_buf *s)
 {
 	s->len = 0;
@@ -37,6 +41,14 @@ static inline void seq_buf_clear(struct seq_buf *s)
 		s->buffer[0] = '\0';
 }
 
+/**
+ * seq_buf_init - initialize a seq_buf
+ * @s: the seq_buf handle
+ * @buf: pointer to the buffer
+ * @size: total size of @buf
+ *
+ * The contents of the buffer are ignored.
+ */
 static inline void
 seq_buf_init(struct seq_buf *s, char *buf, unsigned int size)
 {
@@ -45,6 +57,26 @@ seq_buf_init(struct seq_buf *s, char *buf, unsigned int size)
 	seq_buf_clear(s);
 }
 
+/**
+ * seq_buf_init_append - initialize a seq_buf over a buffer that may
+ *			 already hold NUL-terminated content
+ * @s: the seq_buf handle
+ * @buf: pointer to the (possibly non-empty) buffer
+ * @size: total size of @buf
+ *
+ * Unlike seq_buf_init(), which always clears @buf, this preserves
+ * whatever NUL-terminated content @buf already holds and positions
+ * @s to append after it. Useful for converting code that used to
+ * append to an existing buffer with strlcat()/scnprintf() and friends.
+ */
+static inline void
+seq_buf_init_append(struct seq_buf *s, char *buf, unsigned int size)
+{
+	s->buffer = buf;
+	s->size = size;
+	s->len = strnlen(buf, size);
+}
+
 /*
  * seq_buf have a buffer that might overflow. When this happens
  * len is set to be greater than size.
@@ -108,6 +140,33 @@ static inline const char *seq_buf_str(struct seq_buf *s)
 	return s->buffer;
 }
 
+/**
+ * seq_buf_strlen - get the length of the NUL-terminated string in seq_buf
+ * @s: the seq_buf handle
+ *
+ * Like seq_buf_str(), this makes sure that the buffer in @s is
+ * NUL-terminated, and returns the length of the resulting string.
+ * Unlike seq_buf_used(), the returned length is always correct, even
+ * when the buffer is completely full: in that case seq_buf_used()
+ * reports @s->size, but the last byte was overwritten with the
+ * trailing NUL, so only @s->size - 1 bytes of content remain.
+ *
+ * Returns: the length of the NUL-terminated string in @s->buffer.
+ */
+static inline size_t seq_buf_strlen(struct seq_buf *s)
+{
+	if (WARN_ON(s->size == 0))
+		return 0;
+
+	if (seq_buf_buffer_left(s)) {
+		s->buffer[s->len] = 0;
+		return s->len;
+	}
+
+	s->buffer[s->size - 1] = 0;
+	return s->size - 1;
+}
+
 /**
  * seq_buf_get_buf - get buffer to write arbitrary data to
  * @s: the seq_buf handle
@@ -179,6 +238,7 @@ extern int seq_buf_putmem(struct seq_buf *s, const void *mem, unsigned int len);
 extern int seq_buf_putmem_hex(struct seq_buf *s, const void *mem,
 			      unsigned int len);
 extern int seq_buf_path(struct seq_buf *s, const struct path *path, const char *esc);
+extern size_t seq_buf_puts_trunc(struct seq_buf *s, const char *str);
 extern int seq_buf_hex_dump(struct seq_buf *s, const char *prefix_str,
 			    int prefix_type, int rowsize, int groupsize,
 			    const void *buf, size_t len, bool ascii);
lib/seq_buf.c +33 -0
index a92093f346da..831a1542fb78 100644
@@ -199,6 +199,39 @@ int seq_buf_puts(struct seq_buf *s, const char *str)
 }
 EXPORT_SYMBOL_GPL(seq_buf_puts);
 
+/**
+ * seq_buf_puts_trunc - append as much of a string as fits, keeping any of it
+ * @s: the seq_buf handle
+ * @str: the string to append
+ *
+ * This copies the leading bytes of @str that fit, reserving room for the NUL
+ * terminator later added by seq_buf_str(). This differs from seq_buf_puts(),
+ * which writes nothing at all if @str doesn't fully fit.
+ *
+ * Unlike seq_buf_puts(), this does NOT NUL-terminate @s->buffer as it
+ * goes (it copies raw bytes via seq_buf_putmem(), not @str's own
+ * terminator). Callers MUST call seq_buf_str() or seq_buf_strlen()
+ * before using @s->buffer as a C string.
+ *
+ * Returns: the number of bytes copied from @str.
+ */
+size_t seq_buf_puts_trunc(struct seq_buf *s, const char *str)
+{
+	size_t len = strlen(str);
+
+	WARN_ON(s->size == 0);
+
+	if (seq_buf_can_fit(s, len))
+		return seq_buf_puts(s, str);
+
+	/* Truncate the string to fit the buffer. */
+	len = s->size - s->len;
+	memcpy(s->buffer + s->len, str, len);
+	seq_buf_set_overflow(s);
+	return len;
+}
+EXPORT_SYMBOL_GPL(seq_buf_puts_trunc);
+
 /**
  * seq_buf_putc - sequence printing of simple character
  * @s: seq_buf descriptor
lib/tests/seq_buf_kunit.c +48 -0

68 lines not on this page · raw message

Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
#5 lore raw
Quoted 3 lines · Bill Wendling

Converting some strlcat() call sites seq_buf need behavior seq_buf doesn't currently provide, either directly or without introducing subtle bugs:

Ah, our implementations have crossed paths in flight. :)

Quoted 1 line

- seq_buf_strlen(): seq_buf_used() reports the full buffer size when

We agree, we need this one. :)

Quoted 1 line

- seq_buf_init_append(): seq_buf_init() always clears the buffer it's

Oh, nice, yeah this is cool. I'd like a different name that mentions "str"? Maybe _from_str?

Quoted 1 line

- seq_buf_puts_trunc(): seq_buf_puts() (like seq_buf_printf() and

I prefer my solution that just truncates and sets overflow. Nothing i found needed the non-trunc behavior.

I'll send a v2 of the other series with seq_buf_init_from_str?

-Kees

Signature · 1 line
Kees Cook
#6 lore raw
Quoted 14 lines · Kees Cook
Quoted 3 lines · Bill Wendling

Converting some strlcat() call sites seq_buf need behavior seq_buf doesn't currently provide, either directly or without introducing subtle bugs:

Ah, our implementations have crossed paths in flight. :)

Quoted 1 line

- seq_buf_strlen(): seq_buf_used() reports the full buffer size when

We agree, we need this one. :)

Quoted 1 line

- seq_buf_init_append(): seq_buf_init() always clears the buffer it's

Quoted 8 lines

Oh, nice, yeah this is cool. I'd like a different name that mentions "str"? Maybe _from_str?

Quoted 1 line

- seq_buf_puts_trunc(): seq_buf_puts() (like seq_buf_printf() and

I prefer my solution that just truncates and sets overflow. Nothing i found needed the non-trunc behavior.

I'll send a v2 of the other series with seq_buf_init_from_str?

Hi Kees,

I didn't know you were working on this as well.

Andrew, please ignore this patch. I apologize for the spam.

-bw

Quoted 4 lines

-Kees

--
Kees Cook