Skip to content

Commit 848f7e0

Browse files
committed
Refactor PDO placeholder's quoted string to zend_string
1 parent 63cda0f commit 848f7e0

File tree

1 file changed

+23
-46
lines changed

1 file changed

+23
-46
lines changed

ext/pdo/pdo_sql_parser.re

Lines changed: 23 additions & 46 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_CHAR('?');
128126
plc->freeq = 0;
129127
escapes++;
130128
} else {
@@ -235,17 +233,14 @@ safe:
235233
php_stream_from_zval_no_verify(stm, parameter);
236234
if (stm) {
237235
zend_string *buf;
238-
zend_string *quoted_buf;
239236

240237
buf = php_stream_copy_to_mem(stm, PHP_STREAM_COPY_ALL, 0);
241238
if (!buf) {
242239
buf = ZSTR_EMPTY_ALLOC();
243240
}
244241

245-
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);
242+
plc->quoted = stmt->dbh->methods->quoter(stmt->dbh, buf, param->param_type);
243+
plc->freeq = 1;
249244

250245
if (buf) {
251246
zend_string_release_ex(buf, 0);
@@ -255,7 +250,6 @@ safe:
255250
ret = -1;
256251
goto clean_up;
257252
}
258-
plc->freeq = 1;
259253
} else {
260254
enum pdo_param_type param_type = param->param_type;
261255
zend_string *buf = NULL;
@@ -267,47 +261,32 @@ safe:
267261

268262
switch (param_type) {
269263
case PDO_PARAM_BOOL:
270-
plc->quoted = zend_is_true(parameter) ? "1" : "0";
271-
plc->qlen = sizeof("1")-1;
264+
plc->quoted = zend_is_true(parameter) ? ZSTR_CHAR('1') : ZSTR_CHAR('0');
272265
plc->freeq = 0;
273266
break;
274267

275268
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));
269+
plc->quoted = zend_long_to_str(zval_get_long(parameter));
280270
plc->freeq = 1;
281271
break;
282272

283273
case PDO_PARAM_NULL:
284-
plc->quoted = "NULL";
285-
plc->qlen = sizeof("NULL")-1;
274+
plc->quoted = ZSTR_KNOWN(ZEND_STR_NULL);
286275
plc->freeq = 0;
287276
break;
288277

289278
default: {
290-
zend_string *quoted_buf;
291-
292-
/* TODO Should this be zval_try_get_string_func() ? */
293-
buf = zval_get_string(parameter);
294-
/* TODO Check when this can occur? */
279+
buf = zval_try_get_string(parameter);
280+
/* parameter does not have a string representation, buf == NULL */
295281
if (EG(exception)) {
296282
/* bork */
297283
ret = -1;
298284
strncpy(stmt->error_code, stmt->dbh->error_code, 6);
299-
/* TODO Is this dead code now? */
300-
if (buf) {
301-
zend_string_release_ex(buf, 0);
302-
}
303285
goto clean_up;
304286
}
305287

306-
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);
288+
plc->quoted = stmt->dbh->methods->quoter(stmt->dbh, buf, param_type);
309289
plc->freeq = 1;
310-
zend_string_release_ex(quoted_buf, 0);
311290
}
312291
}
313292

@@ -322,10 +301,9 @@ safe:
322301
} else {
323302
parameter = &param->parameter;
324303
}
325-
plc->quoted = Z_STRVAL_P(parameter);
326-
plc->qlen = Z_STRLEN_P(parameter);
304+
plc->quoted = Z_STR_P(parameter);
327305
}
328-
newbuffer_len += plc->qlen;
306+
newbuffer_len += ZSTR_LEN(plc->quoted);
329307
}
330308

331309
rewrite:
@@ -344,8 +322,8 @@ rewrite:
344322
newbuffer += t;
345323
}
346324
if (plc->quoted) {
347-
memcpy(newbuffer, plc->quoted, plc->qlen);
348-
newbuffer += plc->qlen;
325+
memcpy(newbuffer, ZSTR_VAL(plc->quoted), ZSTR_LEN(plc->quoted));
326+
newbuffer += ZSTR_LEN(plc->quoted);
349327
} else {
350328
memcpy(newbuffer, plc->pos, plc->len);
351329
newbuffer += plc->len;
@@ -368,7 +346,7 @@ rewrite:
368346

369347
} else if (query_type == PDO_PLACEHOLDER_POSITIONAL) {
370348
/* rewrite ? to :pdoX */
371-
char *name, *idxbuf;
349+
char *name;
372350
const char *tmpl = stmt->named_rewrite_template ? stmt->named_rewrite_template : ":pdo%d";
373351
int bind_no = 1;
374352

@@ -382,6 +360,7 @@ rewrite:
382360
for (plc = placeholders; plc; plc = plc->next) {
383361
int skip_map = 0;
384362
char *p;
363+
zend_string *idxbuf;
385364

386365
if (plc->bindno == PDO_PARSER_BINDNO_ESCAPED_CHAR) {
387366
continue;
@@ -391,24 +370,23 @@ rewrite:
391370

392371
/* check if bound parameter is already available */
393372
if (!strcmp(name, "?") || (p = zend_hash_str_find_ptr(stmt->bound_param_map, name, plc->len)) == NULL) {
394-
spprintf(&idxbuf, 0, tmpl, bind_no++);
373+
idxbuf = zend_strpprintf(0, tmpl, bind_no++);
395374
} else {
396-
idxbuf = estrdup(p);
375+
idxbuf = zend_string_init(p, strlen(p), 0);
397376
skip_map = 1;
398377
}
399378

400379
plc->quoted = idxbuf;
401-
plc->qlen = strlen(plc->quoted);
402380
plc->freeq = 1;
403-
newbuffer_len += plc->qlen;
381+
newbuffer_len += ZSTR_LEN(plc->quoted);
404382

405383
if (!skip_map && stmt->named_rewrite_template) {
406384
/* create a mapping */
407-
zend_hash_str_update_mem(stmt->bound_param_map, name, plc->len, idxbuf, plc->qlen + 1);
385+
zend_hash_str_update_mem(stmt->bound_param_map, name, plc->len, ZSTR_VAL(plc->quoted), ZSTR_LEN(plc->quoted) + 1);
408386
}
409387

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

413391
efree(name);
414392
}
@@ -430,8 +408,7 @@ rewrite:
430408
name = estrndup(plc->pos, plc->len);
431409
zend_hash_index_update_mem(stmt->bound_param_map, plc->bindno, name, plc->len + 1);
432410
efree(name);
433-
plc->quoted = "?";
434-
plc->qlen = 1;
411+
plc->quoted = ZSTR_CHAR('?');
435412
newbuffer_len -= plc->len - 1;
436413
}
437414

@@ -445,7 +422,7 @@ clean_up:
445422
placeholders = plc->next;
446423

447424
if (plc->freeq) {
448-
efree(plc->quoted);
425+
zend_string_release_ex(plc->quoted, 0);
449426
}
450427

451428
efree(plc);

0 commit comments

Comments
 (0)