Skip to content

Commit a3e7444

Browse files
committed
Only compute char len when necessary
Same reasoning as for the validity check.
1 parent 7c80085 commit a3e7444

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

ext/mysqlnd/mysqlnd_charset.c

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -838,27 +838,29 @@ PHPAPI zend_ulong mysqlnd_cset_escape_slashes(const MYSQLND_CHARSET * const cset
838838

839839
for (;escapestr < end; escapestr++) {
840840
char esc = '\0';
841-
unsigned int len = 0;
842841

843842
/* check unicode characters
844843
* Encodings that have a minimum length of 1 are compatible with ASCII.
845844
* So we can skip (for performance reasons) the check to mb_valid for them. */
846-
if (cset->char_maxlen > 1 && (*((zend_uchar *) escapestr) > 0x80 || cset->char_minlen > 1) && (len = cset->mb_valid(escapestr, end))) {
847-
/* check possible overflow */
848-
if ((newstr + len) > newstr_e) {
849-
escape_overflow = TRUE;
850-
break;
851-
}
852-
/* copy mb char without escaping it */
853-
while (len--) {
854-
*newstr++ = *escapestr++;
845+
if (cset->char_maxlen > 1 && (*((zend_uchar *) escapestr) > 0x80 || cset->char_minlen > 1)) {
846+
unsigned int len = cset->mb_valid(escapestr, end);
847+
if (len) {
848+
/* check possible overflow */
849+
if ((newstr + len) > newstr_e) {
850+
escape_overflow = TRUE;
851+
break;
852+
}
853+
/* copy mb char without escaping it */
854+
while (len--) {
855+
*newstr++ = *escapestr++;
856+
}
857+
escapestr--;
858+
continue;
859+
} else if (cset->mb_charlen(*escapestr) > 1) {
860+
esc = *escapestr;
855861
}
856-
escapestr--;
857-
continue;
858862
}
859-
if (cset->char_maxlen > 1 && cset->mb_charlen(*escapestr) > 1) {
860-
esc = *escapestr;
861-
} else {
863+
if (!esc) {
862864
switch (*escapestr) {
863865
case 0:
864866
esc = '0';

0 commit comments

Comments
 (0)