Skip to content

Commit 404c96e

Browse files
authored
Avoid string copies for date/time format in firebird (#17902)
1 parent f89b0a2 commit 404c96e

File tree

3 files changed

+32
-23
lines changed

3 files changed

+32
-23
lines changed

ext/pdo_firebird/firebird_driver.c

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -599,13 +599,13 @@ static void firebird_handle_closer(pdo_dbh_t *dbh) /* {{{ */
599599
}
600600

601601
if (H->date_format) {
602-
efree(H->date_format);
602+
zend_string_release_ex(H->date_format, false);
603603
}
604604
if (H->time_format) {
605-
efree(H->time_format);
605+
zend_string_release_ex(H->time_format, false);
606606
}
607607
if (H->timestamp_format) {
608-
efree(H->timestamp_format);
608+
zend_string_release_ex(H->timestamp_format, false);
609609
}
610610

611611
if (H->einfo.errmsg) {
@@ -1091,10 +1091,9 @@ static bool pdo_firebird_set_attribute(pdo_dbh_t *dbh, zend_long attr, zval *val
10911091
return false;
10921092
}
10931093
if (H->date_format) {
1094-
efree(H->date_format);
1094+
zend_string_release_ex(H->date_format, false);
10951095
}
1096-
spprintf(&H->date_format, 0, "%s", ZSTR_VAL(str));
1097-
zend_string_release_ex(str, 0);
1096+
H->date_format = str;
10981097
}
10991098
return true;
11001099

@@ -1105,10 +1104,9 @@ static bool pdo_firebird_set_attribute(pdo_dbh_t *dbh, zend_long attr, zval *val
11051104
return false;
11061105
}
11071106
if (H->time_format) {
1108-
efree(H->time_format);
1107+
zend_string_release_ex(H->time_format, false);
11091108
}
1110-
spprintf(&H->time_format, 0, "%s", ZSTR_VAL(str));
1111-
zend_string_release_ex(str, 0);
1109+
H->time_format = str;
11121110
}
11131111
return true;
11141112

@@ -1119,10 +1117,9 @@ static bool pdo_firebird_set_attribute(pdo_dbh_t *dbh, zend_long attr, zval *val
11191117
return false;
11201118
}
11211119
if (H->timestamp_format) {
1122-
efree(H->timestamp_format);
1120+
zend_string_release_ex(H->timestamp_format, false);
11231121
}
1124-
spprintf(&H->timestamp_format, 0, "%s", ZSTR_VAL(str));
1125-
zend_string_release_ex(str, 0);
1122+
H->timestamp_format = str;
11261123
}
11271124
return true;
11281125

@@ -1243,15 +1240,27 @@ static int pdo_firebird_get_attribute(pdo_dbh_t *dbh, zend_long attr, zval *val)
12431240
return 1;
12441241

12451242
case PDO_FB_ATTR_DATE_FORMAT:
1246-
ZVAL_STRING(val, H->date_format ? H->date_format : PDO_FB_DEF_DATE_FMT);
1243+
if (H->date_format) {
1244+
ZVAL_STR_COPY(val, H->date_format);
1245+
} else {
1246+
ZVAL_STRING(val, PDO_FB_DEF_DATE_FMT);
1247+
}
12471248
return 1;
12481249

12491250
case PDO_FB_ATTR_TIME_FORMAT:
1250-
ZVAL_STRING(val, H->time_format ? H->time_format : PDO_FB_DEF_TIME_FMT);
1251+
if (H->time_format) {
1252+
ZVAL_STR_COPY(val, H->time_format);
1253+
} else {
1254+
ZVAL_STRING(val, PDO_FB_DEF_TIME_FMT);
1255+
}
12511256
return 1;
12521257

