Skip to content

Commit dc13582

Browse files
committed
Refactor PDO quoter handler to return a zend_string*
1 parent 62e3875 commit dc13582

File tree

8 files changed

+80
-90
lines changed

8 files changed

+80
-90
lines changed

ext/pdo/pdo_dbh.c

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,18 +1113,17 @@ PHP_METHOD(PDO, query)
11131113
}
11141114
/* }}} */
11151115

1116-
/* {{{ quotes string for use in a query. The optional paramtype acts as a hint for drivers that have alternate quoting styles. The default value is PDO_PARAM_STR */
1116+
/* {{{ quotes string for use in a query.
1117+
* The optional paramtype acts as a hint for drivers that have alternate quoting styles.
1118+
* The default value is PDO_PARAM_STR */
11171119
PHP_METHOD(PDO, quote)
11181120
{
11191121
pdo_dbh_t *dbh = Z_PDO_DBH_P(ZEND_THIS);
1120-
char *str;
1121-
size_t str_len;
1122+
zend_string *str;
11221123
zend_long paramtype = PDO_PARAM_STR;
1123-
char *qstr;
1124-
size_t qlen;
11251124

11261125
ZEND_PARSE_PARAMETERS_START(1, 2)
1127-
Z_PARAM_STRING(str, str_len)
1126+
Z_PARAM_STR(str)
11281127
Z_PARAM_OPTIONAL
11291128
Z_PARAM_LONG(paramtype)
11301129
ZEND_PARSE_PARAMETERS_END();
@@ -1137,13 +1136,7 @@ PHP_METHOD(PDO, quote)
11371136
RETURN_FALSE;
11381137
}
11391138

1140-
if (dbh->methods->quoter(dbh, str, str_len, &qstr, &qlen, paramtype)) {
1141-
RETVAL_STRINGL(qstr, qlen);
1142-
efree(qstr);
1143-
return;
1144-
}
1145-
PDO_HANDLE_DBH_ERR();
1146-
RETURN_FALSE;
1139+
RETURN_STR(dbh->methods->quoter(dbh, str, paramtype));
11471140
}
11481141
/* }}} */
11491142

