Skip to content

Commit 92e1f89

Browse files
committed
Refactor PDO placeholder's quoted string to *zend_string
1 parent bdd3d0a commit 92e1f89

File tree

2 files changed

+24
-34
lines changed

2 files changed

+24
-34
lines changed

Zend/zend_string.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,9 @@ EMPTY_SWITCH_DEFAULT_CASE()
551551
_(ZEND_STR_FALSE, "false") \
552552
_(ZEND_STR_NULL_LOWERCASE, "null") \
553553
_(ZEND_STR_MIXED, "mixed") \
554+
_(ZEND_STR_0, "0") \
555+
_(ZEND_STR_1, "1") \
556+
_(ZEND_STR_QUESTION_MARK, "?") \
554557

555558

556559
typedef enum _zend_known_string_id {

ext/pdo/pdo_sql_parser.re

Lines changed: 21 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,7 @@ static int scan(Scanner *s)
7070
struct placeholder {
7171
const char *pos;
7272
size_t len;
73-
size_t qlen; /* quoted length of value */
74-
char *quoted; /* quoted value */
73+
zend_string *quoted; /* quoted value */
7574
int freeq;
7675
int bindno;
7776
struct placeholder *next;
@@ -123,8 +122,7 @@ PDO_API int pdo_parse_params(pdo_stmt_t *stmt, zend_string *inquery, zend_string
123122

124123
if (t == PDO_PARSER_ESCAPED_QUESTION) {
125124
plc->bindno = PDO_PARSER_BINDNO_ESCAPED_CHAR;
126-
plc->quoted = "?";
127-
plc->qlen = 1;
125+
plc->quoted = ZSTR_KNOWN(ZEND_STR_QUESTION_MARK);
128126
plc->freeq = 0;
129127
escapes++;
130128
} else {
@@ -243,9 +241,8 @@ safe:
243241
}
244242

245243
quoted_buf = stmt->dbh->methods->quoter(stmt->dbh, buf, param->param_type);
246-
plc->quoted = estrndup(ZSTR_VAL(quoted_buf), ZSTR_LEN(quoted_buf));
247-
plc->qlen = ZSTR_LEN(quoted_buf);
248-
zend_string_release_ex(quoted_buf, 0);
244+
plc->quoted = quoted_buf;
245+
plc->freeq = 1;
249246

250247
if (buf) {
251248
zend_string_release_ex(buf, 0);
@@ -255,7 +252,6 @@ safe:
255252
ret = -1;
256253
goto clean_up;
257254
}
258-
plc->freeq = 1;
259255
} else {
260256
enum pdo_param_type param_type = param->param_type;
261257
zend_string *buf = NULL;
@@ -267,22 +263,17 @@ safe:
267263

268264
switch (param_type) {
269265
case PDO_PARAM_BOOL:
270-
plc->quoted = zend_is_true(parameter) ? "1" : "0";
271-
plc->qlen = sizeof("1")-1;
266+
plc->quoted = zend_is_true(parameter) ? ZSTR_KNOWN(ZEND_STR_1) : ZSTR_KNOWN(ZEND_STR_0);
272267
plc->freeq = 0;
273268
break;
274269

275270
case PDO_PARAM_INT:
276-
buf = zend_long_to_str(zval_get_long(parameter));
277-
278-
plc->qlen = ZSTR_LEN(buf);
279-
plc->quoted = estrdup(ZSTR_VAL(buf));
271+
plc->quoted = zend_long_to_str(zval_get_long(parameter));
280272
plc->freeq = 1;
281273
break;
282274

283275
case PDO_PARAM_NULL:
284-
plc->quoted = "NULL";
285-
plc->qlen = sizeof("NULL")-1;
276+
plc->quoted = ZSTR_KNOWN(ZEND_STR_NULL);
286277
plc->freeq = 0;
287278
break;
288279

@@ -304,10 +295,8 @@ safe:
304295
}
305296

306297
quoted_buf = stmt->dbh->methods->quoter(stmt->dbh, buf, param_type);
307-
plc->quoted = estrndup(ZSTR_VAL(quoted_buf), ZSTR_LEN(quoted_buf));
308-
plc->qlen = ZSTR_LEN(quoted_buf);
298+
plc->quoted = quoted_buf;
309299
plc->freeq = 1;
310-
zend_string_release_ex(quoted_buf, 0);
311300
}
312301
}
313302

@@ -322,10 +311,9 @@ safe:
322311
} else {
323312
parameter = &param->parameter;
324313
}
325-
plc->quoted = Z_STRVAL_P(parameter);
326-
plc->qlen = Z_STRLEN_P(parameter);
314+
plc->quoted = Z_STR_P(parameter);
327315
}
328-
newbuffer_len += plc->qlen;
316+
newbuffer_len += ZSTR_LEN(plc->quoted);
329317
}
330318

