Skip to content

Commit fa751c7

Browse files
committed
Fixed an issue where pdo_firebird float and double type values were wrong. Changed from using `%F` format with `zend_strpprintf` to using `%H` format with `zend_strpprintf_unchecked`. Fixes phpGH-13119 Closes phpGH-13125
1 parent cd483f1 commit fa751c7

File tree

4 files changed

+94
-8
lines changed

4 files changed

+94
-8
lines changed

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ PHP NEWS
1414
. Fixed LibreSSL undefined reference when OPENSSL_NO_ENGINE not set.
1515
(David Carlier).
1616

17+
- PDO_Firebird:
18+
. Fix GH-13119 (Changed to convert float and double values ​​into strings using
19+
`H` format). (SakiTakamachi)
20+
1721
- Phar:
1822
. Fixed bug #71465 (PHAR doesn't know about litespeed). (nielsdos)
1923

ext/pdo_firebird/firebird_statement.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ static zend_always_inline double get_double_from_sqldata(const ISC_SCHAR *sqldat
5151
READ_AND_RETURN_USING_MEMCPY(double, sqldata);
5252
}
5353

54+
static zend_always_inline float get_float_from_sqldata(const ISC_SCHAR *sqldata)
55+
{
56+
READ_AND_RETURN_USING_MEMCPY(float, sqldata);
57+
}
58+
5459
static zend_always_inline ISC_TIMESTAMP get_isc_timestamp_from_sqldata(const ISC_SCHAR *sqldata)
5560
{
5661
READ_AND_RETURN_USING_MEMCPY(ISC_TIMESTAMP, sqldata);
@@ -459,11 +464,11 @@ static int firebird_stmt_get_col(
459464
break;
460465
case SQL_FLOAT:
461466
/* TODO: Why is this not returned as the native type? */
462-
ZVAL_STR(result, zend_strpprintf(0, "%F", *(float*)var->sqldata));
467+
ZVAL_STR(result, zend_strpprintf_unchecked(0, "%.8H", get_float_from_sqldata(var->sqldata)));
463468
break;
464469
case SQL_DOUBLE:
465470
/* TODO: Why is this not returned as the native type? */
466-
ZVAL_STR(result, zend_strpprintf(0, "%F", get_double_from_sqldata(var->sqldata)));
471+
ZVAL_STR(result, zend_strpprintf_unchecked(0, "%.16H", get_double_from_sqldata(var->sqldata)));
467472
break;
468473
#ifdef SQL_BOOLEAN
469474
case SQL_BOOLEAN:

ext/pdo_firebird/tests/gh10908.phpt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ Array
7979

8080
Array
8181
(
82-
[DBL] => 1.000000
83-
[0] => 1.000000
82+
[DBL] => 1
83+
[0] => 1
8484
)
8585

8686
Array
@@ -103,10 +103,10 @@ Array
103103
[1] => ABC
104104
[NUM] => 12.340
105105
[2] => 12.340
106-
[DBL] => 1.000000
107-
[3] => 1.000000
108-
[FLT] => 2.000000
109-
[4] => 2.000000
106+
[DBL] => 1
107+
[3] => 1
108+
[FLT] => 2
109+
[4] => 2
110110
[TS] => 2023-03-24 17:39:00
111111
[5] => 2023-03-24 17:39:00
112112
[MYDATE] => 2023-03-24

ext/pdo_firebird/tests/gh13119.phpt

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
--TEST--
2+
GH-13119 (float, double value is incorrect)
3+
--EXTENSIONS--
4+
pdo_firebird
5+
--SKIPIF--
6+
<?php require('skipif.inc'); ?>
7+
--XLEAK--
8+
A bug in firebird causes a memory leak when calling `isc_attach_database()`.
9+
See https://github.com/FirebirdSQL/firebird/issues/7849
10+
--FILE--
11+
<?php
12+
13+
require("testdb.inc");
14+
15+
$dbh->exec('CREATE TABLE gh13119 (f_val FLOAT, d_val DOUBLE PRECISION)');
16+
17+
$dbh->exec('INSERT INTO gh13119 VALUES (0.1, 0.1)');
18+
$dbh->exec('INSERT INTO gh13119 VALUES (0.0000000000000001, 0.0000000000000001)');
19+
$dbh->exec('INSERT INTO gh13119 VALUES (12.000000, 12.00000000000000)');
20+
$dbh->exec('INSERT INTO gh13119 VALUES (12.000001, 12.00000000000001)');
21+
$dbh->exec('INSERT INTO gh13119 VALUES (12.345678, 12.34567890123456)');
22+
$dbh->exec('INSERT INTO gh13119 VALUES (0.0000000000000000012345678, 0.000000000000000001234567890123456)');
23+
24+
$stmt = $dbh->query('select * from gh13119');
25+
var_dump($stmt->fetchAll(PDO::FETCH_ASSOC));
26+
?>
27+
--CLEAN--
28+
<?php
29+
require 'testdb.inc';
30+
@$dbh->exec('DROP TABLE gh13119');
31+
unset($dbh);
32+
?>
33+
--EXPECT--
34+
array(6) {
35+
[0]=>
36+
array(2) {
37+
["F_VAL"]=>
38+
string(3) "0.1"
39+
["D_VAL"]=>
40+
string(3) "0.1"
41+
}
42+
[1]=>
43+
array(2) {
44+
["F_VAL"]=>
45+
string(7) "1.0E-16"
46+
["D_VAL"]=>
47+
string(7) "1.0E-16"
48+
}
49+
[2]=>
50+
array(2) {
51+
["F_VAL"]=>
52+
string(2) "12"
53+
["D_VAL"]=>
54+
string(2) "12"
55+
}
56+
[3]=>
57+
array(2) {
58+
["F_VAL"]=>
59+
string(9) "12.000001"
60+
["D_VAL"]=>
61+
string(17) "12.00000000000001"
62+
}
63+
[4]=>
64+
array(2) {
65+
["F_VAL"]=>
66+
string(9) "12.345678"
67+
["D_VAL"]=>
68+
string(17) "12.34567890123456"
69+
}
70+
[5]=>
71+
array(2) {
72+
["F_VAL"]=>
73+
string(13) "1.2345678E-18"
74+
["D_VAL"]=>
75+
string(21) "1.234567890123456E-18"
76+
}
77+
}

0 commit comments

Comments
 (0)