Skip to content
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

Improve MySQLnd quote escaping performance #13466

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 25 additions & 46 deletions ext/mysqlnd/mysqlnd_charset.c
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,12 @@ PHPAPI const MYSQLND_CHARSET * mysqlnd_find_charset_name(const char * const name
}
/* }}} */

static zend_always_inline bool mysqlnd_mb_validity_prerequisites_check(const MYSQLND_CHARSET * const cset, const zend_uchar *str)
{
/* Encodings that have a minimum length of 1 are compatible with ASCII.
* So we can skip (for performance reasons) the check to mb_valid for them. */
return cset->char_maxlen > 1 && (*str > 0x80 || cset->char_minlen > 1);
}

/* {{{ mysqlnd_cset_escape_quotes */
PHPAPI zend_ulong mysqlnd_cset_escape_quotes(const MYSQLND_CHARSET * const cset, char * newstr,
Expand All @@ -777,21 +783,15 @@ PHPAPI zend_ulong mysqlnd_cset_escape_quotes(const MYSQLND_CHARSET * const cset,
const char *newstr_s = newstr;
const char *newstr_e = newstr + 2 * escapestr_len;
const char *end = escapestr + escapestr_len;
bool escape_overflow = FALSE;

DBG_ENTER("mysqlnd_cset_escape_quotes");

for (;escapestr < end; escapestr++) {
unsigned int len = 0;
/* check unicode characters */

if (cset->char_maxlen > 1 && (len = cset->mb_valid(escapestr, end))) {

/* check possible overflow */
if ((newstr + len) > newstr_e) {
escape_overflow = TRUE;
break;
}
if (mysqlnd_mb_validity_prerequisites_check(cset, (const zend_uchar *) escapestr) && (len = cset->mb_valid(escapestr, end))) {
ZEND_ASSERT(newstr + len <= newstr_e);
/* copy mb char without escaping it */
while (len--) {
*newstr++ = *escapestr++;
Expand All @@ -800,25 +800,16 @@ PHPAPI zend_ulong mysqlnd_cset_escape_quotes(const MYSQLND_CHARSET * const cset,
continue;
}
if (*escapestr == '\'') {
if (newstr + 2 > newstr_e) {
escape_overflow = TRUE;
break;
}
ZEND_ASSERT(newstr + 2 <= newstr_e);
*newstr++ = '\'';
*newstr++ = '\'';
} else {
if (newstr + 1 > newstr_e) {
escape_overflow = TRUE;
break;
}
ZEND_ASSERT(newstr + 1 <= newstr_e);
*newstr++ = *escapestr;
}
}
*newstr = '\0';

if (escape_overflow) {
DBG_RETURN((zend_ulong)~0);
}
DBG_RETURN((zend_ulong)(newstr - newstr_s));
}
/* }}} */
Expand All @@ -831,32 +822,29 @@ PHPAPI zend_ulong mysqlnd_cset_escape_slashes(const MYSQLND_CHARSET * const cset
const char *newstr_s = newstr;
const char *newstr_e = newstr + 2 * escapestr_len;
const char *end = escapestr + escapestr_len;
bool escape_overflow = FALSE;

DBG_ENTER("mysqlnd_cset_escape_slashes");
DBG_INF_FMT("charset=%s", cset->name);

for (;escapestr < end; escapestr++) {
char esc = '\0';
unsigned int len = 0;

/* check unicode characters */
if (cset->char_maxlen > 1 && (len = cset->mb_valid(escapestr, end))) {
/* check possible overflow */
if ((newstr + len) > newstr_e) {
escape_overflow = TRUE;
break;
}
/* copy mb char without escaping it */
while (len--) {
*newstr++ = *escapestr++;
if (mysqlnd_mb_validity_prerequisites_check(cset, (const zend_uchar *) escapestr)) {
unsigned int len = cset->mb_valid(escapestr, end);
if (len) {
ZEND_ASSERT(newstr + len <= newstr_e);
/* copy mb char without escaping it */
while (len--) {
*newstr++ = *escapestr++;
}
escapestr--;
continue;
} else if (cset->mb_charlen(*escapestr) > 1) {
esc = *escapestr;
}
escapestr--;
continue;
}
if (cset->char_maxlen > 1 && cset->mb_charlen(*escapestr) > 1) {
esc = *escapestr;
} else {
if (!esc) {
switch (*escapestr) {
case 0:
esc = '0';
Expand All @@ -878,27 +866,18 @@ PHPAPI zend_ulong mysqlnd_cset_escape_slashes(const MYSQLND_CHARSET * const cset
}
}
if (esc) {
if (newstr + 2 > newstr_e) {
escape_overflow = TRUE;
break;
}
ZEND_ASSERT(newstr + 2 <= newstr_e);
/* copy escaped character */
*newstr++ = '\\';
*newstr++ = esc;
} else {
if (newstr + 1 > newstr_e) {
escape_overflow = TRUE;
break;
}
ZEND_ASSERT(newstr + 1 <= newstr_e);
/* copy non escaped character */
*newstr++ = *escapestr;
}
}
*newstr = '\0';

if (escape_overflow) {
DBG_RETURN((zend_ulong)~0);
}
DBG_RETURN((zend_ulong)(newstr - newstr_s));
}
/* }}} */
Expand Down
10 changes: 5 additions & 5 deletions ext/pdo_mysql/mysql_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -309,9 +309,7 @@ static zend_string* mysql_handle_quoter(pdo_dbh_t *dbh, const zend_string *unquo
{
pdo_mysql_db_handle *H = (pdo_mysql_db_handle *)dbh->driver_data;
bool use_national_character_set = 0;
char *quoted;
size_t quotedlen;
zend_string *quoted_str;

if (H->assume_national_character_set_strings) {
use_national_character_set = 1;
Expand All @@ -326,7 +324,9 @@ static zend_string* mysql_handle_quoter(pdo_dbh_t *dbh, const zend_string *unquo
PDO_DBG_ENTER("mysql_handle_quoter");
PDO_DBG_INF_FMT("dbh=%p", dbh);
PDO_DBG_INF_FMT("unquoted=%.*s", (int)ZSTR_LEN(unquoted), ZSTR_VAL(unquoted));
quoted = safe_emalloc(2, ZSTR_LEN(unquoted), 3 + (use_national_character_set ? 1 : 0));

zend_string *quoted_str = zend_string_safe_alloc(2, ZSTR_LEN(unquoted), 3 + (use_national_character_set ? 1 : 0), false);
char *quoted = ZSTR_VAL(quoted_str);

if (use_national_character_set) {
quotedlen = mysql_real_escape_string_quote(H->server, quoted + 2, ZSTR_VAL(unquoted), ZSTR_LEN(unquoted), '\'');
Expand All @@ -343,8 +343,8 @@ static zend_string* mysql_handle_quoter(pdo_dbh_t *dbh, const zend_string *unquo
quoted[++quotedlen] = '\0';
PDO_DBG_INF_FMT("quoted=%.*s", (int)quotedlen, quoted);

quoted_str = zend_string_init(quoted, quotedlen, 0);
efree(quoted);
quoted_str = zend_string_truncate(quoted_str, quotedlen, false);

PDO_DBG_RETURN(quoted_str);
}
/* }}} */
Expand Down