Skip to content

Commit 32a254f

Browse files
committed
Revert "Refactor PDO quoter handler to return a zend_string*"
Just a sanity check that I didn't break something else This reverts commit a3b48c4.
1 parent a3b48c4 commit 32a254f

File tree

9 files changed

+106
-96
lines changed

9 files changed

+106
-96
lines changed

ext/pdo/pdo_dbh.c

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

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 */
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 */
11191117
PHP_METHOD(PDO, quote)
11201118
{
11211119
pdo_dbh_t *dbh = Z_PDO_DBH_P(ZEND_THIS);
1122-
zend_string *str;
1120+
char *str;
1121+
size_t str_len;
11231122
zend_long paramtype = PDO_PARAM_STR;
1123+
char *qstr;
1124+
size_t qlen;
11241125

11251126
ZEND_PARSE_PARAMETERS_START(1, 2)
1126-
Z_PARAM_STR(str)
1127+
Z_PARAM_STRING(str, str_len)
11271128
Z_PARAM_OPTIONAL
11281129
Z_PARAM_LONG(paramtype)
11291130
ZEND_PARSE_PARAMETERS_END();
@@ -1136,7 +1137,13 @@ PHP_METHOD(PDO, quote)
11361137
RETURN_FALSE;
11371138
}
11381139

1139-
RETURN_STR(dbh->methods->quoter(dbh, str, paramtype));
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;
11401147
}
11411148
/* }}} */
11421149

ext/pdo/pdo_sql_parser.re

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -235,18 +235,21 @@ safe:
235235
php_stream_from_zval_no_verify(stm, parameter);
236236
if (stm) {
237237
zend_string *buf;
238-
zend_string *quoted;
239238

240239
buf = php_stream_copy_to_mem(stm, PHP_STREAM_COPY_ALL, 0);
241240
if (!buf) {
242241
buf = ZSTR_EMPTY_ALLOC();
243242
}
244-
245-
quoted = stmt->dbh->methods->quoter(stmt->dbh, buf, param->param_type);
246-
247-
plc->quoted = ZSTR_VAL(quoted);
248-
plc->qlen = ZSTR_LEN(quoted);
249-
243+
if (!stmt->dbh->methods->quoter(stmt->dbh, ZSTR_VAL(buf), ZSTR_LEN(buf), &plc->quoted, &plc->qlen,
244+
param->param_type)) {
245+
/* bork */
246+
ret = -1;
247+
strncpy(stmt->error_code, stmt->dbh->error_code, 6);
248+
if (buf) {
249+
zend_string_release_ex(buf, 0);
250+
}
251+
goto clean_up;
252+
}
250253
if (buf) {
251254
zend_string_release_ex(buf, 0);
252255
}
@@ -286,10 +289,12 @@ safe:
286289
plc->freeq = 0;
287290
break;
288291

289-
default: {
292+
default:
290293
buf = zval_get_string(parameter);
291-
292-
if (EG(exception)) {
294+
if (EG(exception) ||
295+
!stmt->dbh->methods->quoter(stmt->dbh, ZSTR_VAL(buf),
296+
ZSTR_LEN(buf), &plc->quoted, &plc->qlen,
297+
param_type)) {
293298
/* bork */
294299
ret = -1;
295300
strncpy(stmt->error_code, stmt->dbh->error_code, 6);
@@ -298,12 +303,7 @@ safe:
298303
}
299304
goto clean_up;
300305
}
301-
302-
zend_string *quoted = stmt->dbh->methods->quoter(stmt->dbh, buf, param->param_type);
303-
plc->quoted = ZSTR_VAL(quoted);
304-
plc->qlen = ZSTR_LEN(quoted);
305306
plc->freeq = 1;
306-
}
307307
}
308308

309309
if (buf) {

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 zend_string* (*pdo_dbh_quote_func)(pdo_dbh_t *dbh, const zend_string *unquoted, enum pdo_param_type paramtype);
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);
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 zend_string* dblib_handle_quoter(pdo_dbh_t *dbh, const zend_string *unquoted, enum pdo_param_type paramtype)
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)
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, *quoted;
155-
size_t quotedlen = 0;
154+
char * q;
155+
*quotedlen = 0;
156156

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