12531258
case PDO_FB_ATTR_TIMESTAMP_FORMAT:
1254-
ZVAL_STRING(val, H->timestamp_format ? H->timestamp_format : PDO_FB_DEF_TIMESTAMP_FMT);
1259+
if (H->timestamp_format) {
1260+
ZVAL_STR_COPY(val, H->timestamp_format);
1261+
} else {
1262+
ZVAL_STRING(val, PDO_FB_DEF_TIMESTAMP_FMT);
1263+
}
12551264
return 1;
12561265

12571266
case PDO_FB_TRANSACTION_ISOLATION_LEVEL:

ext/pdo_firebird/firebird_statement.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ static int get_formatted_time_tz(pdo_stmt_t *stmt, const ISC_TIME_TZ* timeTz, zv
9393
}
9494
time = fb_encode_time(hours, minutes, seconds, fractions);
9595
isc_decode_sql_time(&time, &t);
96-
fmt = S->H->time_format ? S->H->time_format : PDO_FB_DEF_TIME_FMT;
96+
fmt = S->H->time_format ? ZSTR_VAL(S->H->time_format) : PDO_FB_DEF_TIME_FMT;
9797

9898
size_t len = strftime(timeBuf, sizeof(timeBuf), fmt, &t);
9999
if (len == 0) {
@@ -123,7 +123,7 @@ static int get_formatted_timestamp_tz(pdo_stmt_t *stmt, const ISC_TIMESTAMP_TZ*
123123
ts.timestamp_time = fb_encode_time(hours, minutes, seconds, fractions);
124124
isc_decode_timestamp(&ts, &t);
125125

126-
fmt = S->H->timestamp_format ? S->H->timestamp_format : PDO_FB_DEF_TIMESTAMP_FMT;
126+
fmt = S->H->timestamp_format ? ZSTR_VAL(S->H->timestamp_format) : PDO_FB_DEF_TIMESTAMP_FMT;
127127

128128
size_t len = strftime(timestampBuf, sizeof(timestampBuf), fmt, &t);
129129
if (len == 0) {
@@ -546,18 +546,18 @@ static int pdo_firebird_stmt_get_col(
546546
break;
547547
case SQL_TYPE_DATE:
548548
isc_decode_sql_date((ISC_DATE*)var->sqldata, &t);
549-
fmt = S->H->date_format ? S->H->date_format : PDO_FB_DEF_DATE_FMT;
549+
fmt = S->H->date_format ? ZSTR_VAL(S->H->date_format) : PDO_FB_DEF_DATE_FMT;
550550
if (0) {
551551
case SQL_TYPE_TIME:
552552
isc_decode_sql_time((ISC_TIME*)var->sqldata, &t);
553-
fmt = S->H->time_format ? S->H->time_format : PDO_FB_DEF_TIME_FMT;
553+
fmt = S->H->time_format ? ZSTR_VAL(S->H->time_format) : PDO_FB_DEF_TIME_FMT;
554554
} else if (0) {
555555
case SQL_TIMESTAMP:
556556
{
557557
ISC_TIMESTAMP timestamp = php_get_isc_timestamp_from_sqldata(var->sqldata);
558558
isc_decode_timestamp(&timestamp, &t);
559559
}
560-
fmt = S->H->timestamp_format ? S->H->timestamp_format : PDO_FB_DEF_TIMESTAMP_FMT;
560+
fmt = S->H->timestamp_format ? ZSTR_VAL(S->H->timestamp_format) : PDO_FB_DEF_TIMESTAMP_FMT;
561561
}
562562
/* convert the timestamp into a string */
563563
char buf[80];

ext/pdo_firebird/php_pdo_firebird_int.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@ typedef struct {
7373
zend_ulong txn_isolation_level;
7474

7575
/* date and time format strings, can be set by the set_attribute method */
76-
char *date_format;
77-
char *time_format;
78-
char *timestamp_format;
76+
zend_string *date_format;
77+
zend_string *time_format;
78+
zend_string *timestamp_format;
7979

8080
unsigned sql_dialect:2;
8181

0 commit comments

Comments
 (0)