@@ -70,8 +70,7 @@ static int scan(Scanner *s)
70
70
struct placeholder {
71
71
const char *pos;
72
72
size_t len;
73
- size_t qlen; /* quoted length of value */
74
- char *quoted; /* quoted value */
73
+ zend_string *quoted; /* quoted value */
75
74
int freeq;
76
75
int bindno;
77
76
struct placeholder *next;
@@ -123,8 +122,7 @@ PDO_API int pdo_parse_params(pdo_stmt_t *stmt, zend_string *inquery, zend_string
123
122
124
123
if (t == PDO_PARSER_ESCAPED_QUESTION) {
125
124
plc->bindno = PDO_PARSER_BINDNO_ESCAPED_CHAR;
126
- plc->quoted = " ?" ;
127
- plc->qlen = 1 ;
125
+ plc->quoted = ZSTR_CHAR (' ?' );
128
126
plc->freeq = 0 ;
129
127
escapes++;
130
128
} else {
@@ -235,17 +233,14 @@ safe:
235
233
php_stream_from_zval_no_verify (stm, parameter);
236
234
if (stm) {
237
235
zend_string *buf;
238
- zend_string *quoted_buf;
239
236
240
237
buf = php_stream_copy_to_mem (stm, PHP_STREAM_COPY_ALL, 0 );
241
238
if (!buf) {
242
239
buf = ZSTR_EMPTY_ALLOC ();
243
240
}
244
241
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 ;
249
244
250
245
if (buf) {
251
246
zend_string_release_ex (buf, 0 );
@@ -255,7 +250,6 @@ safe:
255
250
ret = -1 ;
256
251
goto clean_up;
257
252
}
258
- plc->freeq = 1 ;
259
253
} else {
260
254
enum pdo_param_type param_type = param->param_type ;
261
255
zend_string *buf = NULL ;
@@ -267,47 +261,32 @@ safe:
267
261
268
262
switch (param_type) {
269
263
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' );
272
265
plc->freeq = 0 ;
273
266
break ;
274
267
275
268
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));
280
270
plc->freeq = 1 ;
281
271
break ;
282
272
283
273
case PDO_PARAM_NULL:
284
- plc->quoted = " NULL" ;
285
- plc->qlen = sizeof (" NULL" )-1 ;
274
+ plc->quoted = ZSTR_KNOWN (ZEND_STR_NULL);
286
275
plc->freeq = 0 ;
287
276
break ;
288
277
289
278
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 */
295
281
if (EG (exception)) {
296
282
/* bork */
297
283
ret = -1 ;
298
284
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
- }
303
285
goto clean_up;
304
286
}
305
287
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);
309
289
plc->freeq = 1 ;
310
- zend_string_release_ex (quoted_buf, 0 );
311
290
}
312
291
}
313
292
@@ -322,10 +301,9 @@ safe:
322
301
} else {
323
302
parameter = ¶m->parameter ;
324
303
}
325
- plc->quoted = Z_STRVAL_P (parameter);
326
- plc->qlen = Z_STRLEN_P (parameter);
304
+ plc->quoted = Z_STR_P (parameter);
327
305
}
328
- newbuffer_len += plc->qlen ;
306
+ newbuffer_len += ZSTR_LEN ( plc->quoted ) ;
329
307
}
330
308
331
309
rewrite:
@@ -344,8 +322,8 @@ rewrite:
344
322
newbuffer += t;
345
323
}
346
324
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 ) ;
349
327
} else {
350
328
memcpy (newbuffer, plc->pos , plc->len );
351
329
newbuffer += plc->len ;
@@ -368,7 +346,7 @@ rewrite:
368
346
369
347
} else if (query_type == PDO_PLACEHOLDER_POSITIONAL) {
370
348
/* rewrite ? to :pdoX */
371
- char *name, *idxbuf ;
349
+ char *name;
372
350
const char *tmpl = stmt->named_rewrite_template ? stmt->named_rewrite_template : " :pdo%d" ;
373
351
int bind_no = 1 ;
374
352
@@ -382,6 +360,7 @@ rewrite:
382
360
for (plc = placeholders; plc; plc = plc->next ) {
383
361
int skip_map = 0 ;
384
362
char *p;
363
+ zend_string *idxbuf;
385
364
386
365
if (plc->bindno == PDO_PARSER_BINDNO_ESCAPED_CHAR) {
387
366
continue ;
@@ -391,24 +370,23 @@ rewrite:
391
370
392
371
/* check if bound parameter is already available */
393
372
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++);
395
374
} else {
396
- idxbuf = estrdup (p );
375
+ idxbuf = zend_string_init (p, strlen (p), 0 );
397
376
skip_map = 1 ;
398
377
}
399
378
400
379
plc->quoted = idxbuf;
401
- plc->qlen = strlen (plc->quoted );
402
380
plc->freeq = 1 ;
403
- newbuffer_len += plc->qlen ;
381
+ newbuffer_len += ZSTR_LEN ( plc->quoted ) ;
404
382
405
383
if (!skip_map && stmt->named_rewrite_template ) {
406
384
/* 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 );
408
386
}
409
387
410
388
/* 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 );
412
390
413
391
efree (name);
414
392
}
@@ -430,8 +408,7 @@ rewrite:
430
408
name = estrndup (plc->pos , plc->len );
431
409
zend_hash_index_update_mem (stmt->bound_param_map , plc->bindno , name, plc->len + 1 );
432
410
efree (name);
433
- plc->quoted = " ?" ;
434
- plc->qlen = 1 ;
411
+ plc->quoted = ZSTR_CHAR (' ?' );
435
412
newbuffer_len -= plc->len - 1 ;
436
413
}
437
414
@@ -445,7 +422,7 @@ clean_up:
445
422
placeholders = plc->next ;
446
423
447
424
if (plc->freeq ) {
448
- efree (plc->quoted );
425
+ zend_string_release_ex (plc->quoted , 0 );
449
426
}
450
427
451
428
efree (plc);
0 commit comments