167167
/* Detect quoted length, adding extra char for doubled single quotes */
168-
for (i = 0; i < ZSTR_LEN(unquoted); i++) {
169-
if (ZSTR_VAL(unquoted)[i] == '\'') ++quotedlen;
170-
++quotedlen;
168+
for (i = 0; i < unquotedlen; i++) {
169+
if (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 < ZSTR_LEN(unquoted); i++) {
183+
for (i = 0; i < unquotedlen; i++) {
184184
if (unquoted[i] == '\'') {
185185
*q++ = '\'';
186186
*q++ = '\'';
187187
} else {
188-
*q++ = ZSTR_VAL(unquoted)[i];
188+
*q++ = unquoted[i];
189189
}
190190
}
191191
*q++ = '\'';
192192

193193
*q = 0;
194194

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

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

ext/pdo_firebird/firebird_driver.c

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -651,39 +651,42 @@ 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 zend_string* firebird_handle_quoter(pdo_dbh_t *dbh, const zend_string *unquoted, enum pdo_param_type paramtype)
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)
655656
{
656657
int qcount = 0;
657658
char const *co, *l, *r;
658-
char *c, *quoted;
659-
size_t quotedlen;
659+
char *c;
660660

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

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

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

673676
/* foreach (chunk that ends in a quote) */
674-
for (l = ZSTR_VAL(unquoted); (r = strchr(l,'\'')); l = r+1) {
677+
for (l = unquoted; (r = strchr(l,'\'')); l = r+1) {
675678
strncpy(c, l, r-l+1);
676679
c += (r-l+1);
677680
/* add the second quote */
678681
*c++ = '\'';
679682
}
680683

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

686-
return zend_string_init(quoted, quotedlen, 0);
689+
return true;
687690
}
688691
/* }}} */
689692

ext/pdo_mysql/mysql_driver.c

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -302,12 +302,10 @@ 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 zend_string* mysql_handle_quoter(pdo_dbh_t *dbh, const zend_string *unquoted, enum pdo_param_type paramtype )
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 )
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;
311309

312310
if (H->assume_national_character_set_strings) {
313311
use_national_character_set = 1;
@@ -321,24 +319,24 @@ static zend_string* mysql_handle_quoter(pdo_dbh_t *dbh, const zend_string *unquo
321319

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

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

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

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));
336+
(*quoted)[++*quotedlen] = '\'';
337+
(*quoted)[++*quotedlen] = '\0';
338+
PDO_DBG_INF_FMT("quoted=%.*s", (int)*quotedlen, *quoted);
339+
PDO_DBG_RETURN(true);
342340
}
343341
/* }}} */
344342

ext/pdo_oci/oci_driver.c

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

357-
static zend_string* oci_handle_quoter(pdo_dbh_t *dbh, const zend_string *unquoted, enum pdo_param_type paramtype ) /* {{{ */
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 ) /* {{{ */
358358
{
359359
int qcount = 0;
360360
char const *cu, *l, *r;
361-
char *c, *quoted;
361+
char *c;
362362

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

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

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

375378
/* foreach (chunk that ends in a quote) */
376-
for (l = ZSTR_VAL(unquoted); (r = strchr(l,'\'')); l = r+1) {
379+
for (l = unquoted; (r = strchr(l,'\'')); l = r+1) {
377380
strncpy(c, l, r-l+1);
378381
c += (r-l+1);
379382
*c++ = '\''; /* add second quote */
380383
}
381384

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

387-
return zend_string_init(quoted, quotedlen, 0);
390+
return true;
388391
}
389392
/* }}} */
390393

ext/pdo_pgsql/pgsql_driver.c

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

322-
static zend_string* pgsql_handle_quoter(pdo_dbh_t *dbh, const zend_string *unquoted, enum pdo_param_type paramtype)
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)
323323
{
324324
unsigned char *escaped;
325-
char *quoted;
326-
size_t quotedlen;
327325
pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
328326
size_t tmp_len;
329327

330328
switch (paramtype) {
331329
case PDO_PARAM_LOB:
332330
/* escapedlen returned by PQescapeBytea() accounts for trailing 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';
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';
340338
PQfreemem(escaped);
341339
break;
342340
default:
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);
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;
351349
}
352350

353351
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: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -230,11 +230,12 @@ 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 zend_string* sqlite_handle_quoter(pdo_dbh_t *dbh, const zend_string *unquoted, enum pdo_param_type paramtype)
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 )
234234
{
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);
235+
*quoted = safe_emalloc(2, unquotedlen, 3);
236+
sqlite3_snprintf(2*unquotedlen + 3, *quoted, "'%q'", unquoted);
237+
*quotedlen = strlen(*quoted);
238+
return true;
238239
}
239240

240241
static bool sqlite_handle_begin(pdo_dbh_t *dbh)

0 commit comments

Comments
 (0)