Skip to contentSkip to conversation

lib: string: add functions to case-convert strings

PATCH Applied Markus Mayer 7 participants 24 messages Jul 8, 2016 – Jul 20, 2016

Subsystems (10)
DRM drivers SCSI DRM misc Staging +6 more subsystems

v3 of 5 Applied Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v3 cover lib: string: add functions to case-convert strings

This series introduces a family of generic string case conversion functions. This kind of functionality is needed in several places in the kernel. Right now, everybody seems to be implementing their own copy of this functionality.

Based on the discussion of the previous version of this series[1] and the use cases found in the kernel, it does look like having several flavours of case conversion functions is beneficial. The use cases fall into three categories:
- copying a string and converting the case while specifying a maximum length to mimic strlcpy()
- copying a string and converting the case without specifying a length to mimic strcpy()
- converting the case of a string in-place (i.e. modifying the string that was passed in)

Consequently, I am proposing these new functions:

    void strlcpytoupper(char *dst, const char *src, size_t len);
    void strlcpytolower(char *dst, const char *src, size_t len);
    void strcpytoupper(char *dst, const char *src);
    void strcpytolower(char *dst, const char *src);
    void strtoupper(char *s);
    void strtolower(char *s);

Several drivers are being modified to make use of the functions above. Another driver that also makes use of this functionality will be submitted upstream shortly, which prompted this whole exercise.

The changes made here have been compile-tested, but not tried out, due to lack of required hardware.

Changes since v2:
- use strlcpy() semantics not strncpy() semantics, i.e. guarantee NULL termination
- as a result strncpyto<upper|lower> are now called strlcpyto<upper|lower>
- make functions void
- use len == -1 (SIZE_MAX) as no-limit indicator rather then len == 0
- change PATCH 2/7 to match strlcpy() semantics
- change PATCH 4/7 to match strlcpy() semantics

Changes since v1:
- expanded strtolower() into a family of functions that cover use cases when a length argument is or isn't required and that support copying the string into a new buffer or changing it in-place
- changed the function semantics to return a pointer to the terminating '\0' character of the modified string
- added strtoupper() functionality mirroring the above
- dropped the ACPICA patch, since that code is OS independent and can't rely on a Linux library function (see [2])
- Added two new patches replacing strtoupper() implementations

[1] https://lkml.org/lkml/2016/6/30/727
[2] https://lkml.org/lkml/2016/7/1/9

Markus Mayer (7):
lib: string: add functions to case-convert strings drm/nouveau/core: make use of new strlcpytolower() function ACPI / device_sysfs: make use of new strtolower() function
staging: speakup: replace spk_strlwr() with strlcpytolower()
iscsi-target: replace iscsi_initiatorname_tolower() with strtolower() drm/nouveau/fifo/gk104: make use of new strcpytoupper() function
power_supply: make use of new strcpytoupper() function

 drivers/acpi/device_sysfs.c                      |  4 +--
 drivers/gpu/drm/nouveau/nvkm/core/firmware.c     |  9 +-----
 drivers/gpu/drm/nouveau/nvkm/engine/fifo/gk104.c |  5 +--
 drivers/power/power_supply_sysfs.c               | 13 +++-----
 drivers/staging/speakup/kobjects.c               |  3 +-
 drivers/staging/speakup/main.c                   |  3 +-
 drivers/staging/speakup/speakup.h                |  1 -
 drivers/staging/speakup/varhandlers.c            | 12 -------
 drivers/target/iscsi/iscsi_target_nego.c         | 17 +---------
 include/linux/string.h                           | 40 ++++++++++++++++++++++++
 lib/string.c                                     | 38 ++++++++++++++++++++++
 11 files changed, 90 insertions(+), 55 deletions(-)
Signature · 6 lines
2.7.4

_______________________________________________
Nouveau mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/nouveau
#1 lore raw

PATCH v3 1/7 lib: string: add functions to case-convert strings

Add a collection of generic functions to convert strings to lowercase or uppercase.

Changing the case of a string (with or without copying it first) seems to be a recurring requirement in the kernel that is currently being solved by several duplicated implementations doing the same thing. This change aims at reducing this code duplication.

The new functions are

    void strlcpytoupper(char *dst, const char *src, size_t len);
    void strlcpytolower(char *dst, const char *src, size_t len);
    void strcpytoupper(char *dst, const char *src);
    void strcpytolower(char *dst, const char *src);
    void strtoupper(char *s);
    void strtolower(char *s);

The "str[l]cpyto*" versions of the function take a destination string and a source string as arguments. The "strlcpyto*" versions additionally take a length argument like strlcpy() itself. Lastly, the strto* functions take a single string argument and modify the passed-in string.

Like strlcpy(), and unlike strncpy(), the functions guarantee NULL termination of the destination string.

---
 include/linux/string.h | 40 ++++++++++++++++++++++++++++++++++++++++
 lib/string.c           | 38 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 78 insertions(+)
include/linux/string.h +40 -0
index 26b6f6a..36c9d14 100644
@@ -116,6 +116,8 @@ extern void * memchr(const void *,int,__kernel_size_t);
 #endif
 void *memchr_inv(const void *s, int c, size_t n);
 char *strreplace(char *s, char old, char new);
+extern void strlcpytoupper(char *dst, const char *src, size_t len);
+extern void strlcpytolower(char *dst, const char *src, size_t len);
 
 extern void kfree_const(const void *x);
 
@@ -169,4 +171,42 @@ static inline const char *kbasename(const char *path)
 	return tail ? tail + 1 : path;
 }
 
+/**
+ * strcpytoupper - Copy string and convert to uppercase.
+ * @dst: The buffer to store the result.
+ * @src: The string to convert to uppercase.
+ */
+static inline void strcpytoupper(char *dst, const char *src)
+{
+	strlcpytoupper(dst, src, -1);
+}
+
+/**
+ * strcpytolower - Copy string and convert to lowercase.
+ * @dst: The buffer to store the result.
+ * @src: The string to convert to lowercase.
+ */
+static inline void strcpytolower(char *dst, const char *src)
+{
+	strlcpytolower(dst, src, -1);
+}
+
+/**
+ * strtoupper - Convert string to uppercase.
+ * @s: The string to operate on.
+ */
+static inline void strtoupper(char *s)
+{
+	strlcpytoupper(s, s, -1);
+}
+
+/**
+ * strtolower - Convert string to lowercase.
+ * @s: The string to operate on.
+ */
+static inline void strtolower(char *s)
+{
+	strlcpytolower(s, s, -1);
+}
+
 #endif /* _LINUX_STRING_H_ */
lib/string.c +38 -0
index ed83562..fd8c427 100644
@@ -952,3 +952,41 @@ char *strreplace(char *s, char old, char new)
 	return s;
 }
 EXPORT_SYMBOL(strreplace);
+
+/**
+ * strlcpytoupper - Copy a length-limited string and convert to uppercase.
+ * @dst: The buffer to store the result.
+ * @src: The string to convert to uppercase.
+ * @len: Maximum string length. May be SIZE_MAX (-1) to set no limit.
+ */
+void strlcpytoupper(char *dst, const char *src, size_t len)
+{
+	size_t i;
+
+	if (!len)
+		return;
+
+	for (i = 0; i < len && src[i]; ++i)
+		dst[i] = toupper(src[i]);
+	dst[i < len ? i : i - 1] = '\0';
+}
+EXPORT_SYMBOL(strlcpytoupper);
+
+/**
+ * strlcpytolower - Copy a length-limited string and convert to lowercase.
+ * @dst: The buffer to store the result.
+ * @src: The string to convert to lowercase.
+ * @len: Maximum string length. May be SIZE_MAX (-1) to set no limit.
+ */
+void strlcpytolower(char *dst, const char *src, size_t len)
+{
+	size_t i;
+
+	if (!len)
+		return;
+
+	for (i = 0; i < len && src[i]; ++i)
+		dst[i] = tolower(src[i]);
+	dst[i < len ? i : i - 1] = '\0';
+}
+EXPORT_SYMBOL(strlcpytolower);
Signature · 6 lines
2.7.4

_______________________________________________
Nouveau mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/nouveau
#9 lore raw

PATCH v3 2/7 drm/nouveau/core: make use of new strlcpytolower() function

In reply to Markus Mayer

Call strlcpytolower() rather than copying the string explicitly and then walking it to convert it to lowercase.

---

The semantics of the new function has changed, so this patch has been updated since v2 to match the new strlcpy() semantics (no explicit NULL terminating is required).

See https://patchwork.kernel.org/patch/9215207/ for the previous version.

 drivers/gpu/drm/nouveau/nvkm/core/firmware.c | 9 +--------
 1 file changed, 1 insertion(+), 8 deletions(-)