331319
rewrite:
@@ -344,8 +332,8 @@ rewrite:
344332
newbuffer += t;
345333
}
346334
if (plc->quoted) {
347-
memcpy(newbuffer, plc->quoted, plc->qlen);
348-
newbuffer += plc->qlen;
335+
memcpy(newbuffer, ZSTR_VAL(plc->quoted), ZSTR_LEN(plc->quoted));
336+
newbuffer += ZSTR_LEN(plc->quoted);
349337
} else {
350338
memcpy(newbuffer, plc->pos, plc->len);
351339
newbuffer += plc->len;
@@ -368,7 +356,7 @@ rewrite:
368356

369357
} else if (query_type == PDO_PLACEHOLDER_POSITIONAL) {
370358
/* rewrite ? to :pdoX */
371-
char *name, *idxbuf;
359+
char *name;
372360
const char *tmpl = stmt->named_rewrite_template ? stmt->named_rewrite_template : ":pdo%d";
373361
int bind_no = 1;
374362

@@ -382,6 +370,7 @@ rewrite:
382370
for (plc = placeholders; plc; plc = plc->next) {
383371
int skip_map = 0;
384372
char *p;
373+
zend_string *idxbuf;
385374

386375
if (plc->bindno == PDO_PARSER_BINDNO_ESCAPED_CHAR) {
387376
continue;
@@ -391,24 +380,23 @@ rewrite:
391380

392381
/* check if bound parameter is already available */
393382
if (!strcmp(name, "?") || (p = zend_hash_str_find_ptr(stmt->bound_param_map, name, plc->len)) == NULL) {
394-
spprintf(&idxbuf, 0, tmpl, bind_no++);
383+
idxbuf = strpprintf(0, tmpl, bind_no++);
395384
} else {
396-
idxbuf = estrdup(p);
385+
idxbuf = zend_string_init(p, strlen(p), 0);
397386
skip_map = 1;
398387
}
399388

400389
plc->quoted = idxbuf;
401-
plc->qlen = strlen(plc->quoted);
402390
plc->freeq = 1;
403-
newbuffer_len += plc->qlen;
391+
newbuffer_len += ZSTR_LEN(plc->quoted);
404392

405393
if (!skip_map && stmt->named_rewrite_template) {
406394
/* create a mapping */
407-
zend_hash_str_update_mem(stmt->bound_param_map, name, plc->len, idxbuf, plc->qlen + 1);
395+
zend_hash_str_update_mem(stmt->bound_param_map, name, plc->len, ZSTR_VAL(plc->quoted), ZSTR_LEN(plc->quoted) + 1);
408396
}
409397

410398
/* map number to name */
411-
zend_hash_index_update_mem(stmt->bound_param_map, plc->bindno, idxbuf, plc->qlen + 1);
399+
zend_hash_index_update_mem(stmt->bound_param_map, plc->bindno, ZSTR_VAL(plc->quoted), ZSTR_LEN(plc->quoted) + 1);
412400

413401
efree(name);
414402
}
@@ -430,8 +418,7 @@ rewrite:
430418
name = estrndup(plc->pos, plc->len);
431419
zend_hash_index_update_mem(stmt->bound_param_map, plc->bindno, name, plc->len + 1);
432420
efree(name);
433-
plc->quoted = "?";
434-
plc->qlen = 1;
421+
plc->quoted = ZSTR_KNOWN(ZEND_STR_QUESTION_MARK);
435422
newbuffer_len -= plc->len - 1;
436423
}
437424

@@ -445,7 +432,7 @@ clean_up:
445432
placeholders = plc->next;
446433

447434
if (plc->freeq) {
448-
efree(plc->quoted);
435+
zend_string_release_ex(plc->quoted, 0);
449436
}
450437

451438
efree(plc);

0 commit comments

Comments
 (0)