From: Sergey Senozhatsky <sergey.senozhatsky.work@gmail.com>
To: Andrew Morton <akpm@linux-foundation.org>
Cc: Arnd Bergmann <arnd@arndb.de>, Martin Wilck <mwilck@suse.com>,
Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
Andy Shevchenko <andriy.shevchenko@linux.intel.com>,
linux-kernel@vger.kernel.org,
Sergey Senozhatsky <sergey.senozhatsky@gmail.com>
Subject: [RFC][PATCH] lib/string: introduce sysfs_strncpy() and sysfs_strlcpy()
Date: Tue, 21 Aug 2018 15:24:59 +0900 [thread overview]
Message-ID: <20180821062459.1807-1-sergey.senozhatsky@gmail.com> (raw)
In sysfs ->store() callbacks we usually need to remember that a
supplied sysfs input string might or might not contain whitespaces
and a trailing new-line symbol, which we need to take care of.
Examples:
echo "newline" > /sys/.../attr
echo -n "no_newine" > /sys/.../attr
That's why a typical sysfs ->store() should do something like
below when it copies a string:
ssize_t FOO_store(struct device *dev, ....)
{
....
strlcpy(value, buf, MAX_SZ);
sz = strlen(value);
if (sz > 0 && value[sz - 1] == '\n')
value[sz - 1] = 0x00;
...
}
or use a sysfs-string friendly strcmp() function when comparing
a given string to a pre-defined one:
ssize_t FOO_store(struct device *dev, ....)
{
...
if (sysfs_streq(buf, "normal"))
mode = m_normal;
...
}
Per Andrew Morton:
: There's a LOT of code which does basically-the-same-thing with sysfs
: input. And a lot of it misses things, such as leading whitespace.
: Passing all this through helpers would provide consistency as well
: as code-size reductions, improved reviewability, etc.
This patch introduces two such helpers - sysfs_strncpy() and
sysfs_strlcpy(), which, basically, do what strncpy() and strlcpy() do,
but additionally they remove leading and trailing white-spaces and
tailing new-line symbols. So a FOO_store() example which was posted
above may be rewritten to:
ssize_t FOO_store(struct device *dev, ....)
{
....
sysfs_strlcpy(value, buf, MAX_SZ);
...
}
Signed-off-by: Sergey Senozhatsky <sergey.senozhatsky@gmail.com>
Suggested-by: Andrew Morton <akpm@linux-foundation.org>
---
include/linux/string.h | 3 +++
lib/string.c | 51 ++++++++++++++++++++++++++++++++++++++++++
2 files changed, 54 insertions(+)
diff --git a/include/linux/string.h b/include/linux/string.h
index 4a5a0eb7df51..62b08bbc3ada 100644
--- a/include/linux/string.h
+++ b/include/linux/string.h
@@ -175,6 +175,9 @@ extern char **argv_split(gfp_t gfp, const char *str, int *argcp);
extern void argv_free(char **argv);
extern bool sysfs_streq(const char *s1, const char *s2);
+extern char *sysfs_strncpy(char *dest, const char *src, size_t count);
+extern size_t sysfs_strlcpy(char *dest, const char *src, size_t size);
+
extern int kstrtobool(const char *s, bool *res);
static inline int strtobool(const char *s, bool *res)
{
diff --git a/lib/string.c b/lib/string.c
index 2c0900a5d51a..e81b1be00796 100644
--- a/lib/string.c
+++ b/lib/string.c
@@ -631,6 +631,57 @@ bool sysfs_streq(const char *s1, const char *s2)
}
EXPORT_SYMBOL(sysfs_streq);
+/**
+ * sysfs_strncpy - Trim a length-limited C-string (wgutesoaces and a trailing
+ * newline symbol) and copy into a buffer
+ * @dest: Where to copy the string to
+ * @src: Where to copy the string from
+ * @count: The maximum number of bytes to copy
+ *
+ * A wrapper around strncpy().
+ *
+ */
+char *sysfs_strncpy(char *dest, const char *src, size_t count)
+{
+ char *c;
+
+ strncpy(dest, skip_spaces(src), count);
+
+ c = dest + count - 1;
+ while (c >= dest && (isspace(*c) || *c == '\n' || *c == '\0')) {
+ *c = '\0';
+ c--;
+ }
+ return dest;
+}
+EXPORT_SYMBOL(sysfs_strncpy);
+
+/**
+ * sysfs_strlcpy - Trim a C-string (whitespaces and a trailing newline symbol)
+ * and copy it into a sized buffer
+ * @dest: Where to copy the string to
+ * @src: Where to copy the string from
+ * @size: size of destination buffer
+ *
+ * A wrapper around strlcpy().
+ *
+ */
+size_t sysfs_strlcpy(char *dest, const char *src, size_t size)
+{
+ size_t ret;
+ char *c;
+
+ ret = strlcpy(dest, skip_spaces(src), size);
+
+ size = strlen(dest);
+ c = dest + size - 1;
+ while (c >= dest && (isspace(*c) || *c == '\n'))
+ c--;
+ *(c + 1) = '\0';
+ return ret;
+}
+EXPORT_SYMBOL(sysfs_strlcpy);
+
/**
* match_string - matches given string in an array
* @array: array of strings
--
2.18.0
next reply other threads:[~2018-08-21 6:25 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-08-21 6:24 Sergey Senozhatsky [this message]
2018-08-21 7:59 ` [RFC][PATCH] lib/string: introduce sysfs_strncpy() and sysfs_strlcpy() Rasmus Villemoes
2018-08-21 9:50 ` Sergey Senozhatsky
2018-08-21 9:54 ` Sergey Senozhatsky
2018-08-21 11:44 ` Sergey Senozhatsky
2018-08-21 12:00 ` Andy Shevchenko
2018-08-22 0:32 ` Sergey Senozhatsky
2018-08-21 12:43 ` Rasmus Villemoes
2018-08-22 5:13 ` Sergey Senozhatsky
2018-08-21 13:57 ` Greg Kroah-Hartman
2018-08-22 4:58 ` Sergey Senozhatsky
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=20180821062459.1807-1-sergey.senozhatsky@gmail.com \
--to=sergey.senozhatsky.work@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=andriy.shevchenko@linux.intel.com \
--cc=arnd@arndb.de \
--cc=gregkh@linuxfoundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mwilck@suse.com \
--cc=sergey.senozhatsky@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).