drivers/gpu/drm/nouveau/nvkm/core/firmware.c +1 -8
index 34ecd4a..982601e 100644
@@ -36,16 +36,9 @@ nvkm_firmware_get(struct nvkm_device *device, const char *fwname,
 {
 	char f[64];
 	char cname[16];
-	int i;
 
 	/* Convert device name to lowercase */
-	strncpy(cname, device->chip->name, sizeof(cname));
-	cname[sizeof(cname) - 1] = '\0';
-	i = strlen(cname);
-	while (i) {
-		--i;
-		cname[i] = tolower(cname[i]);
-	}
+	strlcpytolower(cname, device->chip->name, sizeof(cname));
 
 	snprintf(f, sizeof(f), "nvidia/%s/%s.bin", cname, fwname);
 	return request_firmware(fw, f, device->dev);
Signature · 6 lines
2.7.4

_______________________________________________
Nouveau mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/nouveau
#14 lore raw

PATCH v3 5/7 iscsi-target: replace iscsi_initiatorname_tolower() with strtolower()

In reply to Markus Mayer

After introducing generic strtolower(), iscsi_initiatorname_tolower() is no longer needed.

---
 drivers/target/iscsi/iscsi_target_nego.c | 17 +----------------
 1 file changed, 1 insertion(+), 16 deletions(-)
drivers/target/iscsi/iscsi_target_nego.c +1 -16
index 89d34bd..fa20638 100644
@@ -987,21 +987,6 @@ static int iscsi_target_do_login(struct iscsi_conn *conn, struct iscsi_login *lo
 	return 0;
 }
 
-static void iscsi_initiatorname_tolower(
-	char *param_buf)
-{
-	char *c;
-	u32 iqn_size = strlen(param_buf), i;
-
-	for (i = 0; i < iqn_size; i++) {
-		c = &param_buf[i];
-		if (!isupper(*c))
-			continue;
-
-		*c = tolower(*c);
-	}
-}
-
 /*
  * Processes the first Login Request..
  */
@@ -1075,7 +1060,7 @@ int iscsi_target_locate_portal(
 	 * RFC-3720 3.2.6.1. section c) that says that iSCSI IQNs
 	 * are NOT case sensitive.
 	 */
-	iscsi_initiatorname_tolower(i_buf);
+	strtolower(i_buf);
 
 	if (!s_buf) {
 		if (!login->leading_connection)
Signature · 1 line
2.7.4
#18 lore raw

PATCH v3 6/7 drm/nouveau/fifo/gk104: make use of new strcpytoupper() function

In reply to Markus Mayer

Call strcpytoupper() rather than copying the string explicitly and then walking it to convert it to uppercase.

---
 drivers/gpu/drm/nouveau/nvkm/engine/fifo/gk104.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
drivers/gpu/drm/nouveau/nvkm/engine/fifo/gk104.c +1 -4
index 743f3a1..8d01032 100644
@@ -332,10 +332,7 @@ gk104_fifo_intr_fault(struct gk104_fifo *fifo, int unit)
 		enum nvkm_devidx engidx = nvkm_top_fault(device->top, unit);
 		if (engidx < NVKM_SUBDEV_NR) {
 			const char *src = nvkm_subdev_name[engidx];
-			char *dst = en;
-			do {
-				*dst++ = toupper(*src++);
-			} while(*src);
+			strcpytoupper(en, src);
 			engine = nvkm_device_engine(device, engidx);
 		}
 	} else {
Signature · 6 lines
2.7.4

_______________________________________________
Nouveau mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/nouveau
#10 lore raw

PATCH v3 3/7 ACPI / device_sysfs: make use of new strtolower() function

In reply to Markus Mayer

Call strtolower() rather than walking the string explicitly to convert it to lowercase.

---

Rafael, I left off your ACK, since the implementation of my function changed somewhat (doesn't check the argument NULL, doesn't return anything). Please let me know if you are still okay with this.

 drivers/acpi/device_sysfs.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/acpi/device_sysfs.c +1 -3
index 7b2c48f..1db38c7 100644
@@ -200,12 +200,10 @@ static int create_of_modalias(struct acpi_device *acpi_dev, char *modalias,
 	const union acpi_object *of_compatible, *obj;
 	int len, count;
 	int i, nval;
-	char *c;
 
 	acpi_get_name(acpi_dev->handle, ACPI_SINGLE_NAME, &buf);
 	/* DT strings are all in lower case */
-	for (c = buf.pointer; *c != '\0'; c++)
-		*c = tolower(*c);
+	strtolower(buf.pointer);
 
 	len = snprintf(modalias, size, "of:N%sT", (char *)buf.pointer);
 	ACPI_FREE(buf.pointer);
Signature · 1 line
2.7.4
#19 lore raw

PATCH v3 7/7 power_supply: make use of new strcpytoupper() function

In reply to Markus Mayer

Call strcpytoupper() rather than walking the string explicitly to convert it to uppercase.

---
 drivers/power/power_supply_sysfs.c | 13 +++++--------
 1 file changed, 5 insertions(+), 8 deletions(-)
drivers/power/power_supply_sysfs.c +5 -8
index 80fed98..20fdcc5 100644
@@ -256,19 +256,16 @@ void power_supply_init_attrs(struct device_type *dev_type)
 
 static char *kstruprdup(const char *str, gfp_t gfp)
 {
-	char *ret, *ustr;
+	char *ustr;
 
-	ustr = ret = kmalloc(strlen(str) + 1, gfp);
+	ustr = kmalloc(strlen(str) + 1, gfp);
 
-	if (!ret)
+	if (!ustr)
 		return NULL;
 
-	while (*str)
-		*ustr++ = toupper(*str++);
+	strcpytoupper(ustr, str);
 
-	*ustr = 0;
-
-	return ret;
+	return ustr;
 }
 
 int power_supply_uevent(struct device *dev, struct kobj_uevent_env *env)
Signature · 1 line
2.7.4
#12 lore raw

PATCH v3 4/7 staging: speakup: replace spk_strlwr() with strlcpytolower()

In reply to Markus Mayer

After introducing generic strltolower() and strtolower(), spk_strlwr() is no longer needed.

---

Samuel, I left off your ACK, since the implementation of my function changed in v2 and again in v3. This patch has been updated to match the new strlcpy() semantics. Here, this means we want the length argument to be sizeof(new_synth_name).

See https://patchwork.kernel.org/patch/9215217/ for the previous version.

 drivers/staging/speakup/kobjects.c    |  3 +--
 drivers/staging/speakup/main.c        |  3 ++-
 drivers/staging/speakup/speakup.h     |  1 -
 drivers/staging/speakup/varhandlers.c | 12 ------------
 4 files changed, 3 insertions(+), 16 deletions(-)
drivers/staging/speakup/kobjects.c +1 -2
index 528cbdc..c6e0c2d 100644
@@ -387,11 +387,10 @@ static ssize_t synth_store(struct kobject *kobj, struct kobj_attribute *attr,
 	len = strlen(buf);
 	if (len < 2 || len > 9)
 		return -EINVAL;
-	strncpy(new_synth_name, buf, len);
+	strlcpytolower(new_synth_name, buf, sizeof(new_synth_name));
 	if (new_synth_name[len - 1] == '\n')
 		len--;
 	new_synth_name[len] = '\0';
-	spk_strlwr(new_synth_name);
 	if ((synth != NULL) && (!strcmp(new_synth_name, synth->name))) {
 		pr_warn("%s already in use\n", new_synth_name);
 	} else if (synth_init(new_synth_name) != 0) {
drivers/staging/speakup/main.c +2 -1
index 97ca4ec..970f568 100644
@@ -2314,7 +2314,8 @@ static int __init speakup_init(void)
 	spk_initialize_msgs();	/* Initialize arrays for i18n. */
 	spk_reset_default_chars();
 	spk_reset_default_chartab();
-	spk_strlwr(synth_name);
+	if (synth_name)
+		strtolower(synth_name);
 	spk_vars[0].u.n.high = vc->vc_cols;
 	for (var = spk_vars; var->var_id != MAXVARS; var++)
 		speakup_register_var(var);
drivers/staging/speakup/speakup.h +0 -1
index df74c91..4725785 100644
@@ -50,7 +50,6 @@ void synth_insert_next_index(int sent_num);
 void spk_reset_index_count(int sc);
 void spk_get_index_count(int *linecount, int *sentcount);
 int spk_set_key_info(const u_char *key_info, u_char *k_buffer);
-char *spk_strlwr(char *s);
 char *spk_s2uchar(char *start, char *dest);
 int speakup_kobj_init(void);
 void speakup_kobj_exit(void);
drivers/staging/speakup/varhandlers.c +0 -12
index e1393d2..a1af222 100644
@@ -309,18 +309,6 @@ int spk_set_mask_bits(const char *input, const int which, const int how)
 	return 0;
 }
 
-char *spk_strlwr(char *s)
-{
-	char *p;
-
-	if (!s)
-		return NULL;
-
-	for (p = s; *p; p++)
-		*p = tolower(*p);
-	return s;
-}
-
 char *spk_s2uchar(char *start, char *dest)
 {
 	int val;
Signature · 1 line
2.7.4
#11 lore raw

Re: [PATCH v3 3/7] ACPI / device_sysfs: make use of new strtolower() function

In reply to Markus Mayer
Quoted 9 lines · Markus Mayer

Call strtolower() rather than walking the string explicitly to convert it to lowercase.

---

Rafael, I left off your ACK, since the implementation of my function changed somewhat (doesn't check the argument NULL, doesn't return anything). Please let me know if you are still okay with this.

I am, as long as it doesn't change the result, which seems to be the case.

Quoted 12 lines
 drivers/acpi/device_sysfs.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/acpi/device_sysfs.c +0 -0
index 7b2c48f..1db38c7 100644
@@ -200,12 +200,10 @@ static int create_of_modalias(struct acpi_device *acpi_dev, char *modalias,
 	const union acpi_object *of_compatible, *obj;
 	int len, count;
 	int i, nval;

drivers/acpi/device_sysfs.c static int create_of_modalias(struct acpi_device *acpi_dev, char *modalias, · line 209

Quoted 10 lines
drivers/acpi/device_sysfs.c +1 -3
-	char *c;
 
 	acpi_get_name(acpi_dev->handle, ACPI_SINGLE_NAME, &buf);
 	/* DT strings are all in lower case */
-	for (c = buf.pointer; *c != '\0'; c++)
-		*c = tolower(*c);
+	strtolower(buf.pointer);
 
 	len = snprintf(modalias, size, "of:N%sT", (char *)buf.pointer);
 	ACPI_FREE(buf.pointer);

Thanks,
Rafael

#2 lore raw
In reply to Markus Mayer
Quoted 47 lines · Markus Mayer

Add a collection of generic functions to convert strings to lowercase or uppercase.

Changing the case of a string (with or without copying it first) seems to be a recurring requirement in the kernel that is currently being solved by several duplicated implementations doing the same thing. This change aims at reducing this code duplication.

The new functions are

    void strlcpytoupper(char *dst, const char *src, size_t len);
    void strlcpytolower(char *dst, const char *src, size_t len);
    void strcpytoupper(char *dst, const char *src);
    void strcpytolower(char *dst, const char *src);
    void strtoupper(char *s);
    void strtolower(char *s);

The "str[l]cpyto*" versions of the function take a destination string and a source string as arguments. The "strlcpyto*" versions additionally take a length argument like strlcpy() itself. Lastly, the strto* functions take a single string argument and modify the passed-in string.

Like strlcpy(), and unlike strncpy(), the functions guarantee NULL termination of the destination string.

---
 include/linux/string.h | 40 ++++++++++++++++++++++++++++++++++++++++
 lib/string.c           | 38 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 78 insertions(+)
include/linux/string.h +2 -0
index 26b6f6a..36c9d14 100644
@@ -116,6 +116,8 @@ extern void * memchr(const void *,int,__kernel_size_t);
 #endif
 void *memchr_inv(const void *s, int c, size_t n);
 char *strreplace(char *s, char old, char new);
+extern void strlcpytoupper(char *dst, const char *src, size_t len);
+extern void strlcpytolower(char *dst, const char *src, size_t len);
 
 extern void kfree_const(const void *x);
 
@@ -169,4 +171,42 @@ static inline const char *kbasename(const char *path)
 	return tail ? tail + 1 : path;
 }
 

include/linux/string.h static inline const char *kbasename(const char *path) · line 183

Quoted 10 lines
include/linux/string.h +10 -0
+/**
+ * strcpytoupper - Copy string and convert to uppercase.
+ * @dst: The buffer to store the result.
+ * @src: The string to convert to uppercase.
+ */
+static inline void strcpytoupper(char *dst, const char *src)
+{
+	strlcpytoupper(dst, src, -1);
+}
+

Why not use SIZE_MAX instead of -1?

include/linux/string.h static inline const char *kbasename(const char *path) · line 193

Quoted 10 lines
include/linux/string.h +10 -0
+/**
+ * strcpytolower - Copy string and convert to lowercase.
+ * @dst: The buffer to store the result.
+ * @src: The string to convert to lowercase.
+ */
+static inline void strcpytolower(char *dst, const char *src)
+{
+	strlcpytolower(dst, src, -1);
+}
+

Same here, and the 2 below :)

Thanks Markus,
Luis

Quoted 66 lines
include/linux/string.h +18 -0
+/**
+ * strtoupper - Convert string to uppercase.
+ * @s: The string to operate on.
+ */
+static inline void strtoupper(char *s)
+{
+	strlcpytoupper(s, s, -1);
+}
+
+/**
+ * strtolower - Convert string to lowercase.
+ * @s: The string to operate on.
+ */
+static inline void strtolower(char *s)
+{
+	strlcpytolower(s, s, -1);
+}
+
 #endif /* _LINUX_STRING_H_ */
lib/string.c +38 -0
index ed83562..fd8c427 100644
@@ -952,3 +952,41 @@ char *strreplace(char *s, char old, char new)
 	return s;
 }
 EXPORT_SYMBOL(strreplace);
+
+/**
+ * strlcpytoupper - Copy a length-limited string and convert to uppercase.
+ * @dst: The buffer to store the result.
+ * @src: The string to convert to uppercase.
+ * @len: Maximum string length. May be SIZE_MAX (-1) to set no limit.
+ */
+void strlcpytoupper(char *dst, const char *src, size_t len)
+{
+	size_t i;
+
+	if (!len)
+		return;
+
+	for (i = 0; i < len && src[i]; ++i)
+		dst[i] = toupper(src[i]);
+	dst[i < len ? i : i - 1] = '\0';
+}
+EXPORT_SYMBOL(strlcpytoupper);
+
+/**
+ * strlcpytolower - Copy a length-limited string and convert to lowercase.
+ * @dst: The buffer to store the result.
+ * @src: The string to convert to lowercase.
+ * @len: Maximum string length. May be SIZE_MAX (-1) to set no limit.
+ */
+void strlcpytolower(char *dst, const char *src, size_t len)
+{
+	size_t i;
+
+	if (!len)
+		return;
+
+	for (i = 0; i < len && src[i]; ++i)
+		dst[i] = tolower(src[i]);
+	dst[i < len ? i : i - 1] = '\0';
+}
+EXPORT_SYMBOL(strlcpytolower);
#3 lore raw
Quoted 48 lines · Luis de Bethencourt
Quoted 47 lines · Markus Mayer

Add a collection of generic functions to convert strings to lowercase or uppercase.

Changing the case of a string (with or without copying it first) seems to be a recurring requirement in the kernel that is currently being solved by several duplicated implementations doing the same thing. This change aims at reducing this code duplication.

The new functions are

    void strlcpytoupper(char *dst, const char *src, size_t len);
    void strlcpytolower(char *dst, const char *src, size_t len);
    void strcpytoupper(char *dst, const char *src);
    void strcpytolower(char *dst, const char *src);
    void strtoupper(char *s);
    void strtolower(char *s);

The "str[l]cpyto*" versions of the function take a destination string and a source string as arguments. The "strlcpyto*" versions additionally take a length argument like strlcpy() itself. Lastly, the strto* functions take a single string argument and modify the passed-in string.

Like strlcpy(), and unlike strncpy(), the functions guarantee NULL termination of the destination string.

---
 include/linux/string.h | 40 ++++++++++++++++++++++++++++++++++++++++
 lib/string.c           | 38 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 78 insertions(+)
include/linux/string.h +2 -0
index 26b6f6a..36c9d14 100644
@@ -116,6 +116,8 @@ extern void * memchr(const void *,int,__kernel_size_t);
 #endif
 void *memchr_inv(const void *s, int c, size_t n);
 char *strreplace(char *s, char old, char new);
+extern void strlcpytoupper(char *dst, const char *src, size_t len);
+extern void strlcpytolower(char *dst, const char *src, size_t len);
 
 extern void kfree_const(const void *x);
 
@@ -169,4 +171,42 @@ static inline const char *kbasename(const char *path)
      return tail ? tail + 1 : path;
 }
 
Quoted 12 lines

include/linux/string.h static inline const char *kbasename(const char *path) · line 183

Quoted 10 lines
include/linux/string.h +10 -0
+/**
+ * strcpytoupper - Copy string and convert to uppercase.
+ * @dst: The buffer to store the result.
+ * @src: The string to convert to uppercase.
+ */
+static inline void strcpytoupper(char *dst, const char *src)
+{
+     strlcpytoupper(dst, src, -1);
+}
+

Why not use SIZE_MAX instead of -1?

Sure. I'll change all four of them. Thanks.

Quoted 74 lines

include/linux/string.h static inline const char *kbasename(const char *path) · line 193

Quoted 10 lines
include/linux/string.h +10 -0
+/**
+ * strcpytolower - Copy string and convert to lowercase.
+ * @dst: The buffer to store the result.
+ * @src: The string to convert to lowercase.
+ */
+static inline void strcpytolower(char *dst, const char *src)
+{
+     strlcpytolower(dst, src, -1);
+}
+

Same here, and the 2 below :)

Thanks Markus,
Luis

Quoted 58 lines
include/linux/string.h +18 -0
+/**
+ * strtoupper - Convert string to uppercase.
+ * @s: The string to operate on.
+ */
+static inline void strtoupper(char *s)
+{
+     strlcpytoupper(s, s, -1);
+}
+
+/**
+ * strtolower - Convert string to lowercase.
+ * @s: The string to operate on.
+ */
+static inline void strtolower(char *s)
+{
+     strlcpytolower(s, s, -1);
+}
+
 #endif /* _LINUX_STRING_H_ */
lib/string.c +31 -0
index ed83562..fd8c427 100644
@@ -952,3 +952,41 @@ char *strreplace(char *s, char old, char new)
      return s;
 }
 EXPORT_SYMBOL(strreplace);
+
+/**
+ * strlcpytoupper - Copy a length-limited string and convert to uppercase.
+ * @dst: The buffer to store the result.
+ * @src: The string to convert to uppercase.
+ * @len: Maximum string length. May be SIZE_MAX (-1) to set no limit.
+ */
+void strlcpytoupper(char *dst, const char *src, size_t len)
+{
+     size_t i;
+
+     if (!len)
+             return;
+
+     for (i = 0; i < len && src[i]; ++i)
+             dst[i] = toupper(src[i]);
+     dst[i < len ? i : i - 1] = '\0';
+}
+EXPORT_SYMBOL(strlcpytoupper);
+
+/**
+ * strlcpytolower - Copy a length-limited string and convert to lowercase.
+ * @dst: The buffer to store the result.
+ * @src: The string to convert to lowercase.
+ * @len: Maximum string length. May be SIZE_MAX (-1) to set no limit.
+ */
+void strlcpytolower(char *dst, const char *src, size_t len)
+{
+     size_t i;
+
+     if (!len)
Quoted 9 lines

lib/string.c char *strreplace(char *s, char old, char new) · line 992

Quoted 8 lines
lib/string.c +7 -0
+             return;
+
+     for (i = 0; i < len && src[i]; ++i)
+             dst[i] = tolower(src[i]);
+     dst[i < len ? i : i - 1] = '\0';
+}
+EXPORT_SYMBOL(strlcpytolower);

_______________________________________________
Nouveau mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/nouveau

#20 lore raw
In reply to Markus Mayer
Quoted 16 lines · Markus Mayer

This series introduces a family of generic string case conversion functions. This kind of functionality is needed in several places in the kernel. Right now, everybody seems to be implementing their own copy of this functionality.

Based on the discussion of the previous version of this series[1] and the use cases found in the kernel, it does look like having several flavours of case conversion functions is beneficial. The use cases fall into three categories:
- copying a string and converting the case while specifying a maximum length to mimic strlcpy()
- copying a string and converting the case without specifying a length to mimic strcpy()
- converting the case of a string in-place (i.e. modifying the string that was passed in)

Quoted 7 lines

Consequently, I am proposing these new functions:

     void strlcpytoupper(char *dst, const char *src, size_t len);
     void strlcpytolower(char *dst, const char *src, size_t len);
     void strcpytoupper(char *dst, const char *src);
     void strcpytolower(char *dst, const char *src);
     void strtoupper(char *s);
     void strtolower(char *s);

You may want to read the article here:

https://lwn.net/Articles/659214/

and follow up some of the discussion threads on LKML about the best semantics to advertise for the strlcpy/strscpy variants. It might be helpful to return some kind of overflow/truncation error from your copy functions so people can error-check the result.

Signature · 2 lines
Chris Metcalf, Mellanox Technologies
http://www.mellanox.com
#21 lore raw
Quoted 27 lines · Chris Metcalf
Quoted 17 lines · Markus Mayer

This series introduces a family of generic string case conversion functions. This kind of functionality is needed in several places in the kernel. Right now, everybody seems to be implementing their own copy of this functionality.

Based on the discussion of the previous version of this series[1] and the use cases found in the kernel, it does look like having several flavours of case conversion functions is beneficial. The use cases fall into three categories:
- copying a string and converting the case while specifying a maximum length to mimic strlcpy()
- copying a string and converting the case without specifying a length to mimic strcpy()
- converting the case of a string in-place (i.e. modifying the string that was passed in)

Quoted 7 lines

Consequently, I am proposing these new functions:

     void strlcpytoupper(char *dst, const char *src, size_t len);
     void strlcpytolower(char *dst, const char *src, size_t len);
     void strcpytoupper(char *dst, const char *src);
     void strcpytolower(char *dst, const char *src);
     void strtoupper(char *s);
     void strtolower(char *s);
Quoted 3 lines

You may want to read the article here:

https://lwn.net/Articles/659214/

I'll read that. Thanks.

Quoted 4 lines

and follow up some of the discussion threads on LKML about the best semantics to advertise for the strlcpy/strscpy variants. It might be helpful to return some kind of overflow/truncation error from your copy functions so people can error-check the result.

I am inclined to agree. However, everybody has been telling me that these functions should be void. Originally they weren't.

Regards,
-Markus
_______________________________________________
Nouveau mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/nouveau

#4 lore raw
In reply to Markus Mayer
Quoted 62 lines · Markus Mayer
Quoted 48 lines · Luis de Bethencourt
Quoted 47 lines · Markus Mayer

Add a collection of generic functions to convert strings to lowercase or uppercase.

Changing the case of a string (with or without copying it first) seems to be a recurring requirement in the kernel that is currently being solved by several duplicated implementations doing the same thing. This change aims at reducing this code duplication.

The new functions are

    void strlcpytoupper(char *dst, const char *src, size_t len);
    void strlcpytolower(char *dst, const char *src, size_t len);
    void strcpytoupper(char *dst, const char *src);
    void strcpytolower(char *dst, const char *src);
    void strtoupper(char *s);
    void strtolower(char *s);

The "str[l]cpyto*" versions of the function take a destination string and a source string as arguments. The "strlcpyto*" versions additionally take a length argument like strlcpy() itself. Lastly, the strto* functions take a single string argument and modify the passed-in string.

Like strlcpy(), and unlike strncpy(), the functions guarantee NULL termination of the destination string.

---
 include/linux/string.h | 40 ++++++++++++++++++++++++++++++++++++++++
 lib/string.c           | 38 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 78 insertions(+)
include/linux/string.h +2 -0
index 26b6f6a..36c9d14 100644
@@ -116,6 +116,8 @@ extern void * memchr(const void *,int,__kernel_size_t);
 #endif
 void *memchr_inv(const void *s, int c, size_t n);
 char *strreplace(char *s, char old, char new);
+extern void strlcpytoupper(char *dst, const char *src, size_t len);
+extern void strlcpytolower(char *dst, const char *src, size_t len);
 
 extern void kfree_const(const void *x);
 
@@ -169,4 +171,42 @@ static inline const char *kbasename(const char *path)
      return tail ? tail + 1 : path;
 }
 
Quoted 12 lines

include/linux/string.h static inline const char *kbasename(const char *path) · line 183

Quoted 10 lines
include/linux/string.h +10 -0
+/**
+ * strcpytoupper - Copy string and convert to uppercase.
+ * @dst: The buffer to store the result.
+ * @src: The string to convert to uppercase.
+ */
+static inline void strcpytoupper(char *dst, const char *src)
+{
+     strlcpytoupper(dst, src, -1);
+}
+

Why not use SIZE_MAX instead of -1?

Quoted 1 line

Sure. I'll change all four of them. Thanks.

Turns out there's actually a circular dependency here. SIZE_MAX is defined in linux/kernel.h. So, string.h would need to include kernel.h. But kernel.h, by way of several other headers, includes string.h.

Attempting to include kernel.h in string.h then leads to something like this:

  CHK     include/config/kernel.release
  CHK     include/generated/uapi/linux/version.h
  CHK     include/generated/utsrelease.h
  CC      scripts/mod/devicetable-offsets.s
  CHK     include/generated/timeconst.h

In file included from include/linux/printk.h:289:0,
from include/linux/kernel.h:13,
from include/linux/string.h:11,
from include/uapi/linux/uuid.h:21,
from include/linux/uuid.h:19,
from include/linux/mod_devicetable.h:12,
from scripts/mod/devicetable-offsets.c:2:
include/linux/dynamic_debug.h: In function ‘ddebug_dyndbg_module_param_cb’:
include/linux/dynamic_debug.h:122:2: error: implicit declaration of function ‘strstr’ [-Werror=implicit-function-declaration]
if (strstr(param, "dyndbg")) {
^
include/linux/dynamic_debug.h:122:6: warning: incompatible implicit declaration of built-in function ‘strstr’ [enabled by default]

  if (strstr(param, "dyndbg")) {
      ^

Since kernel.h is referencing string.h (which is needed, but not included a second time due to the include guards), this leads to undeclared string functions, because we are still in the early stages of including string.h itself and haven't gotten to the function declarations yet.

Quoted 75 lines
Quoted 75 lines

include/linux/string.h static inline const char *kbasename(const char *path) · line 193

Quoted 10 lines
include/linux/string.h +10 -0
+/**
+ * strcpytolower - Copy string and convert to lowercase.
+ * @dst: The buffer to store the result.
+ * @src: The string to convert to lowercase.
+ */
+static inline void strcpytolower(char *dst, const char *src)
+{
+     strlcpytolower(dst, src, -1);
+}
+

Same here, and the 2 below :)

Thanks Markus,
Luis

Quoted 59 lines
include/linux/string.h +18 -0
+/**
+ * strtoupper - Convert string to uppercase.
+ * @s: The string to operate on.
+ */
+static inline void strtoupper(char *s)
+{
+     strlcpytoupper(s, s, -1);
+}
+
+/**
+ * strtolower - Convert string to lowercase.
+ * @s: The string to operate on.
+ */
+static inline void strtolower(char *s)
+{
+     strlcpytolower(s, s, -1);
+}
+
 #endif /* _LINUX_STRING_H_ */
lib/string.c +32 -0
index ed83562..fd8c427 100644
@@ -952,3 +952,41 @@ char *strreplace(char *s, char old, char new)
      return s;
 }
 EXPORT_SYMBOL(strreplace);
+
+/**
+ * strlcpytoupper - Copy a length-limited string and convert to uppercase.
+ * @dst: The buffer to store the result.
+ * @src: The string to convert to uppercase.
+ * @len: Maximum string length. May be SIZE_MAX (-1) to set no limit.
+ */
+void strlcpytoupper(char *dst, const char *src, size_t len)
+{
+     size_t i;
+
+     if (!len)
+             return;
+
+     for (i = 0; i < len && src[i]; ++i)
+             dst[i] = toupper(src[i]);
+     dst[i < len ? i : i - 1] = '\0';
+}
+EXPORT_SYMBOL(strlcpytoupper);
+
+/**
+ * strlcpytolower - Copy a length-limited string and convert to lowercase.
+ * @dst: The buffer to store the result.
+ * @src: The string to convert to lowercase.
+ * @len: Maximum string length. May be SIZE_MAX (-1) to set no limit.
+ */
+void strlcpytolower(char *dst, const char *src, size_t len)
+{
+     size_t i;
+
+     if (!len)
+             return;
Quoted 8 lines
Quoted 8 lines

lib/string.c char *strreplace(char *s, char old, char new) · line 992

Quoted 7 lines
lib/string.c +6 -0
+
+     for (i = 0; i < len && src[i]; ++i)
+             dst[i] = tolower(src[i]);
+     dst[i < len ? i : i - 1] = '\0';
+}
+EXPORT_SYMBOL(strlcpytolower);

_______________________________________________
Nouveau mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/nouveau

#5 lore raw
Quoted 91 lines · Markus Mayer
Quoted 62 lines · Markus Mayer
Quoted 48 lines · Luis de Bethencourt
Quoted 47 lines · Markus Mayer

Add a collection of generic functions to convert strings to lowercase or uppercase.

Changing the case of a string (with or without copying it first) seems to be a recurring requirement in the kernel that is currently being solved by several duplicated implementations doing the same thing. This change aims at reducing this code duplication.

The new functions are

    void strlcpytoupper(char *dst, const char *src, size_t len);
    void strlcpytolower(char *dst, const char *src, size_t len);
    void strcpytoupper(char *dst, const char *src);
    void strcpytolower(char *dst, const char *src);
    void strtoupper(char *s);
    void strtolower(char *s);

The "str[l]cpyto*" versions of the function take a destination string and a source string as arguments. The "strlcpyto*" versions additionally take a length argument like strlcpy() itself. Lastly, the strto* functions take a single string argument and modify the passed-in string.

Like strlcpy(), and unlike strncpy(), the functions guarantee NULL termination of the destination string.

---
 include/linux/string.h | 40 ++++++++++++++++++++++++++++++++++++++++
 lib/string.c           | 38 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 78 insertions(+)
include/linux/string.h +2 -0
index 26b6f6a..36c9d14 100644
@@ -116,6 +116,8 @@ extern void * memchr(const void *,int,__kernel_size_t);
 #endif
 void *memchr_inv(const void *s, int c, size_t n);
 char *strreplace(char *s, char old, char new);
+extern void strlcpytoupper(char *dst, const char *src, size_t len);
+extern void strlcpytolower(char *dst, const char *src, size_t len);
 
 extern void kfree_const(const void *x);
 
@@ -169,4 +171,42 @@ static inline const char *kbasename(const char *path)
      return tail ? tail + 1 : path;
 }
 
