Skip to content

Commit d67df60

Browse files
authored
intl: use uspoof_check2UTF8 call when available. (#9478)
1 parent 163c0b5 commit d67df60

File tree

4 files changed

+24
-2
lines changed

4 files changed

+24
-2
lines changed

ext/intl/spoofchecker/spoofchecker_class.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,13 @@ void spoofchecker_object_destroy(Spoofchecker_object* co)
129129
co->uspoof = NULL;
130130
}
131131

132+
#if U_ICU_VERSION_MAJOR_NUM >= 58
133+
if (co->uspoofres) {
134+
uspoof_closeCheckResult(co->uspoofres);
135+
co->uspoofres = NULL;
136+
}
137+
#endif
138+
132139
intl_error_reset(SPOOFCHECKER_ERROR_P(co));
133140
}
134141
/* }}} */

ext/intl/spoofchecker/spoofchecker_class.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ typedef struct {
2828
intl_error err;
2929

3030
// ICU Spoofchecker
31-
USpoofChecker* uspoof;
31+
USpoofChecker* uspoof;
32+
#if U_ICU_VERSION_MAJOR_NUM >= 58
33+
USpoofCheckResult* uspoofres;
34+
#endif
3235

3336
zend_object zo;
3437
} Spoofchecker_object;

ext/intl/spoofchecker/spoofchecker_create.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ PHP_METHOD(Spoofchecker, __construct)
4949
uspoof_check2 APIs when it became stable, to use extended check result APIs.
5050
Subsequent changes in the unicode security algos are to be watched.*/
5151
uspoof_setRestrictionLevel(co->uspoof, SPOOFCHECKER_DEFAULT_RESTRICTION_LEVEL);
52+
co->uspoofres = uspoof_openCheckResult(SPOOFCHECKER_ERROR_CODE_P(co));
5253
#else
5354
/* Single-script enforcement is on by default. This fails for languages
5455
like Japanese that legally use multiple scripts within a single word,

ext/intl/spoofchecker/spoofchecker_main.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
/* {{{ Checks if a given text contains any suspicious characters */
2323
PHP_METHOD(Spoofchecker, isSuspicious)
2424
{
25-
int ret;
25+
int32_t ret, errmask;
2626
char *text;
2727
size_t text_len;
2828
zval *error_code = NULL;
@@ -34,10 +34,21 @@ PHP_METHOD(Spoofchecker, isSuspicious)
3434

3535
SPOOFCHECKER_METHOD_FETCH_OBJECT;
3636

37+
#if U_ICU_VERSION_MAJOR_NUM >= 58
38+
ret = uspoof_check2UTF8(co->uspoof, text, text_len, co->uspoofres, SPOOFCHECKER_ERROR_CODE_P(co));
39+
#else
3740
ret = uspoof_checkUTF8(co->uspoof, text, text_len, NULL, SPOOFCHECKER_ERROR_CODE_P(co));
41+
#endif
3842

3943
if (U_FAILURE(SPOOFCHECKER_ERROR_CODE(co))) {
4044
php_error_docref(NULL, E_WARNING, "(%d) %s", SPOOFCHECKER_ERROR_CODE(co), u_errorName(SPOOFCHECKER_ERROR_CODE(co)));
45+
#if U_ICU_VERSION_MAJOR_NUM >= 58
46+
errmask = uspoof_getCheckResultChecks(co->uspoofres, SPOOFCHECKER_ERROR_CODE_P(co));
47+
48+
if (errmask != ret) {
49+
php_error_docref(NULL, E_WARNING, "unexpected error (%d), does not relate to the flags passed to setChecks (%d)", ret, errmask);
50+
}
51+
#endif
4152
RETURN_TRUE;
4253
}
4354

0 commit comments

Comments
 (0)