shared/util: Refactor code from strisutf8 and strtoutf8
PATCH 1/1 shared/util: Refactor code from strisutf8 and strtoutf8
Move duplicate code to static validateutf8() and fix boundary access on multi-byte character check.
--- src/shared/util.c | 56 +++++++++++++++-------------------------------- 1 file changed, 18 insertions(+), 38 deletions(-)
src/shared/util.c +18 -38
@@ -1909,7 +1909,7 @@ char *strstrip(char *str)return str; } -bool strisutf8 (const char *str,size_t len) +static bool validateutf8 (const char *str,size_t len,size_t *invalid_index) {size_t i =0 ;
@@ -1928,17 +1928,23 @@ bool strisutf8(const char *str, size_t len) size =3 ; else if ((c & 0xF8) == 0xF0) size = 4; - else + else {/* Invalid UTF-8 sequence */ +if (invalid_index) + *invalid_index = i;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 ) +if (i + j >= len || (str[i + j] &0xC0 ) !=0x80 ) {/* Invalid UTF-8 sequence */ +if (invalid_index) + *invalid_index = i;return false; + } }/* Move to the next character */
@@ -1948,6 +1954,11 @@ bool strisutf8(const char *str, size_t len)return true; } +bool strisutf8 (const char *str,size_t len) +{ +return validateutf8 (str, len,NULL ); +} +bool argsisutf8 (int argc,char *argv[]) {for (int i =0 ; i < argc; i++) {
@@ -1962,42 +1973,11 @@ bool argsisutf8(int argc, char *argv[])char *strtoutf8 (char *str,size_t len) { -size_t i =0 ; - -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 */ - goto done; - -/* 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 */ - goto done; - } +size_t invalid_index =0 ; -/* Move to the next character */ - i += size; - } +if (!validateutf8 (str, len, &invalid_index)) +/* Truncate to the longest valid UTF-8 string */ +memset (str + invalid_index,0 , len - invalid_index); -done: -/* Truncate to the longest valid UTF-8 string */ -memset (str + i,0 , len - i);return str; }
Signature · 1 line
2.43.0
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=980429
---Test result---
Test Summary:
CheckPatch PENDING 0.36 seconds GitLint PENDING 0.55 seconds BuildEll PASS 20.07 seconds BluezMake PASS 2575.82 seconds MakeCheck PASS 20.22 seconds MakeDistcheck PASS 182.18 seconds CheckValgrind PASS 233.93 seconds CheckSmatch PASS 302.45 seconds bluezmakeextell PASS 126.73 seconds IncrementalBuild PENDING 0.26 seconds ScanBuild PASS 899.39 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
Luiz Augusto von Dentz
maintainerHi Frédéric,
Quoted 106 lines · Frédéric Danis
Move duplicate code to static
validateutf8()and fix boundary access on multi-byte character check.--- src/shared/util.c | 56 +++++++++++++++-------------------------------- 1 file changed, 18 insertions(+), 38 deletions(-)src/shared/util.c +18
-39@@ -1909,7 +1909,7 @@ char *strstrip(char *str)return str; } -bool strisutf8 (const char *str,size_t len) +static bool validateutf8 (const char *str,size_t len,size_t *invalid_index) {size_t i =0 ;@@ -1928,17 +1928,23 @@ bool strisutf8(const char *str, size_t len) size =3 ; else if ((c & 0xF8) == 0xF0) size = 4; - else + else {/* Invalid UTF-8 sequence */ +if (invalid_index) + *invalid_index = i;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 ) +if (i + j >= len || (str[i + j] &0xC0 ) !=0x80 ) {/* Invalid UTF-8 sequence */ +if (invalid_index) + *invalid_index = i;return false; + } }/* Move to the next character */ @@ -1948,6 +1954,11 @@ bool strisutf8(const char *str, size_t len)return true; } +bool strisutf8 (const char *str,size_t len) +{ +return validateutf8 (str, len,NULL ); +} +bool argsisutf8 (int argc,char *argv[]) {for (int i =0 ; i < argc; i++) {@@ -1962,42 +1973,11 @@ bool argsisutf8(int argc, char *argv[])char *strtoutf8 (char *str,size_t len) { -size_t i =0 ; - -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 */ - goto done; - -/* 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 */ - goto done; - } +size_t invalid_index =0 ; -/* Move to the next character */ - i += size; - } +if (!validateutf8 (str, len, &invalid_index)) +/* Truncate to the longest valid UTF-8 string */ +memset (str + invalid_index,0 , len - invalid_index); -done: -/* Truncate to the longest valid UTF-8 string */ -memset (str + i,0 , len - i);return str; } --
Quoted 2 lines
2.43.0
I did something similar yesterday:
https://patchwork.kernel.org/project/bluetooth/patch/[email protected]/
Let me know if you have any comments.
Signature · 1 line
Luiz Augusto von Dentz
Hi Luiz,
Quoted 113 lines · Luiz Augusto von Dentz
Hi Frédéric,
Quoted 105 lines · Frédéric Danis
Move duplicate code to static
validateutf8()and fix boundary access on multi-byte character check.--- src/shared/util.c | 56 +++++++++++++++-------------------------------- 1 file changed, 18 insertions(+), 38 deletions(-)src/shared/util.c +18
-39@@ -1909,7 +1909,7 @@ char *strstrip(char *str)return str; } -bool strisutf8 (const char *str,size_t len) +static bool validateutf8 (const char *str,size_t len,size_t *invalid_index) {size_t i =0 ;@@ -1928,17 +1928,23 @@ bool strisutf8(const char *str, size_t len) size =3 ; else if ((c & 0xF8) == 0xF0) size = 4; - else + else {/* Invalid UTF-8 sequence */ +if (invalid_index) + *invalid_index = i;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 ) +if (i + j >= len || (str[i + j] &0xC0 ) !=0x80 ) {/* Invalid UTF-8 sequence */ +if (invalid_index) + *invalid_index = i;return false; + } }/* Move to the next character */ @@ -1948,6 +1954,11 @@ bool strisutf8(const char *str, size_t len)return true; } +bool strisutf8 (const char *str,size_t len) +{ +return validateutf8 (str, len,NULL ); +} +bool argsisutf8 (int argc,char *argv[]) {for (int i =0 ; i < argc; i++) {@@ -1962,42 +1973,11 @@ bool argsisutf8(int argc, char *argv[])char *strtoutf8 (char *str,size_t len) { -size_t i =0 ; - -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 */ - goto done; - -/* 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 */ - goto done; - } +size_t invalid_index =0 ; -/* Move to the next character */ - i += size; - } +if (!validateutf8 (str, len, &invalid_index)) +/* Truncate to the longest valid UTF-8 string */ +memset (str + invalid_index,0 , len - invalid_index); -done: -/* Truncate to the longest valid UTF-8 string */ -memset (str + i,0 , len - i);return str; } --Quoted 2 lines
2.43.0
I did something similar yesterday:
Quoted 3 lines
https://patchwork.kernel.org/project/bluetooth/patch/[email protected]/
Let me know if you have any comments.
Sorry, I missed it, you can discard mine
Signature · 6 lines
Frédéric Danis Senior Software Engineer Collabora Ltd. Platinum Building, St John's Innovation Park, Cambridge CB4 0DS, United Kingdom Registered in England & Wales, no. 5513718