-
Notifications
You must be signed in to change notification settings - Fork 7.9k
UTF-8 validate strings before interning #10870
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -180,6 +180,10 @@ static zend_always_inline zend_string *zend_add_interned_string(zend_string *str | |||||||||||
GC_SET_REFCOUNT(str, 1); | ||||||||||||
GC_ADD_FLAGS(str, IS_STR_INTERNED | flags); | ||||||||||||
|
||||||||||||
if (!ZSTR_IS_VALID_UTF8(str) && zend_string_validate_utf8(str)) { | ||||||||||||
GC_ADD_FLAGS(str, IS_STR_VALID_UTF8); | ||||||||||||
} | ||||||||||||
|
||||||||||||
ZVAL_INTERNED_STR(&val, str); | ||||||||||||
|
||||||||||||
zend_hash_add_new(interned_strings, str, &val); | ||||||||||||
|
@@ -493,3 +497,45 @@ ZEND_API zend_string *zend_string_concat3( | |||||||||||
|
||||||||||||
return res; | ||||||||||||
} | ||||||||||||
|
||||||||||||
// Copyright (c) 2008-2009 Bjoern Hoehrmann <[email protected]> | ||||||||||||
// See http://bjoern.hoehrmann.de/utf-8/decoder/dfa/ for details. | ||||||||||||
// https://stackoverflow.com/a/22135005/1320374 | ||||||||||||
|
||||||||||||
enum { | ||||||||||||
UTF8_ACCEPT = 0, | ||||||||||||
UTF8_REJECT = 1, | ||||||||||||
}; | ||||||||||||
|
||||||||||||
static const uint8_t utf8d[] = { | ||||||||||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 00..1f | ||||||||||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 20..3f | ||||||||||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 40..5f | ||||||||||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 60..7f | ||||||||||||
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9, // 80..9f | ||||||||||||
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, // a0..bf | ||||||||||||
8,8,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, // c0..df | ||||||||||||
0xa,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x4,0x3,0x3, // e0..ef | ||||||||||||
0xb,0x6,0x6,0x6,0x5,0x8,0x8,0x8,0x8,0x8,0x8,0x8,0x8,0x8,0x8,0x8, // f0..ff | ||||||||||||
0x0,0x1,0x2,0x3,0x5,0x8,0x7,0x1,0x1,0x1,0x4,0x6,0x1,0x1,0x1,0x1, // s0..s0 | ||||||||||||
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,0,1,0,1,1,1,1,1,1, // s1..s2 | ||||||||||||
1,2,1,1,1,1,1,2,1,2,1,1,1,1,1,1,1,1,1,1,1,1,1,2,1,1,1,1,1,1,1,1, // s3..s4 | ||||||||||||
1,2,1,1,1,1,1,1,1,2,1,1,1,1,1,1,1,1,1,1,1,1,1,3,1,3,1,1,1,1,1,1, // s5..s6 | ||||||||||||
1,3,1,1,1,1,1,3,1,3,1,1,1,1,1,1,1,3,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // s7..s8 | ||||||||||||
}; | ||||||||||||
|
||||||||||||
ZEND_API bool zend_string_validate_utf8(zend_string *string) { | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. https://github.com/alexdowad/php-src/blob/d882511808d973a354f63bb5821f552d46c09d8e/ext/mbstring/mbstring.c#L4621 should be used (and moved to non-mbstring/core if needed) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doesn't seem to modify the pointer
Suggested change
|
||||||||||||
char *str = ZSTR_VAL(string); | ||||||||||||
size_t len = ZSTR_LEN(string); | ||||||||||||
uint32_t state = UTF8_ACCEPT; | ||||||||||||
|
||||||||||||
for (size_t i = 0; i < len; i++) { | ||||||||||||
uint32_t type = utf8d[(uint8_t)str[i]]; | ||||||||||||
state = utf8d[256 + state * 16 + type]; | ||||||||||||
|
||||||||||||
if (state == UTF8_REJECT) | ||||||||||||
break; | ||||||||||||
Comment on lines
+536
to
+537
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. CS Nit:
Suggested change
|
||||||||||||
} | ||||||||||||
|
||||||||||||
return state == UTF8_ACCEPT; | ||||||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is under BSD licence so it should be pointed here as well or ideally new header created so it doesn't mix licenses in the single file. I have been actually using this in jsond for some time and I separated the headers for that.