From c03ae20df3d5b86a75b6d93f286e1fe9aef0731b Mon Sep 17 00:00:00 2001 From: Saki Takamachi Date: Tue, 23 Jul 2024 00:09:23 +0900 Subject: [PATCH] Fixed segmentation fault when attribute value was not set --- ext/pdo_firebird/firebird_driver.c | 6 +-- .../tests/attr_datetime_format.phpt | 40 +++++++++++++++++++ 2 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 ext/pdo_firebird/tests/attr_datetime_format.phpt diff --git a/ext/pdo_firebird/firebird_driver.c b/ext/pdo_firebird/firebird_driver.c index beeb4352940eb..abfbca1f9d730 100644 --- a/ext/pdo_firebird/firebird_driver.c +++ b/ext/pdo_firebird/firebird_driver.c @@ -1193,15 +1193,15 @@ static int pdo_firebird_get_attribute(pdo_dbh_t *dbh, zend_long attr, zval *val) return 1; case PDO_FB_ATTR_DATE_FORMAT: - ZVAL_STRING(val, H->date_format); + ZVAL_STRING(val, H->date_format ? H->date_format : PDO_FB_DEF_DATE_FMT); return 1; case PDO_FB_ATTR_TIME_FORMAT: - ZVAL_STRING(val, H->time_format); + ZVAL_STRING(val, H->time_format ? H->time_format : PDO_FB_DEF_TIME_FMT); return 1; case PDO_FB_ATTR_TIMESTAMP_FORMAT: - ZVAL_STRING(val, H->timestamp_format); + ZVAL_STRING(val, H->timestamp_format ? H->timestamp_format : PDO_FB_DEF_TIMESTAMP_FMT); return 1; case PDO_FB_TRANSACTION_ISOLATION_LEVEL: diff --git a/ext/pdo_firebird/tests/attr_datetime_format.phpt b/ext/pdo_firebird/tests/attr_datetime_format.phpt new file mode 100644 index 0000000000000..6daa6cd66705f --- /dev/null +++ b/ext/pdo_firebird/tests/attr_datetime_format.phpt @@ -0,0 +1,40 @@ +--TEST-- +PDO_Firebird: attr date, time, and timestamp formats +--EXTENSIONS-- +pdo_firebird +--SKIPIF-- + +--XLEAK-- +A bug in firebird causes a memory leak when calling `isc_attach_database()`. +See https://github.com/FirebirdSQL/firebird/issues/7849 +--FILE-- +getAttribute(Pdo\firebird::ATTR_DATE_FORMAT), "\n"; +echo 'ATTR_TIME_FORMAT: ', $dbh->getAttribute(Pdo\firebird::ATTR_TIME_FORMAT), "\n"; +echo 'ATTR_TIMESTAMP_FORMAT: ', $dbh->getAttribute(Pdo\firebird::ATTR_TIMESTAMP_FORMAT), "\n"; + +$dbh->setAttribute(Pdo\firebird::ATTR_DATE_FORMAT, 'Y----m----d'); +$dbh->setAttribute(Pdo\firebird::ATTR_TIME_FORMAT, 'H::::i::::s'); +$dbh->setAttribute(Pdo\firebird::ATTR_TIMESTAMP_FORMAT, 'Y----m----d....H::::i::::s'); + +echo "\n"; + +echo "== State after setting value. ==\n"; +echo 'ATTR_DATE_FORMAT: ', $dbh->getAttribute(Pdo\firebird::ATTR_DATE_FORMAT), "\n"; +echo 'ATTR_TIME_FORMAT: ', $dbh->getAttribute(Pdo\firebird::ATTR_TIME_FORMAT), "\n"; +echo 'ATTR_TIMESTAMP_FORMAT: ', $dbh->getAttribute(Pdo\firebird::ATTR_TIMESTAMP_FORMAT), "\n"; +?> +--EXPECT-- +== Default state with nothing set. == +ATTR_DATE_FORMAT: %Y-%m-%d +ATTR_TIME_FORMAT: %H:%M:%S +ATTR_TIMESTAMP_FORMAT: %Y-%m-%d %H:%M:%S + +== State after setting value. == +ATTR_DATE_FORMAT: Y----m----d +ATTR_TIME_FORMAT: H::::i::::s +ATTR_TIMESTAMP_FORMAT: Y----m----d....H::::i::::s