aboutsummaryrefslogtreecommitdiff
path: root/src/utf.c
diff options
context:
space:
mode:
authordrh <>2024-09-19 13:39:06 +0000
committerdrh <>2024-09-19 13:39:06 +0000
commitf8305e46169d531fce2f778b1de99b59b7cd2318 (patch)
tree7ab84a4889f8d15a45089fdaf8480956c8385d35 /src/utf.c
parent8513eb6ba84b11a645e1a60184cd649a2039c9e2 (diff)
downloadsqlite-f8305e46169d531fce2f778b1de99b59b7cd2318.tar.gz
sqlite-f8305e46169d531fce2f778b1de99b59b7cd2318.zip
Improved rebustness to malformed UTF-16 inputs to sqlite3_prepare16_v2().
FossilOrigin-Name: 7b3a517b3e16ea487ca77a2c88a0c11d737de366524fc911aa1bdd6bfb7ad148
Diffstat (limited to 'src/utf.c')
-rw-r--r--src/utf.c12
1 files changed, 7 insertions, 5 deletions
diff --git a/src/utf.c b/src/utf.c
index 216864f5c..083ada788 100644
--- a/src/utf.c
+++ b/src/utf.c
@@ -514,20 +514,22 @@ char *sqlite3Utf16to8(sqlite3 *db, const void *z, int nByte, u8 enc){
}
/*
-** zIn is a UTF-16 encoded unicode string at least nChar characters long.
+** zIn is a UTF-16 encoded unicode string at least nByte bytes long.
** Return the number of bytes in the first nChar unicode characters
-** in pZ. nChar must be non-negative.
+** in pZ. nChar must be non-negative. Surrogate pairs count as a single
+** character.
*/
-int sqlite3Utf16ByteLen(const void *zIn, int nChar){
+int sqlite3Utf16ByteLen(const void *zIn, int nByte, int nChar){
int c;
unsigned char const *z = zIn;
+ unsigned char const *zEnd = &z[nByte-1];
int n = 0;
if( SQLITE_UTF16NATIVE==SQLITE_UTF16LE ) z++;
- while( n<nChar ){
+ while( n<nChar && ALWAYS(z<=zEnd) ){
c = z[0];
z += 2;
- if( c>=0xd8 && c<0xdc && z[0]>=0xdc && z[0]<0xe0 ) z += 2;
+ if( c>=0xd8 && c<0xdc && z<=zEnd && z[0]>=0xdc && z[0]<0xe0 ) z += 2;
n++;
}
return (int)(z-(unsigned char const *)zIn)