Quoted 12 lines

include/linux/string.h static inline const char *kbasename(const char *path) · line 183

Quoted 10 lines
include/linux/string.h +10 -0
+/**
+ * strcpytoupper - Copy string and convert to uppercase.
+ * @dst: The buffer to store the result.
+ * @src: The string to convert to uppercase.
+ */
+static inline void strcpytoupper(char *dst, const char *src)
+{
+     strlcpytoupper(dst, src, -1);
+}
+

Why not use SIZE_MAX instead of -1?

Quoted 1 line

Sure. I'll change all four of them. Thanks.

Turns out there's actually a circular dependency here. SIZE_MAX is defined in linux/kernel.h. So, string.h would need to include kernel.h. But kernel.h, by way of several other headers, includes string.h.

Attempting to include kernel.h in string.h then leads to something like this:

  CHK     include/config/kernel.release
  CHK     include/generated/uapi/linux/version.h
  CHK     include/generated/utsrelease.h
  CC      scripts/mod/devicetable-offsets.s
  CHK     include/generated/timeconst.h

In file included from include/linux/printk.h:289:0,
from include/linux/kernel.h:13,
from include/linux/string.h:11,
from include/uapi/linux/uuid.h:21,
from include/linux/uuid.h:19,
from include/linux/mod_devicetable.h:12,
from scripts/mod/devicetable-offsets.c:2:
include/linux/dynamic_debug.h: In function ‘ddebug_dyndbg_module_param_cb’:
include/linux/dynamic_debug.h:122:2: error: implicit declaration of function ‘strstr’ [-Werror=implicit-function-declaration]
if (strstr(param, "dyndbg")) {
^
include/linux/dynamic_debug.h:122:6: warning: incompatible implicit declaration of built-in function ‘strstr’ [enabled by default]

Quoted 8 lines
  if (strstr(param, "dyndbg")) {
      ^

Since kernel.h is referencing string.h (which is needed, but not included a second time due to the include guards), this leads to undeclared string functions, because we are still in the early stages of including string.h itself and haven't gotten to the function declarations yet.

Hi Markus,

Amazing. I see this happening as well, but I know it shouldn't.

The reason the #ifndef guards in headers are there is precisely to allow circular dependencies.

The problem in your output reads as:
strstr() is in string.h
#include string.h -> that includes kernel.h -> that includes string.h

The third should do nothing based on _LINUX_STRING_H_ being defined already and all code inside the #ifndef in string.h not being executed. Yet it shouldn't block the first include above since that macro isn't defined, which is what the error suggests since it doesn't have strstr() If _LINUX_STRING_H is defined, strstr() should be available.

Investigating this issue, it only happens when CONFIG_DYNAMIC_DEBUG is not set and line 170 of dynamic_debug.h runs, but just above we have an include of string.h.

Very strange that #include <linux/string.h> isn't doing its job.

The first thing I tried is to understand where dynamic_debug.h is used and removed the unneeded ones:
--------------------------------------------------------------------------

include/linux/kernel.h +0 -1
@@ -11,7 +11,6 @@
 #include <linux/log2.h>
 #include <linux/typecheck.h>
 #include <linux/printk.h>
-#include <linux/dynamic_debug.h>
 #include <asm/byteorder.h>
 #include <uapi/linux/kernel.h>
 
include/linux/printk.h +2 -1
@@ -307,10 +307,11 @@ asmlinkage __printf(1, 2) __cold void __pr_info(const char *fmt, ...);
        no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
 #endif
 
-#include <linux/dynamic_debug.h>
 
 /* If you are writing a driver, please use dev_dbg instead */
 #if defined(CONFIG_DYNAMIC_DEBUG)
+#include <linux/dynamic_debug.h>
+
 /* dynamic_pr_debug() uses pr_fmt() internally so we don't need it here */
 #define pr_debug(fmt, ...) \
        dynamic_pr_debug(fmt, ##__VA_ARGS__)
kernel/module.c +1 -1
index beaebea..e70a2fa 100644
@@ -60,6 +60,7 @@
 #include <linux/jump_label.h>
 #include <linux/pfn.h>
 #include <linux/bsearch.h>
+#include <linux/dynamic_debug.h>
 #include <uapi/linux/module.h>
 #include "module-internal.h"
--------------------------------------------------------------------------

This diff [0] fixes the issue but it is a workaround for the original issue about string.h not being properly included in dynamic_debug.h

Puzzled by this and can't figure out what is happening wrong.

The second thing I tried was adding
#warning "Linking to string header"
in include/linux/string.h, and I don't see any include path mentioning kernel.h, where do you see the circular dependency? I might be missing something.

Thanks,
Luis

[0] Sent for comments:
https://lkml.org/lkml/2016/7/13/686

Quoted 75 lines
Quoted 75 lines
Quoted 75 lines

include/linux/string.h static inline const char *kbasename(const char *path) · line 193

Quoted 10 lines
include/linux/string.h +10 -0
+/**
+ * strcpytolower - Copy string and convert to lowercase.
+ * @dst: The buffer to store the result.
+ * @src: The string to convert to lowercase.
+ */
+static inline void strcpytolower(char *dst, const char *src)
+{
+     strlcpytolower(dst, src, -1);
+}
+

Same here, and the 2 below :)

Thanks Markus,
Luis

Quoted 59 lines
include/linux/string.h +18 -0
+/**
+ * strtoupper - Convert string to uppercase.
+ * @s: The string to operate on.
+ */
+static inline void strtoupper(char *s)
+{
+     strlcpytoupper(s, s, -1);
+}
+
+/**
+ * strtolower - Convert string to lowercase.
+ * @s: The string to operate on.
+ */
+static inline void strtolower(char *s)
+{
+     strlcpytolower(s, s, -1);
+}
+
 #endif /* _LINUX_STRING_H_ */
lib/string.c +32 -0
index ed83562..fd8c427 100644
@@ -952,3 +952,41 @@ char *strreplace(char *s, char old, char new)
      return s;
 }
 EXPORT_SYMBOL(strreplace);
+
+/**
+ * strlcpytoupper - Copy a length-limited string and convert to uppercase.
+ * @dst: The buffer to store the result.
+ * @src: The string to convert to uppercase.
+ * @len: Maximum string length. May be SIZE_MAX (-1) to set no limit.
+ */
+void strlcpytoupper(char *dst, const char *src, size_t len)
+{
+     size_t i;
+
+     if (!len)
+             return;
+
+     for (i = 0; i < len && src[i]; ++i)
+             dst[i] = toupper(src[i]);
+     dst[i < len ? i : i - 1] = '\0';
+}
+EXPORT_SYMBOL(strlcpytoupper);
+
+/**
+ * strlcpytolower - Copy a length-limited string and convert to lowercase.
+ * @dst: The buffer to store the result.
+ * @src: The string to convert to lowercase.
+ * @len: Maximum string length. May be SIZE_MAX (-1) to set no limit.
+ */
+void strlcpytolower(char *dst, const char *src, size_t len)
+{
+     size_t i;
+
+     if (!len)
+             return;
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

lib/string.c char *strreplace(char *s, char old, char new) · line 992

Quoted 7 lines
lib/string.c +6 -0
+
+     for (i = 0; i < len && src[i]; ++i)
+             dst[i] = tolower(src[i]);
+     dst[i < len ? i : i - 1] = '\0';
+}
+EXPORT_SYMBOL(strlcpytolower);

_______________________________________________
Nouveau mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/nouveau

Acked-by on v3 4/7
#13 lore raw

Re: [PATCH v3 4/7] staging: speakup: replace spk_strlwr() with strlcpytolower()

In reply to Markus Mayer
Quoted 4 lines · Markus Mayer, on Fri 08 Jul 2016 15:43:11 -0700, wrote:

After introducing generic strltolower() and strtolower(), spk_strlwr() is no longer needed.

Quoted 85 lines

---

Samuel, I left off your ACK, since the implementation of my function changed in v2 and again in v3. This patch has been updated to match the new strlcpy() semantics. Here, this means we want the length argument to be sizeof(new_synth_name).

See https://patchwork.kernel.org/patch/9215217/ for the previous version.

 drivers/staging/speakup/kobjects.c    |  3 +--
 drivers/staging/speakup/main.c        |  3 ++-
 drivers/staging/speakup/speakup.h     |  1 -
 drivers/staging/speakup/varhandlers.c | 12 ------------
 4 files changed, 3 insertions(+), 16 deletions(-)
drivers/staging/speakup/kobjects.c +1 -2
index 528cbdc..c6e0c2d 100644
@@ -387,11 +387,10 @@ static ssize_t synth_store(struct kobject *kobj, struct kobj_attribute *attr,
 	len = strlen(buf);
 	if (len < 2 || len > 9)
 		return -EINVAL;
-	strncpy(new_synth_name, buf, len);
+	strlcpytolower(new_synth_name, buf, sizeof(new_synth_name));
 	if (new_synth_name[len - 1] == '\n')
 		len--;
 	new_synth_name[len] = '\0';
-	spk_strlwr(new_synth_name);
 	if ((synth != NULL) && (!strcmp(new_synth_name, synth->name))) {
 		pr_warn("%s already in use\n", new_synth_name);
 	} else if (synth_init(new_synth_name) != 0) {
drivers/staging/speakup/main.c +2 -1
index 97ca4ec..970f568 100644
@@ -2314,7 +2314,8 @@ static int __init speakup_init(void)
 	spk_initialize_msgs();	/* Initialize arrays for i18n. */
 	spk_reset_default_chars();
 	spk_reset_default_chartab();
-	spk_strlwr(synth_name);
+	if (synth_name)
+		strtolower(synth_name);
 	spk_vars[0].u.n.high = vc->vc_cols;
 	for (var = spk_vars; var->var_id != MAXVARS; var++)
 		speakup_register_var(var);
drivers/staging/speakup/speakup.h +0 -1
index df74c91..4725785 100644
@@ -50,7 +50,6 @@ void synth_insert_next_index(int sent_num);
 void spk_reset_index_count(int sc);
 void spk_get_index_count(int *linecount, int *sentcount);
 int spk_set_key_info(const u_char *key_info, u_char *k_buffer);
-char *spk_strlwr(char *s);
 char *spk_s2uchar(char *start, char *dest);
 int speakup_kobj_init(void);
 void speakup_kobj_exit(void);
drivers/staging/speakup/varhandlers.c +0 -12
index e1393d2..a1af222 100644
@@ -309,18 +309,6 @@ int spk_set_mask_bits(const char *input, const int which, const int how)
 	return 0;
 }
 
-char *spk_strlwr(char *s)
-{
-	char *p;
-
-	if (!s)
-		return NULL;
-
-	for (p = s; *p; p++)
-		*p = tolower(*p);
-	return s;
-}
-
 char *spk_s2uchar(char *start, char *dest)
 {
 	int val;
Signature · 1 line
2.7.4
Signature · 4 lines
Samuel
  bien sûr que ça convient mieux à tout le monde
  enfin, dans la mesure où tout le monde c'est comme moi
 -+- le consensus, c'est facile -+-
#6 lore raw
In reply to Luis de Bethencourt
Quoted 71 lines · Luis de Bethencourt

On 11/07/16 23:46, Markus Mayer wrote:

Hi Markus,

Amazing. I see this happening as well, but I know it shouldn't.

The reason the #ifndef guards in headers are there is precisely to allow circular dependencies.

The problem in your output reads as:
strstr() is in string.h
#include string.h -> that includes kernel.h -> that includes string.h

The third should do nothing based on _LINUX_STRING_H_ being defined already and all code inside the #ifndef in string.h not being executed. Yet it shouldn't block the first include above since that macro isn't defined, which is what the error suggests since it doesn't have strstr() If _LINUX_STRING_H is defined, strstr() should be available.

Investigating this issue, it only happens when CONFIG_DYNAMIC_DEBUG is not set and line 170 of dynamic_debug.h runs, but just above we have an include of string.h.

Very strange that #include <linux/string.h> isn't doing its job.

The first thing I tried is to understand where dynamic_debug.h is used and removed the unneeded ones:
--------------------------------------------------------------------------

include/linux/kernel.h +0 -1
@@ -11,7 +11,6 @@
 #include <linux/log2.h>
 #include <linux/typecheck.h>
 #include <linux/printk.h>
-#include <linux/dynamic_debug.h>
 #include <asm/byteorder.h>
 #include <uapi/linux/kernel.h>
 
include/linux/printk.h +2 -1
@@ -307,10 +307,11 @@ asmlinkage __printf(1, 2) __cold void __pr_info(const char *fmt, ...);
        no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
 #endif
 
-#include <linux/dynamic_debug.h>
 
 /* If you are writing a driver, please use dev_dbg instead */
 #if defined(CONFIG_DYNAMIC_DEBUG)
+#include <linux/dynamic_debug.h>
+
 /* dynamic_pr_debug() uses pr_fmt() internally so we don't need it here */
 #define pr_debug(fmt, ...) \
        dynamic_pr_debug(fmt, ##__VA_ARGS__)
kernel/module.c +1 -1
index beaebea..e70a2fa 100644
@@ -60,6 +60,7 @@
 #include <linux/jump_label.h>
 #include <linux/pfn.h>
 #include <linux/bsearch.h>
+#include <linux/dynamic_debug.h>
 #include <uapi/linux/module.h>
 #include "module-internal.h"
--------------------------------------------------------------------------

This diff [0] fixes the issue but it is a workaround for the original issue about string.h not being properly included in dynamic_debug.h

Quoted 7 lines

Puzzled by this and can't figure out what is happening wrong.

The second thing I tried was adding
#warning "Linking to string header"
in include/linux/string.h, and I don't see any include path mentioning kernel.h, where do you see the circular dependency? I might be missing something.

I did some more poking around and this is what I found.

For starters, the problem happens with kernel/bounds.c. Without worrying about SIZE_MAX or making any other changes, I added a #warning line to kernel.h and string.h to see the include sequence.

$ aarch64-linux-gcc -Wp,-MD,kernel/.bounds.s.d  -nostdinc -isystem
/opt/toolchain/stbgcc-4.8-1.5/bin/../lib/gcc/aarch64-linux-gnu/4.8.5/include
-I./arch/arm64/include -Iarch/arm64/include/generated/uapi
-Iarch/arm64/include/generated  -Iinclude -I./arch/arm64/include/uapi
-Iarch/arm64/include/generated/uapi -I./include/uapi
-Iinclude/generated/uapi -include ./include/linux/kconfig.h
-D__KERNEL__ -mlittle-endian -Wall -Wundef -Wstrict-prototypes
-Wno-trigraphs -fno-strict-aliasing -fno-common
-Werror-implicit-function-declaration -Wno-format-security -std=gnu89
-mgeneral-regs-only -fno-asynchronous-unwind-tables
-fno-delete-null-pointer-checks -O2 --param=allow-store-data-races=0
-Wframe-larger-than=2048 -fno-stack-protector
-Wno-unused-but-set-variable -fno-omit-frame-pointer
-fno-optimize-sibling-calls -fno-var-tracking-assignments -g
-Wdeclaration-after-statement -Wno-pointer-sign -fno-strict-overflow
-fconserve-stack -Werror=implicit-int -Werror=strict-prototypes
-DKBUILD_BASENAME='"bounds"'  -DKBUILD_MODNAME='"bounds"'
-fverbose-asm -E -o kernel/bounds.i kernel/bounds.c
In file included from include/asm-generic/bug.h:13:0,
                 from ./arch/arm64/include/asm/bug.h:62,
                 from include/linux/bug.h:4,
                 from include/linux/page-flags.h:9,
                 from kernel/bounds.c:9:
include/linux/kernel.h:4:2: warning: #warning In kernel.h [-Wcpp]
 #warning In kernel.h
  ^
In file included from include/linux/dynamic_debug.h:111:0,
                 from include/linux/printk.h:289,
                 from include/linux/kernel.h:14,
                 from include/asm-generic/bug.h:13,
                 from ./arch/arm64/include/asm/bug.h:62,
                 from include/linux/bug.h:4,
                 from include/linux/page-flags.h:9,
                 from kernel/bounds.c:9:
include/linux/string.h:4:2: warning: #warning In string.h [-Wcpp]
 #warning In string.h
  ^

So, kernel.h gets pulled in first. string.h gets pulled in by kernel.h (via dynamic_debug.h as you pointed out). The build still succeeds at this point (because I didn't change string.h yet).

Now, if I reference SIZE_MAX in string.h, there's a problem: string.h gets pulled in at the top of kernel.h, before kernel.h defines SIZE_MAX. The compiler complains ("error: ‘SIZE_MAX’ undeclared").

If I add "#include <linux/kernel.h>" to the top of string.h in an attempt to get SIZE_MAX sooner, nothing happens, because kernel.h has already been included (and therefore the include guard is defined, meaning that the new attempt to include it doesn't do anything). So, I am leaving it out at this point.

To fix the SIZE_MAX issue, I quickly hacked up kernel.h, so SIZE_MAX gets defined before string.h gets pulled in.

$ git diff
include/linux/kernel.h +3 -2
index 94aa10f..6cd5269 100644
@@ -10,8 +10,6 @@
 #include <linux/bitops.h>
 #include <linux/log2.h>
 #include <linux/typecheck.h>
-#include <linux/printk.h>
-#include <linux/dynamic_debug.h>
 #include <asm/byteorder.h>
 #include <uapi/linux/kernel.h>
 
@@ -29,6 +27,9 @@
 #define ULLONG_MAX     (~0ULL)
 #define SIZE_MAX       (~(size_t)0)
 
+#include <linux/printk.h>
+#include <linux/dynamic_debug.h>
+
 #define U8_MAX         ((u8)~0U)
 #define S8_MAX         ((s8)(U8_MAX>>1))
 #define S8_MIN         ((s8)(-S8_MAX - 1))

This solves bounds.c. But it doesn't help with other files that don't automatically pull in kernel.h before string.h. They still complain about SIZE_MAX being undeclared. So, #include <linux/kernel.h> *does* need to go into string.h. At this point, we run into the next problem.

$ aarch64-linux-gcc -Wp,-MD,scripts/mod/.devicetable-offsets.s.d
-nostdinc -isystem
/opt/toolchain/stbgcc-4.8-1.5/bin/../lib/gcc/aarch64-linux-gnu/4.8.5/include
-I./arch/arm64/include -Iarch/arm64/include/generated/uapi
-Iarch/arm64/include/generated  -Iinclude -I./arch/arm64/include/uapi
-Iarch/arm64/include/generated/uapi -I./include/uapi
-Iinclude/generated/uapi -include ./include/linux/kconfig.h
-D__KERNEL__ -mlittle-endian -Wall -Wundef -Wstrict-prototypes
-Wno-trigraphs -fno-strict-aliasing -fno-common
-Werror-implicit-function-declaration -Wno-format-security -std=gnu89
-mgeneral-regs-only -fno-asynchronous-unwind-tables
-fno-delete-null-pointer-checks -O2 --param=allow-store-data-races=0
-Wframe-larger-than=2048 -fno-stack-protector
-Wno-unused-but-set-variable -fno-omit-frame-pointer
-fno-optimize-sibling-calls -fno-var-tracking-assignments -g
-Wdeclaration-after-statement -Wno-pointer-sign -fno-strict-overflow
-fconserve-stack -Werror=implicit-int -Werror=strict-prototypes
-DKBUILD_BASENAME='"devicetable_offsets"'
-DKBUILD_MODNAME='"devicetable_offsets"'  -fverbose-asm -S -o
scripts/mod/devicetable-offsets.s scripts/mod/devicetable-offsets.c
In file included from include/uapi/linux/uuid.h:21:0,
                 from include/linux/uuid.h:19,
                 from include/linux/mod_devicetable.h:12,
                 from scripts/mod/devicetable-offsets.c:2:
include/linux/string.h:4:2: warning: #warning In string.h [-Wcpp]
 #warning In string.h
  ^
In file included from include/linux/string.h:7:0,
                 from include/uapi/linux/uuid.h:21,
                 from include/linux/uuid.h:19,
                 from include/linux/mod_devicetable.h:12,
                 from scripts/mod/devicetable-offsets.c:2:
include/linux/kernel.h:4:2: warning: #warning In kernel.h [-Wcpp]
 #warning In kernel.h
  ^
In file included from include/linux/printk.h:289:0,
                 from include/linux/kernel.h:31,
                 from include/linux/string.h:7,
                 from include/uapi/linux/uuid.h:21,
                 from include/linux/uuid.h:19,
                 from include/linux/mod_devicetable.h:12,
                 from scripts/mod/devicetable-offsets.c:2:
include/linux/dynamic_debug.h: In function ‘ddebug_dyndbg_module_param_cb’:
include/linux/dynamic_debug.h:122:2: error: implicit declaration of
function ‘strstr’ [-Werror=implicit-function-declaration]
  if (strstr(param, "dyndbg")) {
  ^
include/linux/dynamic_debug.h:122:6: warning: incompatible implicit
declaration of built-in function ‘strstr’ [enabled by default]
  if (strstr(param, "dyndbg")) {
      ^
cc1: some warnings being treated as errors

So, now we have the following sequence:

string.h -> kernel.h -> dynamic_debug.h -> oops, we don't know strstr() yet.

Yes, dynamic_debug.h does include string.h, but because of the include guard, that second attempt at including string.h is stubbed out and doesn't do anything.

To get past that issue, I can move "#include <linux/kernel.h>" all the way down and only include it directly before my new functions. That way strstr() is declared when dynamic_debug.h wants it.

include/linux/string.h +2 -0
index 6cc85dc..eb2d9a8 100644
@@ -171,6 +171,8 @@ static inline const char *kbasename(const char *path)
        return tail ? tail + 1 : path;
 }
 
+#include <linux/kernel.h>
+
 /**
  * strcpytoupper - Copy string and convert to uppercase.
  * @dst: The buffer to store the result.

This is some scary stuff, but this way it seems to build. Not too sure how to fix this properly and without risking some major headaches when one least expects them.

Regards,
-Markus

Quoted 2 lines

Thanks,
Luis

_______________________________________________
Nouveau mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/nouveau

#22 lore raw
In reply to Markus Mayer
Quoted 28 lines · Markus Mayer
Quoted 27 lines · Chris Metcalf
Quoted 24 lines · Markus Mayer

This series introduces a family of generic string case conversion functions. This kind of functionality is needed in several places in the kernel. Right now, everybody seems to be implementing their own copy of this functionality.

Based on the discussion of the previous version of this series[1] and the use cases found in the kernel, it does look like having several flavours of case conversion functions is beneficial. The use cases fall into three categories:
- copying a string and converting the case while specifying a maximum length to mimic strlcpy()
- copying a string and converting the case without specifying a length to mimic strcpy()
- converting the case of a string in-place (i.e. modifying the string that was passed in)

Consequently, I am proposing these new functions:

     void strlcpytoupper(char *dst, const char *src, size_t len);
     void strlcpytolower(char *dst, const char *src, size_t len);
     void strcpytoupper(char *dst, const char *src);
     void strcpytolower(char *dst, const char *src);
     void strtoupper(char *s);
     void strtolower(char *s);
Quoted 5 lines
Quoted 3 lines

You may want to read the article here:

https://lwn.net/Articles/659214/

I'll read that. Thanks.

It doesn't look like there is going to be the danger of "mass changes". So far, I have two ACKs (one where the semantics doesn't change, because it's using strtolower()) and the other in a driver in staging.

But I understand the concern and will keep an eye out if there are other ACKs.

Quoted 7 lines
Quoted 4 lines

and follow up some of the discussion threads on LKML about the best semantics to advertise for the strlcpy/strscpy variants. It might be helpful to return some kind of overflow/truncation error from your copy functions so people can error-check the result.

I am inclined to agree. However, everybody has been telling me that these functions should be void. Originally they weren't.

What about something like this? It might also work to keep the four static inline functions as "void" (since they won't ever return E2BIG anyway) and just have strlcpyto* return an integer (since that's where the buffer could be too small).

Rasmus, what's your take?

include/linux/string.h +18 -10
index ae82d13..6cc85dc 100644
@@ -116,8 +116,8 @@ extern void * memchr(const void *,int,__kernel_size_t);
 #endif
 void *memchr_inv(const void *s, int c, size_t n);
 char *strreplace(char *s, char old, char new);
-extern void strlcpytoupper(char *dst, const char *src, size_t len);
-extern void strlcpytolower(char *dst, const char *src, size_t len);
+extern int strlcpytoupper(char *dst, const char *src, size_t len);
+extern int strlcpytolower(char *dst, const char *src, size_t len);
 
 extern void kfree_const(const void *x);
 
@@ -175,38 +175,46 @@ static inline const char *kbasename(const char *path)
  * strcpytoupper - Copy string and convert to uppercase.
  * @dst: The buffer to store the result.
  * @src: The string to convert to uppercase.
+ *
+ * Returns the number of characters copied.
  */
-static inline void strcpytoupper(char *dst, const char *src)
+static inline int strcpytoupper(char *dst, const char *src)
 {
-	strlcpytoupper(dst, src, ~(size_t)0);
+	return strlcpytoupper(dst, src, ~(size_t)0);
 }
 
 /**
  * strcpytolower - Copy string and convert to lowercase.
  * @dst: The buffer to store the result.
  * @src: The string to convert to lowercase.
+ *
+ * Returns the number of characters copied.
  */
-static inline void strcpytolower(char *dst, const char *src)
+static inline int strcpytolower(char *dst, const char *src)
 {
-	strlcpytolower(dst, src, ~(size_t)0);
+	return strlcpytolower(dst, src, ~(size_t)0);
 }
 
 /**
  * strtoupper - Convert string to uppercase.
  * @s: The string to operate on.
+ *
+ * Returns the number of characters copied.
  */
-static inline void strtoupper(char *s)
+static inline int strtoupper(char *s)
 {
-	strlcpytoupper(s, s, ~(size_t)0);
+	return strlcpytoupper(s, s, ~(size_t)0);
 }
 
 /**
  * strtolower - Convert string to lowercase.
  * @s: The string to operate on.
+ *
+ * Returns the number of characters copied.
  */
-static inline void strtolower(char *s)
+static inline int strtolower(char *s)
 {
-	strlcpytolower(s, s, ~(size_t)0);
+	return strlcpytolower(s, s, ~(size_t)0);
 }
 
 #endif /* _LINUX_STRING_H_ */
lib/string.c +12 -4
index 7903e10..d36d5fb2 100644
@@ -958,17 +958,21 @@ EXPORT_SYMBOL(strreplace);
  * @dst: The buffer to store the result.
  * @src: The string to convert to uppercase.
  * @len: Maximum string length. May be SIZE_MAX to set no limit.
+ *
+ * Returns the number of characters copied or -E2BIG if @dst wasn't big enough.
  */
-void strlcpytoupper(char *dst, const char *src, size_t len)
+int strlcpytoupper(char *dst, const char *src, size_t len)
 {
 	size_t i;
 
 	if (!len)
-		return;
+		return -E2BIG;
 
 	for (i = 0; i < len && src[i]; ++i)
 		dst[i] = toupper(src[i]);
 	dst[i < len ? i : i - 1] = '\0';
+
+	return (i < len) ? i : -E2BIG;
 }
 EXPORT_SYMBOL(strlcpytoupper);
 
@@ -977,16 +981,20 @@ EXPORT_SYMBOL(strlcpytoupper);
  * @dst: The buffer to store the result.
  * @src: The string to convert to lowercase.
  * @len: Maximum string length. May be SIZE_MAX to set no limit.
+ *
+ * Returns the number of characters copied or -E2BIG if @dst wasn't big enough.
  */
-void strlcpytolower(char *dst, const char *src, size_t len)
+int strlcpytolower(char *dst, const char *src, size_t len)
 {
 	size_t i;
 
 	if (!len)
-		return;
+		return -E2BIG;
 
 	for (i = 0; i < len && src[i]; ++i)
 		dst[i] = tolower(src[i]);
 	dst[i < len ? i : i - 1] = '\0';
+
+	return (i < len) ? i : -E2BIG;
 }
 EXPORT_SYMBOL(strlcpytolower);

_______________________________________________
Nouveau mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/nouveau

#7 lore raw
In reply to Markus Mayer
Quoted 250 lines · Markus Mayer
Quoted 71 lines · Luis de Bethencourt

On 11/07/16 23:46, Markus Mayer wrote:

Hi Markus,

Amazing. I see this happening as well, but I know it shouldn't.

The reason the #ifndef guards in headers are there is precisely to allow circular dependencies.

The problem in your output reads as:
strstr() is in string.h
#include string.h -> that includes kernel.h -> that includes string.h

The third should do nothing based on _LINUX_STRING_H_ being defined already and all code inside the #ifndef in string.h not being executed. Yet it shouldn't block the first include above since that macro isn't defined, which is what the error suggests since it doesn't have strstr() If _LINUX_STRING_H is defined, strstr() should be available.

Investigating this issue, it only happens when CONFIG_DYNAMIC_DEBUG is not set and line 170 of dynamic_debug.h runs, but just above we have an include of string.h.

Very strange that #include <linux/string.h> isn't doing its job.

The first thing I tried is to understand where dynamic_debug.h is used and removed the unneeded ones:
--------------------------------------------------------------------------

include/linux/kernel.h +0 -1
@@ -11,7 +11,6 @@
 #include <linux/log2.h>
 #include <linux/typecheck.h>
 #include <linux/printk.h>
-#include <linux/dynamic_debug.h>
 #include <asm/byteorder.h>
 #include <uapi/linux/kernel.h>
 
include/linux/printk.h +2 -1
@@ -307,10 +307,11 @@ asmlinkage __printf(1, 2) __cold void __pr_info(const char *fmt, ...);
        no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
 #endif
 
-#include <linux/dynamic_debug.h>
 
 /* If you are writing a driver, please use dev_dbg instead */
 #if defined(CONFIG_DYNAMIC_DEBUG)
+#include <linux/dynamic_debug.h>
+
 /* dynamic_pr_debug() uses pr_fmt() internally so we don't need it here */
 #define pr_debug(fmt, ...) \
        dynamic_pr_debug(fmt, ##__VA_ARGS__)
kernel/module.c +1 -1
index beaebea..e70a2fa 100644
@@ -60,6 +60,7 @@
 #include <linux/jump_label.h>
 #include <linux/pfn.h>
 #include <linux/bsearch.h>
+#include <linux/dynamic_debug.h>
 #include <uapi/linux/module.h>
 #include "module-internal.h"
--------------------------------------------------------------------------

This diff [0] fixes the issue but it is a workaround for the original issue about string.h not being properly included in dynamic_debug.h

Quoted 7 lines

Puzzled by this and can't figure out what is happening wrong.

The second thing I tried was adding
#warning "Linking to string header"
in include/linux/string.h, and I don't see any include path mentioning kernel.h, where do you see the circular dependency? I might be missing something.

I did some more poking around and this is what I found.

For starters, the problem happens with kernel/bounds.c. Without worrying about SIZE_MAX or making any other changes, I added a #warning line to kernel.h and string.h to see the include sequence.

$ aarch64-linux-gcc -Wp,-MD,kernel/.bounds.s.d  -nostdinc -isystem
/opt/toolchain/stbgcc-4.8-1.5/bin/../lib/gcc/aarch64-linux-gnu/4.8.5/include
(quoted diff) +0 -16
-I./arch/arm64/include -Iarch/arm64/include/generated/uapi
-Iarch/arm64/include/generated  -Iinclude -I./arch/arm64/include/uapi
-Iarch/arm64/include/generated/uapi -I./include/uapi
-Iinclude/generated/uapi -include ./include/linux/kconfig.h
-D__KERNEL__ -mlittle-endian -Wall -Wundef -Wstrict-prototypes
-Wno-trigraphs -fno-strict-aliasing -fno-common
-Werror-implicit-function-declaration -Wno-format-security -std=gnu89
-mgeneral-regs-only -fno-asynchronous-unwind-tables
-fno-delete-null-pointer-checks -O2 --param=allow-store-data-races=0
-Wframe-larger-than=2048 -fno-stack-protector
-Wno-unused-but-set-variable -fno-omit-frame-pointer
-fno-optimize-sibling-calls -fno-var-tracking-assignments -g
-Wdeclaration-after-statement -Wno-pointer-sign -fno-strict-overflow
-fconserve-stack -Werror=implicit-int -Werror=strict-prototypes
-DKBUILD_BASENAME='"bounds"'  -DKBUILD_MODNAME='"bounds"'
-fverbose-asm -E -o kernel/bounds.i kernel/bounds.c

In file included from include/asm-generic/bug.h:13:0,

                 from ./arch/arm64/include/asm/bug.h:62,
                 from include/linux/bug.h:4,
                 from include/linux/page-flags.h:9,
                 from kernel/bounds.c:9:

include/linux/kernel.h:4:2: warning: #warning In kernel.h [-Wcpp]
#warning In kernel.h
^
In file included from include/linux/dynamic_debug.h:111:0,

                 from include/linux/printk.h:289,
                 from include/linux/kernel.h:14,
                 from include/asm-generic/bug.h:13,
                 from ./arch/arm64/include/asm/bug.h:62,
                 from include/linux/bug.h:4,
                 from include/linux/page-flags.h:9,
                 from kernel/bounds.c:9:

include/linux/string.h:4:2: warning: #warning In string.h [-Wcpp]
#warning In string.h
^

So, kernel.h gets pulled in first. string.h gets pulled in by kernel.h (via dynamic_debug.h as you pointed out). The build still succeeds at this point (because I didn't change string.h yet).

Now, if I reference SIZE_MAX in string.h, there's a problem: string.h gets pulled in at the top of kernel.h, before kernel.h defines SIZE_MAX. The compiler complains ("error: ‘SIZE_MAX’ undeclared").

If I add "#include <linux/kernel.h>" to the top of string.h in an attempt to get SIZE_MAX sooner, nothing happens, because kernel.h has already been included (and therefore the include guard is defined, meaning that the new attempt to include it doesn't do anything). So, I am leaving it out at this point.

To fix the SIZE_MAX issue, I quickly hacked up kernel.h, so SIZE_MAX gets defined before string.h gets pulled in.

$ git diff
include/linux/kernel.h +3 -2
index 94aa10f..6cd5269 100644
@@ -10,8 +10,6 @@
 #include <linux/bitops.h>
 #include <linux/log2.h>
 #include <linux/typecheck.h>
-#include <linux/printk.h>
-#include <linux/dynamic_debug.h>
 #include <asm/byteorder.h>
 #include <uapi/linux/kernel.h>
 
@@ -29,6 +27,9 @@
 #define ULLONG_MAX     (~0ULL)
 #define SIZE_MAX       (~(size_t)0)
 
+#include <linux/printk.h>
+#include <linux/dynamic_debug.h>
+
 #define U8_MAX         ((u8)~0U)
 #define S8_MAX         ((s8)(U8_MAX>>1))
 #define S8_MIN         ((s8)(-S8_MAX - 1))

This solves bounds.c. But it doesn't help with other files that don't automatically pull in kernel.h before string.h. They still complain about SIZE_MAX being undeclared. So, #include <linux/kernel.h> *does* need to go into string.h. At this point, we run into the next problem.

$ aarch64-linux-gcc -Wp,-MD,scripts/mod/.devicetable-offsets.s.d
-nostdinc -isystem
/opt/toolchain/stbgcc-4.8-1.5/bin/../lib/gcc/aarch64-linux-gnu/4.8.5/include
include/linux/kernel.h +0 -16
-I./arch/arm64/include -Iarch/arm64/include/generated/uapi
-Iarch/arm64/include/generated  -Iinclude -I./arch/arm64/include/uapi
-Iarch/arm64/include/generated/uapi -I./include/uapi
-Iinclude/generated/uapi -include ./include/linux/kconfig.h
-D__KERNEL__ -mlittle-endian -Wall -Wundef -Wstrict-prototypes
-Wno-trigraphs -fno-strict-aliasing -fno-common
-Werror-implicit-function-declaration -Wno-format-security -std=gnu89
-mgeneral-regs-only -fno-asynchronous-unwind-tables
-fno-delete-null-pointer-checks -O2 --param=allow-store-data-races=0
-Wframe-larger-than=2048 -fno-stack-protector
-Wno-unused-but-set-variable -fno-omit-frame-pointer
-fno-optimize-sibling-calls -fno-var-tracking-assignments -g
-Wdeclaration-after-statement -Wno-pointer-sign -fno-strict-overflow
-fconserve-stack -Werror=implicit-int -Werror=strict-prototypes
-DKBUILD_BASENAME='"devicetable_offsets"'
-DKBUILD_MODNAME='"devicetable_offsets"'  -fverbose-asm -S -o

scripts/mod/devicetable-offsets.s scripts/mod/devicetable-offsets.c In file included from include/uapi/linux/uuid.h:21:0,
from include/linux/uuid.h:19,
from include/linux/mod_devicetable.h:12,
from scripts/mod/devicetable-offsets.c:2:
include/linux/string.h:4:2: warning: #warning In string.h [-Wcpp]
#warning In string.h
^
In file included from include/linux/string.h:7:0,
from include/uapi/linux/uuid.h:21,
from include/linux/uuid.h:19,
from include/linux/mod_devicetable.h:12,
from scripts/mod/devicetable-offsets.c:2:
include/linux/kernel.h:4:2: warning: #warning In kernel.h [-Wcpp]
#warning In kernel.h
^
In file included from include/linux/printk.h:289:0,
from include/linux/kernel.h:31,
from include/linux/string.h:7,
from include/uapi/linux/uuid.h:21,
from include/linux/uuid.h:19,
from include/linux/mod_devicetable.h:12,
from scripts/mod/devicetable-offsets.c:2:
include/linux/dynamic_debug.h: In function ‘ddebug_dyndbg_module_param_cb’:
include/linux/dynamic_debug.h:122:2: error: implicit declaration of function ‘strstr’ [-Werror=implicit-function-declaration]
if (strstr(param, "dyndbg")) {
^
include/linux/dynamic_debug.h:122:6: warning: incompatible implicit declaration of built-in function ‘strstr’ [enabled by default]

  if (strstr(param, "dyndbg")) {
      ^

cc1: some warnings being treated as errors

So, now we have the following sequence:

string.h -> kernel.h -> dynamic_debug.h -> oops, we don't know strstr() yet.

Yes, dynamic_debug.h does include string.h, but because of the include guard, that second attempt at including string.h is stubbed out and doesn't do anything.

To get past that issue, I can move "#include <linux/kernel.h>" all the way down and only include it directly before my new functions. That way strstr() is declared when dynamic_debug.h wants it.

include/linux/string.h +2 -0
index 6cc85dc..eb2d9a8 100644
@@ -171,6 +171,8 @@ static inline const char *kbasename(const char *path)
        return tail ? tail + 1 : path;
 }
 
+#include <linux/kernel.h>
+
 /**
  * strcpytoupper - Copy string and convert to uppercase.
  * @dst: The buffer to store the result.
Quoted 9 lines

This is some scary stuff, but this way it seems to build. Not too sure how to fix this properly and without risking some major headaches when one least expects them.

Regards,
-Markus

Quoted 2 lines

Thanks,
Luis

Hi Markus,

Thank you very much for the thorough testing and solution searching.

Earlier today I sent a patch that removes dynamic_debug.h from include/linux/kernel.h, since it isn't really needed. Sorry about this since it changes what you were testing just a few hours later.

I am starting to think that getting access to SIZE_MAX isn't worth the trouble, specially considering that moving an include out of the top of the file makes the code less readable. The Linux kernel is growing in complexity and trying to keep it readable for newcomers is very important IMHO.

The maintainers have the last word on this, but for now I remove my question about why use -1 instead of SIZE_MAX.

Apologies for that, the silver lining was that at least for me it was interesting to explore this area of the code and its inclusions. I hope it was for you as well.

Thank you very much,
Luis

PD: just a small comment, in case you end up sending a new version of your patches. In the following documentation of your functions.
* @len: Maximum string length. May be SIZE_MAX (-1) to set no limit.

What do you think about "SIZE_MAX or -1" instead of "SIZE_MAX (-1)" to avoid confusing any readers who might think SIZE_MAX is a function? As I think you intend to mean your code accepts both -1 or SIZE_MAX, which is ~0.

Just an idea, feel free to ignore it :)
_______________________________________________
Nouveau mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/nouveau

#8 lore raw
Quoted 8 lines · Luis de Bethencourt

Hi Markus,

Thank you very much for the thorough testing and solution searching.

Earlier today I sent a patch that removes dynamic_debug.h from include/linux/kernel.h, since it isn't really needed. Sorry about this since it changes what you were testing just a few hours later.

I did see at least some of the changes you posted. I was mostly curious to see where the problem was coming from in the first place.

Quoted 5 lines

I am starting to think that getting access to SIZE_MAX isn't worth the trouble, specially considering that moving an include out of the top of the file makes the code less readable. The Linux kernel is growing in complexity and trying to keep it readable for newcomers is very important IMHO.

I've been thinking the same thing. It seems to be turning into a huge effort to make this simple constant available.

Quoted 6 lines

The maintainers have the last word on this, but for now I remove my question about why use -1 instead of SIZE_MAX.

Apologies for that, the silver lining was that at least for me it was interesting to explore this area of the code and its inclusions. I hope it was for you as well.

No need to apologize. It seemed like a no-brainer to use SIZE_MAX. :-) And I am sure all the digging will come handy in some way.

Quoted 10 lines

PD: just a small comment, in case you end up sending a new version of your patches. In the following documentation of your functions.
* @len: Maximum string length. May be SIZE_MAX (-1) to set no limit.

What do you think about "SIZE_MAX or -1" instead of "SIZE_MAX (-1)" to avoid confusing any readers who might think SIZE_MAX is a function? As I think you intend to mean your code accepts both -1 or SIZE_MAX, which is ~0.

Just an idea, feel free to ignore it :)

I think I might use ~(size_t)0 directly instead of -1 (or SIZE_MAX).

Thanks,
-Markus
_______________________________________________
Nouveau mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/nouveau

#23 lore raw
In reply to Markus Mayer
Quoted 50 lines · Markus Mayer
Quoted 28 lines · Markus Mayer
Quoted 27 lines · Chris Metcalf
Quoted 24 lines · Markus Mayer

This series introduces a family of generic string case conversion functions. This kind of functionality is needed in several places in the kernel. Right now, everybody seems to be implementing their own copy of this functionality.

Based on the discussion of the previous version of this series[1] and the use cases found in the kernel, it does look like having several flavours of case conversion functions is beneficial. The use cases fall into three categories:
- copying a string and converting the case while specifying a maximum length to mimic strlcpy()
- copying a string and converting the case without specifying a length to mimic strcpy()
- converting the case of a string in-place (i.e. modifying the string that was passed in)

Consequently, I am proposing these new functions:

     void strlcpytoupper(char *dst, const char *src, size_t len);
     void strlcpytolower(char *dst, const char *src, size_t len);
     void strcpytoupper(char *dst, const char *src);
     void strcpytolower(char *dst, const char *src);
     void strtoupper(char *s);
     void strtolower(char *s);
Quoted 5 lines
Quoted 3 lines

You may want to read the article here:

https://lwn.net/Articles/659214/

I'll read that. Thanks.

It doesn't look like there is going to be the danger of "mass changes". So far, I have two ACKs (one where the semantics doesn't change, because it's using strtolower()) and the other in a driver in staging.

But I understand the concern and will keep an eye out if there are other ACKs.

Quoted 7 lines
Quoted 4 lines

and follow up some of the discussion threads on LKML about the best semantics to advertise for the strlcpy/strscpy variants. It might be helpful to return some kind of overflow/truncation error from your copy functions so people can error-check the result.

I am inclined to agree. However, everybody has been telling me that these functions should be void. Originally they weren't.

Quoted 6 lines

What about something like this? It might also work to keep the four static inline functions as "void" (since they won't ever return E2BIG anyway) and just have strlcpyto* return an integer (since that's where the buffer could be too small).

Rasmus, what's your take?

Ping. Any thoughts on this proposal? Does it make sense for me to sent out a new revision of the patch set incorporating these changes -- at least for the strlcpyto* functions?

Thanks,
-Markus

Quoted 111 lines
include/linux/string.h +18 -10
index ae82d13..6cc85dc 100644
@@ -116,8 +116,8 @@ extern void * memchr(const void *,int,__kernel_size_t);
 #endif
 void *memchr_inv(const void *s, int c, size_t n);
 char *strreplace(char *s, char old, char new);
-extern void strlcpytoupper(char *dst, const char *src, size_t len);
-extern void strlcpytolower(char *dst, const char *src, size_t len);
+extern int strlcpytoupper(char *dst, const char *src, size_t len);
+extern int strlcpytolower(char *dst, const char *src, size_t len);
 
 extern void kfree_const(const void *x);
 
@@ -175,38 +175,46 @@ static inline const char *kbasename(const char *path)
  * strcpytoupper - Copy string and convert to uppercase.
  * @dst: The buffer to store the result.
  * @src: The string to convert to uppercase.
+ *
+ * Returns the number of characters copied.
  */
-static inline void strcpytoupper(char *dst, const char *src)
+static inline int strcpytoupper(char *dst, const char *src)
 {
-	strlcpytoupper(dst, src, ~(size_t)0);
+	return strlcpytoupper(dst, src, ~(size_t)0);
 }
 
 /**
  * strcpytolower - Copy string and convert to lowercase.
  * @dst: The buffer to store the result.
  * @src: The string to convert to lowercase.
+ *
+ * Returns the number of characters copied.
  */
-static inline void strcpytolower(char *dst, const char *src)
+static inline int strcpytolower(char *dst, const char *src)
 {
-	strlcpytolower(dst, src, ~(size_t)0);
+	return strlcpytolower(dst, src, ~(size_t)0);
 }
 
 /**
  * strtoupper - Convert string to uppercase.
  * @s: The string to operate on.
+ *
+ * Returns the number of characters copied.
  */
-static inline void strtoupper(char *s)
+static inline int strtoupper(char *s)
 {
-	strlcpytoupper(s, s, ~(size_t)0);
+	return strlcpytoupper(s, s, ~(size_t)0);
 }
 
 /**
  * strtolower - Convert string to lowercase.
  * @s: The string to operate on.
+ *
+ * Returns the number of characters copied.
  */
-static inline void strtolower(char *s)
+static inline int strtolower(char *s)
 {
-	strlcpytolower(s, s, ~(size_t)0);
+	return strlcpytolower(s, s, ~(size_t)0);
 }
 
 #endif /* _LINUX_STRING_H_ */
lib/string.c +9 -3
index 7903e10..d36d5fb2 100644
@@ -958,17 +958,21 @@ EXPORT_SYMBOL(strreplace);
  * @dst: The buffer to store the result.
  * @src: The string to convert to uppercase.
  * @len: Maximum string length. May be SIZE_MAX to set no limit.
+ *
+ * Returns the number of characters copied or -E2BIG if @dst wasn't big enough.
  */
-void strlcpytoupper(char *dst, const char *src, size_t len)
+int strlcpytoupper(char *dst, const char *src, size_t len)
 {
 	size_t i;
 
 	if (!len)
-		return;
+		return -E2BIG;
 
 	for (i = 0; i < len && src[i]; ++i)
 		dst[i] = toupper(src[i]);
 	dst[i < len ? i : i - 1] = '\0';
+
+	return (i < len) ? i : -E2BIG;
 }
 EXPORT_SYMBOL(strlcpytoupper);
 
@@ -977,16 +981,20 @@ EXPORT_SYMBOL(strlcpytoupper);
  * @dst: The buffer to store the result.
  * @src: The string to convert to lowercase.
  * @len: Maximum string length. May be SIZE_MAX to set no limit.
+ *
+ * Returns the number of characters copied or -E2BIG if @dst wasn't big enough.
  */
-void strlcpytolower(char *dst, const char *src, size_t len)
+int strlcpytolower(char *dst, const char *src, size_t len)
 {
 	size_t i;
 
 	if (!len)

lib/string.c EXPORT_SYMBOL(strlcpytoupper); · line 1000

Quoted 10 lines
lib/string.c +3 -1
-		return;
+		return -E2BIG;
 
 	for (i = 0; i < len && src[i]; ++i)
 		dst[i] = tolower(src[i]);
 	dst[i < len ? i : i - 1] = '\0';
+
+	return (i < len) ? i : -E2BIG;
 }
 EXPORT_SYMBOL(strlcpytolower);

_______________________________________________
Nouveau mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/nouveau

#15 lore raw

Re: [PATCH v3 5/7] iscsi-target: replace iscsi_initiatorname_tolower() with strtolower()

In reply to Markus Mayer
Quoted 35 lines · Markus Mayer

After introducing generic strtolower(), iscsi_initiatorname_tolower() is no longer needed.

---
 drivers/target/iscsi/iscsi_target_nego.c | 17 +----------------
 1 file changed, 1 insertion(+), 16 deletions(-)
drivers/target/iscsi/iscsi_target_nego.c +0 -15
index 89d34bd..fa20638 100644
@@ -987,21 +987,6 @@ static int iscsi_target_do_login(struct iscsi_conn *conn, struct iscsi_login *lo
 	return 0;
 }
 
-static void iscsi_initiatorname_tolower(
-	char *param_buf)
-{
-	char *c;
-	u32 iqn_size = strlen(param_buf), i;
-
-	for (i = 0; i < iqn_size; i++) {
-		c = &param_buf[i];
-		if (!isupper(*c))
-			continue;
-
-		*c = tolower(*c);
-	}
-}
-
 /*
  * Processes the first Login Request..
  */

drivers/target/iscsi/iscsi_target_nego.c int iscsi_target_locate_portal( · line 1066

Quoted 8 lines
drivers/target/iscsi/iscsi_target_nego.c +1 -1
@@ -1075,7 +1060,7 @@ int iscsi_target_locate_portal(
 	 * RFC-3720 3.2.6.1. section c) that says that iSCSI IQNs
 	 * are NOT case sensitive.
 	 */
-	iscsi_initiatorname_tolower(i_buf);
+	strtolower(i_buf);
 
 	if (!s_buf) {
 		if (!login->leading_connection)

Applied to target-pending/for-next.

Thanks Markus.

#16 lore raw

Re: [PATCH v3 5/7] iscsi-target: replace iscsi_initiatorname_tolower() with strtolower()

Quoted 42 lines · Nicholas A. Bellinger
Quoted 41 lines · Markus Mayer

After introducing generic strtolower(), iscsi_initiatorname_tolower() is no longer needed.

---
 drivers/target/iscsi/iscsi_target_nego.c | 17 +----------------
 1 file changed, 1 insertion(+), 16 deletions(-)
drivers/target/iscsi/iscsi_target_nego.c +1 -16
index 89d34bd..fa20638 100644
@@ -987,21 +987,6 @@ static int iscsi_target_do_login(struct iscsi_conn *conn, struct iscsi_login *lo
 	return 0;
 }
 
-static void iscsi_initiatorname_tolower(
-	char *param_buf)
-{
-	char *c;
-	u32 iqn_size = strlen(param_buf), i;
-
-	for (i = 0; i < iqn_size; i++) {
-		c = &param_buf[i];
-		if (!isupper(*c))
-			continue;
-
-		*c = tolower(*c);
-	}
-}
-
 /*
  * Processes the first Login Request..
  */
@@ -1075,7 +1060,7 @@ int iscsi_target_locate_portal(
 	 * RFC-3720 3.2.6.1. section c) that says that iSCSI IQNs
 	 * are NOT case sensitive.
 	 */
-	iscsi_initiatorname_tolower(i_buf);
+	strtolower(i_buf);
 
Quoted 7 lines

drivers/target/iscsi/iscsi_target_nego.c int iscsi_target_locate_portal( · line 1066

Quoted 2 lines
drivers/target/iscsi/iscsi_target_nego.c +0 -0
 	if (!s_buf) {
 		if (!login->leading_connection)

Applied to target-pending/for-next.

Thanks Markus.

Er, my bad. It's not a stand-alone patch..

In that case for the iscsi-target part:

#17 lore raw

Re: [PATCH v3 5/7] iscsi-target: replace iscsi_initiatorname_tolower() with strtolower()

Quoted 48 lines · Nicholas A. Bellinger
Quoted 47 lines · Nicholas A. Bellinger
Quoted 35 lines · Markus Mayer

After introducing generic strtolower(), iscsi_initiatorname_tolower() is no longer needed.

---
 drivers/target/iscsi/iscsi_target_nego.c | 17 +----------------
 1 file changed, 1 insertion(+), 16 deletions(-)
drivers/target/iscsi/iscsi_target_nego.c +0 -15
index 89d34bd..fa20638 100644
@@ -987,21 +987,6 @@ static int iscsi_target_do_login(struct iscsi_conn *conn, struct iscsi_login *lo
    return 0;
 }
 
-static void iscsi_initiatorname_tolower(
-   char *param_buf)
-{
-   char *c;
-   u32 iqn_size = strlen(param_buf), i;
-
-   for (i = 0; i < iqn_size; i++) {
-           c = &param_buf[i];
-           if (!isupper(*c))
-                   continue;
-
-           *c = tolower(*c);
-   }
-}
-
 /*
  * Processes the first Login Request..
  */

drivers/target/iscsi/iscsi_target_nego.c int iscsi_target_locate_portal( · line 1066

Quoted 8 lines
drivers/target/iscsi/iscsi_target_nego.c +1 -1
@@ -1075,7 +1060,7 @@ int iscsi_target_locate_portal(
     * RFC-3720 3.2.6.1. section c) that says that iSCSI IQNs
     * are NOT case sensitive.
     */
-   iscsi_initiatorname_tolower(i_buf);
+   strtolower(i_buf);
 
    if (!s_buf) {
            if (!login->leading_connection)

Applied to target-pending/for-next.

Quoted 4 lines
Quoted 2 lines

Thanks Markus.

Er, my bad. It's not a stand-alone patch..

No, it's not. I was just about to point that out. Patch 1 is taking a bit of time to get straightened out, so it hasn't been applied yet.

Quoted 3 lines

In that case for the iscsi-target part:

Thanks. I'll add the ACK to my tree.

-Markus