Skip to contentSkip to conversation

shared/util: Introduce strnlenutf8

PATCH Applied Luiz Augusto von Dentz 3 participants 3 messages Jul 9, 2025 Bluetooth drivers

lore
Share & tools
lore

Luiz Augusto von Dentz

author maintainer
#0 lore raw

PATCH v2 1/1 shared/util: Introduce strnlenutf8

From: Luiz Augusto von Dentz <[email protected]>

This introduces strnlenutf8 which works similarly to strnlen but return only the number of valid bytes of UTF-8 encoded string then replace the other copies of similar code.

---
 src/shared/util.c | 82 +++++++++++++++++------------------------------
 src/shared/util.h |  2 ++
 2 files changed, 32 insertions(+), 52 deletions(-)
src/shared/util.c +30 -52
index 4780f26b6d59..fa058170e124 100644
@@ -1909,58 +1909,8 @@ char *strstrip(char *str)
 	return str;
 }
 
-bool strisutf8(const char *str, size_t len)
-{
-	size_t i = 0;
+size_t strnlenutf8(const char *str, size_t len)
 
-	while (i < len) {
-		unsigned char c = str[i];
-		size_t size = 0;
-
-		/* Check the first byte to determine the number of bytes in the
-		 * UTF-8 character.
-		 */
-		if ((c & 0x80) == 0x00)
-			size = 1;
-		else if ((c & 0xE0) == 0xC0)
-			size = 2;
-		else if ((c & 0xF0) == 0xE0)
-			size = 3;
-		else if ((c & 0xF8) == 0xF0)
-			size = 4;
-		else
-			/* Invalid UTF-8 sequence */
-			return false;
-
-		/* Check the following bytes to ensure they have the correct
-		 * format.
-		 */
-		for (size_t j = 1; j < size; ++j) {
-			if (i + j > len || (str[i + j] & 0xC0) != 0x80)
-				/* Invalid UTF-8 sequence */
-				return false;
-		}
-
-		/* Move to the next character */
-		i += size;
-	}
-
-	return true;
-}
-
-bool argsisutf8(int argc, char *argv[])
-{
-	for (int i = 0; i < argc; i++) {
-		if (!strisutf8(argv[i], strlen(argv[i]))) {
-			printf("Invalid character in string: %s\n", argv[i]);
-			return false;
-		}
-	}
-
-	return true;
-}
-
-char *strtoutf8(char *str, size_t len)
 {
 	size_t i = 0;
 
@@ -1987,7 +1937,7 @@ char *strtoutf8(char *str, size_t len)
 		 * format.
 		 */
 		for (size_t j = 1; j < size; ++j) {
-			if (i + j > len || (str[i + j] & 0xC0) != 0x80)
+			if (i + j >= len || (str[i + j] & 0xC0) != 0x80)
 				/* Invalid UTF-8 sequence */
 				goto done;
 		}
@@ -1997,6 +1947,34 @@ char *strtoutf8(char *str, size_t len)
 	}
 
 done:
+	return i;
+}
+
+bool strisutf8(const char *str, size_t len)
+{
+	return strnlenutf8(str, len) == len;
+}
+
+bool argsisutf8(int argc, char *argv[])
+{
+	for (int i = 0; i < argc; i++) {
+		if (!strisutf8(argv[i], strlen(argv[i]))) {
+			printf("Invalid character in string: %s\n", argv[i]);
+			return false;
+		}
+	}
+
+	return true;
+}
+
+char *strtoutf8(char *str, size_t len)
+{
+	size_t i = 0;
+
+	i = strnlenutf8(str, len);
+	if (i == len)
+		return str;
+
 	/* Truncate to the longest valid UTF-8 string */
 	memset(str + i, 0, len - i);
 	return str;
src/shared/util.h +2 -0
index 6fc02a9dcb5a..c480351d6e9f 100644
@@ -90,6 +90,8 @@ do {						\
 char *strdelimit(char *str, char *del, char c);
 int strsuffix(const char *str, const char *suffix);
 char *strstrip(char *str);
+
+size_t strnlenutf8(const char *str, size_t len);
 bool strisutf8(const char *str, size_t length);
 bool argsisutf8(int argc, char *argv[]);
 char *strtoutf8(char *str, size_t len);
Signature · 1 line
2.50.0
#1 lore raw

This is automated email and please do not reply to this email!

Dear submitter,

Thank you for submitting the patches to the linux bluetooth mailing list. This is a CI test results with your patch series:
PW Link:https://patchwork.kernel.org/project/bluetooth/list/?series=980525

---Test result---

Test Summary:

CheckPatch                    PENDING   0.26 seconds
GitLint                       PENDING   0.33 seconds
BuildEll                      PASS      20.10 seconds
BluezMake                     PASS      2673.54 seconds
MakeCheck                     PASS      19.93 seconds
MakeDistcheck                 PASS      183.65 seconds
CheckValgrind                 PASS      235.91 seconds
CheckSmatch                   PASS      304.57 seconds
bluezmakeextell               PASS      127.47 seconds
IncrementalBuild              PENDING   0.32 seconds
ScanBuild                     PASS      918.66 seconds

Details
##############################
Test: CheckPatch - PENDING
Desc: Run checkpatch.pl script
Output:

##############################
Test: GitLint - PENDING
Desc: Run gitlint
Output:

##############################
Test: IncrementalBuild - PENDING
Desc: Incremental build with the patches in the series Output:

---
Regards,
Linux Bluetooth

#2 lore raw
In reply to Luiz Augusto von Dentz
This patch was applied to bluetooth/bluez.git (master) by Luiz Augusto von Dentz <[email protected]>:

Hello:

This patch was applied to bluetooth/bluez.git (master) by Luiz Augusto von Dentz <[email protected]>:

Quoted 9 lines · On Wed, 9 Jul 2025 09:14:53 -0400 you wrote:

From: Luiz Augusto von Dentz <[email protected]>

This introduces strnlenutf8 which works similarly to strnlen but return only the number of valid bytes of UTF-8 encoded string then replace the other copies of similar code.

---
 src/shared/util.c | 82 +++++++++++++++++------------------------------
 src/shared/util.h |  2 ++
 2 files changed, 32 insertions(+), 52 deletions(-)

Here is the summary with links:
- [BlueZ,v2] shared/util: Introduce strnlenutf8

    https://git.kernel.org/pub/scm/bluetooth/bluez.git/?id=24a327427d7e

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html