ext/pdo/php_pdo_driver.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ typedef int (*pdo_dbh_prepare_func)(pdo_dbh_t *dbh, zend_string *sql, pdo_stmt_t
235235
typedef zend_long (*pdo_dbh_do_func)(pdo_dbh_t *dbh, const char *sql, size_t sql_len);
236236

237237
/* quote a string */
238-
typedef bool (*pdo_dbh_quote_func)(pdo_dbh_t *dbh, const char *unquoted, size_t unquotedlen, char **quoted, size_t *quotedlen, enum pdo_param_type paramtype);
238+
typedef zend_string* (*pdo_dbh_quote_func)(pdo_dbh_t *dbh, const zend_string *unquoted, enum pdo_param_type paramtype);
239239

240240
/* transaction related (beingTransaction(), commit, rollBack, inTransaction)
241241
* return true in case of success, false otherwise */

ext/pdo_dblib/dblib_driver.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -145,14 +145,14 @@ static zend_long dblib_handle_doer(pdo_dbh_t *dbh, const char *sql, size_t sql_l
145145
return DBCOUNT(H->link);
146146
}
147147

148-
static bool dblib_handle_quoter(pdo_dbh_t *dbh, const char *unquoted, size_t unquotedlen, char **quoted, size_t *quotedlen, enum pdo_param_type paramtype)
148+
static zend_string* dblib_handle_quoter(pdo_dbh_t *dbh, const zend_string *unquoted, enum pdo_param_type paramtype)
149149
{
150150
pdo_dblib_db_handle *H = (pdo_dblib_db_handle *)dbh->driver_data;
151151
zend_bool use_national_character_set = 0;
152152

153153
size_t i;
154-
char * q;
155-
*quotedlen = 0;
154+
char *q, *quoted;
155+
size_t quotedlen = 0;
156156

157157
if (H->assume_national_character_set_strings) {
158158
use_national_character_set = 1;
@@ -165,34 +165,34 @@ static bool dblib_handle_quoter(pdo_dbh_t *dbh, const char *unquoted, size_t unq
165165
}
166166

167167
/* Detect quoted length, adding extra char for doubled single quotes */
168-
for (i = 0; i < unquotedlen; i++) {
169-
if (unquoted[i] == '\'') ++*quotedlen;
170-
++*quotedlen;
168+
for (i = 0; i < ZSTR_LEN(unquoted); i++) {
169+
if (ZSTR_VAL(unquoted)[i] == '\'') ++quotedlen;
170+
++quotedlen;
171171
}
172172

173-
*quotedlen += 2; /* +2 for opening, closing quotes */
173+
quotedlen += 2; /* +2 for opening, closing quotes */
174174
if (use_national_character_set) {
175-
++*quotedlen; /* N prefix */
175+
++quotedlen; /* N prefix */
176176
}
177-
q = *quoted = emalloc(*quotedlen + 1); /* Add byte for terminal null */
177+
q = quoted = emalloc(quotedlen + 1); /* Add byte for terminal null */
178178
if (use_national_character_set) {
179179
*q++ = 'N';
180180
}
181181
*q++ = '\'';
182182

183-
for (i = 0; i < unquotedlen; i++) {
183+
for (i = 0; i < ZSTR_LEN(unquoted); i++) {
184184
if (unquoted[i] == '\'') {
185185
*q++ = '\'';
186186
*q++ = '\'';
187187
} else {
188-
*q++ = unquoted[i];
188+
*q++ = ZSTR_VAL(unquoted)[i];
189189
}
190190
}
191191
*q++ = '\'';
192192

193193
*q = 0;
194194

195-
return true;
195+
return zend_string_init(quoted, quotedlen, 0);
196196
}
197197

198198
static bool pdo_dblib_transaction_cmd(const char *cmd, pdo_dbh_t *dbh)

ext/pdo_firebird/firebird_driver.c

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -651,26 +651,23 @@ static zend_long firebird_handle_doer(pdo_dbh_t *dbh, const char *sql, size_t sq
651651
/* }}} */
652652

653653
/* called by the PDO SQL parser to add quotes to values that are copied into SQL */
654-
static bool firebird_handle_quoter(pdo_dbh_t *dbh, const char *unquoted, size_t unquotedlen, /* {{{ */
655-
char **quoted, size_t *quotedlen, enum pdo_param_type paramtype)
654+
static zend_string* firebird_handle_quoter(pdo_dbh_t *dbh, const zend_string *unquoted, enum pdo_param_type paramtype)
656655
{
657656
int qcount = 0;
658657
char const *co, *l, *r;
659-
char *c;
658+
char *c, *quoted;
659+
size_t quotedlen;
660660

661-
if (!unquotedlen) {
662-
*quotedlen = 2;
663-
*quoted = emalloc(*quotedlen+1);
664-
strcpy(*quoted, "''");
665-
return true;
661+
if (ZSTR_LEN(unquoted) == 0) {
662+
return zend_string_init("''", 2, 0);
666663
}
667664

668665
/* Firebird only requires single quotes to be doubled if string lengths are used */
669666
/* count the number of ' characters */
670-
for (co = unquoted; (co = strchr(co,'\'')); qcount++, co++);
667+
for (co = ZSTR_VAL(unquoted); (co = strchr(co,'\'')); qcount++, co++);
671668

672-
*quotedlen = unquotedlen + qcount + 2;
673-
*quoted = c = emalloc(*quotedlen+1);
669+
quotedlen = ZSTR_LEN(unquoted) + qcount + 2;
670+
quoted = c = emalloc(quotedlen+1);
674671
*c++ = '\'';
675672

676673
/* foreach (chunk that ends in a quote) */
@@ -682,11 +679,11 @@ static bool firebird_handle_quoter(pdo_dbh_t *dbh, const char *unquoted, size_t
682679
}
683680

684681
/* copy the remainder */
685-
strncpy(c, l, *quotedlen-(c-*quoted)-1);
686-
(*quoted)[*quotedlen-1] = '\'';
687-
(*quoted)[*quotedlen] = '\0';
682+
strncpy(c, l, quotedlen-(c-quoted)-1);
683+
quoted[quotedlen-1] = '\'';
684+
quoted[quotedlen] = '\0';
688685

689-
return true;
686+
return zend_string_init(quoted, quotedlen, 0);
690687
}
691688
/* }}} */
692689

ext/pdo_mysql/mysql_driver.c

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -302,10 +302,12 @@ static char *pdo_mysql_last_insert_id(pdo_dbh_t *dbh, const char *name, size_t *
302302
#endif
303303

304304
/* {{{ mysql_handle_quoter */
305-
static bool mysql_handle_quoter(pdo_dbh_t *dbh, const char *unquoted, size_t unquotedlen, char **quoted, size_t *quotedlen, enum pdo_param_type paramtype )
305+
static zend_string* mysql_handle_quoter(pdo_dbh_t *dbh, const zend_string *unquoted, enum pdo_param_type paramtype )
306306
{
307307
pdo_mysql_db_handle *H = (pdo_mysql_db_handle *)dbh->driver_data;
308308
zend_bool use_national_character_set = 0;
309+
char *quoted;
310+
size_t quotedlen;
309311

310312
if (H->assume_national_character_set_strings) {
311313
use_national_character_set = 1;
@@ -319,24 +321,24 @@ static bool mysql_handle_quoter(pdo_dbh_t *dbh, const char *unquoted, size_t unq
319321

320322
PDO_DBG_ENTER("mysql_handle_quoter");
321323
PDO_DBG_INF_FMT("dbh=%p", dbh);
322-
PDO_DBG_INF_FMT("unquoted=%.*s", (int)unquotedlen, unquoted);
323-
*quoted = safe_emalloc(2, unquotedlen, 3 + (use_national_character_set ? 1 : 0));
324+
PDO_DBG_INF_FMT("unquoted=%.*s", (int)ZSTR_LEN(unquoted), ZSTR_VAL(unquoted));
325+
quoted = safe_emalloc(2, ZSTR_LEN(unquoted), 3 + (use_national_character_set ? 1 : 0));
324326

325327
if (use_national_character_set) {
326-
*quotedlen = mysql_real_escape_string_quote(H->server, *quoted + 2, unquoted, unquotedlen, '\'');
327-
(*quoted)[0] = 'N';
328-
(*quoted)[1] = '\'';
328+
quotedlen = mysql_real_escape_string_quote(H->server, quoted + 2, ZSTR_VAL(unquoted), ZSTR_LEN(unquoted), '\'');
329+
quoted[0] = 'N';
330+
quoted[1] = '\'';
329331

330-
++*quotedlen; /* N prefix */
332+
++quotedlen; /* N prefix */
331333
} else {
332-
*quotedlen = mysql_real_escape_string_quote(H->server, *quoted + 1, unquoted, unquotedlen, '\'');
333-
(*quoted)[0] = '\'';
334+
quotedlen = mysql_real_escape_string_quote(H->server, quoted + 1, ZSTR_VAL(unquoted), ZSTR_LEN(unquoted), '\'');
335+
quoted[0] = '\'';
334336
}
335337

336-
(*quoted)[++*quotedlen] = '\'';
337-
(*quoted)[++*quotedlen] = '\0';
338-
PDO_DBG_INF_FMT("quoted=%.*s", (int)*quotedlen, *quoted);
339-
PDO_DBG_RETURN(true);
338+
quoted[++quotedlen] = '\'';
339+
quoted[++quotedlen] = '\0';
340+
PDO_DBG_INF_FMT("quoted=%.*s", (int)quotedlen, quoted);
341+
PDO_DBG_RETURN(zend_string_init(quoted, quotedlen, 0));
340342
}
341343
/* }}} */
342344

ext/pdo_oci/oci_driver.c

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -354,40 +354,37 @@ static zend_long oci_handle_doer(pdo_dbh_t *dbh, const char *sql, size_t sql_len
354354
}
355355
/* }}} */
356356

357-
static bool oci_handle_quoter(pdo_dbh_t *dbh, const char *unquoted, size_t unquotedlen, char **quoted, size_t *quotedlen, enum pdo_param_type paramtype ) /* {{{ */
357+
static zend_string* oci_handle_quoter(pdo_dbh_t *dbh, const zend_string *unquoted, enum pdo_param_type paramtype ) /* {{{ */
358358
{
359359
int qcount = 0;
360360
char const *cu, *l, *r;
361-
char *c;
361+
char *c, *quoted;
362362

363-
if (!unquotedlen) {
364-
*quotedlen = 2;
365-
*quoted = emalloc(*quotedlen+1);
366-
strcpy(*quoted, "''");
367-
return true;
363+
if (ZSTR_LEN(unquoted) == 0) {
364+
return zend_string_init("''", 2, 0);
368365
}
369366

370367
/* count single quotes */
371-
for (cu = unquoted; (cu = strchr(cu,'\'')); qcount++, cu++)
368+
for (cu = ZSTR_VAL(unquoted); (cu = strchr(cu,'\'')); qcount++, cu++)
372369
; /* empty loop */
373370

374-
*quotedlen = unquotedlen + qcount + 2;
375-
*quoted = c = emalloc(*quotedlen+1);
371+
quotedlen = ZSTR_LEN(unquoted) + qcount + 2;
372+
quoted = c = emalloc(quotedlen+1);
376373
*c++ = '\'';
377374

378375
/* foreach (chunk that ends in a quote) */
379-
for (l = unquoted; (r = strchr(l,'\'')); l = r+1) {
376+
for (l = ZSTR_VAL(unquoted); (r = strchr(l,'\'')); l = r+1) {
380377
strncpy(c, l, r-l+1);
381378
c += (r-l+1);
382379
*c++ = '\''; /* add second quote */
383380
}
384381

385382
/* Copy remainder and add enclosing quote */
386-
strncpy(c, l, *quotedlen-(c-*quoted)-1);
387-
(*quoted)[*quotedlen-1] = '\'';
388-
(*quoted)[*quotedlen] = '\0';
383+
strncpy(c, l, quotedlen-(c-quoted)-1);
384+
quoted[quotedlen-1] = '\'';
385+
quoted[quotedlen] = '\0';
389386

390-
return true;
387+
return zend_string_init(quoted, quotedlen, 0);
391388
}
392389
/* }}} */
393390

ext/pdo_pgsql/pgsql_driver.c

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -319,33 +319,35 @@ static zend_long pgsql_handle_doer(pdo_dbh_t *dbh, const char *sql, size_t sql_l
319319
return ret;
320320
}
321321

322-
static bool pgsql_handle_quoter(pdo_dbh_t *dbh, const char *unquoted, size_t unquotedlen, char **quoted, size_t *quotedlen, enum pdo_param_type paramtype)
322+
static zend_string* pgsql_handle_quoter(pdo_dbh_t *dbh, const zend_string *unquoted, enum pdo_param_type paramtype)
323323
{
324324
unsigned char *escaped;
325+
char *quoted;
326+
size_t quotedlen;
325327
pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
326328
size_t tmp_len;
327329

328330
switch (paramtype) {
329331
case PDO_PARAM_LOB:
330332
/* escapedlen returned by PQescapeBytea() accounts for trailing 0 */
331-
escaped = PQescapeByteaConn(H->server, (unsigned char *)unquoted, unquotedlen, &tmp_len);
332-
*quotedlen = tmp_len + 1;
333-
*quoted = emalloc(*quotedlen + 1);
334-
memcpy((*quoted)+1, escaped, *quotedlen-2);
335-
(*quoted)[0] = '\'';
336-
(*quoted)[*quotedlen-1] = '\'';
337-
(*quoted)[*quotedlen] = '\0';
333+
escaped = PQescapeByteaConn(H->server, (unsigned char *)ZSTR_VAL(unquoted), ZSTR_LEN(unquoted), &tmp_len);
334+
quotedlen = tmp_len + 1;
335+
quoted = emalloc(quotedlen + 1);
336+
memcpy(quoted+1, escaped, quotedlen-2);
337+
quoted[0] = '\'';
338+
quoted[quotedlen-1] = '\'';
339+
quoted[quotedlen] = '\0';
338340
PQfreemem(escaped);
339341
break;
340342
default:
341-
*quoted = safe_emalloc(2, unquotedlen, 3);
342-
(*quoted)[0] = '\'';
343-
*quotedlen = PQescapeStringConn(H->server, *quoted + 1, unquoted, unquotedlen, NULL);
344-
(*quoted)[*quotedlen + 1] = '\'';
345-
(*quoted)[*quotedlen + 2] = '\0';
346-
*quotedlen += 2;
347-
}
348-
return true;
343+
quoted = safe_emalloc(2, ZSTR_LEN(unquoted), 3);
344+
quoted[0] = '\'';
345+
quotedlen = PQescapeStringConn(H->server, quoted + 1, ZSTR_VAL(unquoted), ZSTR_LEN(unquoted), NULL);
346+
quoted[quotedlen + 1] = '\'';
347+
quoted[quotedlen + 2] = '\0';
348+
quotedlen += 2;
349+
}
350+
return zend_string_init(quoted, quotedlen, 0);
349351
}
350352

351353
static char *pdo_pgsql_last_insert_id(pdo_dbh_t *dbh, const char *name, size_t *len)

ext/pdo_sqlite/sqlite_driver.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -230,12 +230,11 @@ static char *pdo_sqlite_last_insert_id(pdo_dbh_t *dbh, const char *name, size_t
230230
}
231231

232232
/* NB: doesn't handle binary strings... use prepared stmts for that */
233-
static bool sqlite_handle_quoter(pdo_dbh_t *dbh, const char *unquoted, size_t unquotedlen, char **quoted, size_t *quotedlen, enum pdo_param_type paramtype )
233+
static zend_string* sqlite_handle_quoter(pdo_dbh_t *dbh, const zend_string *unquoted, enum pdo_param_type paramtype)
234234
{
235-
*quoted = safe_emalloc(2, unquotedlen, 3);
236-
sqlite3_snprintf(2*unquotedlen + 3, *quoted, "'%q'", unquoted);
237-
*quotedlen = strlen(*quoted);
238-
return true;
235+
char *quoted = emalloc(2*ZSTR_LEN(unquoted) + 3);
236+
sqlite3_snprintf(2*ZSTR_LEN(unquoted) + 3, quoted, "%Q", ZSTR_VAL(unquoted));
237+
return zend_string_init(quoted, strlen(quoted), 0);
239238
}
240239

241240
static bool sqlite_handle_begin(pdo_dbh_t *dbh)

0 commit comments

Comments
 (0)