Skip to content

ext/pdo_firebird: Fixed GH-15604 Always make input parameters nullable #15605

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions ext/pdo_firebird/firebird_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,15 @@ static int php_firebird_preprocess(const zend_string* sql, char* sql_out, HashTa
return 1;
}

static void set_nullable_input_params(XSQLDA* sqlda)
{
unsigned int i;
XSQLVAR* var;
for (i=0, var = sqlda->sqlvar; i < sqlda->sqld; i++, var++) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit CS

Suggested change
for (i=0, var = sqlda->sqlvar; i < sqlda->sqld; i++, var++) {
for (i = 0, var = sqlda->sqlvar; i < sqlda->sqld; i++, var++) {

var->sqltype |= 1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please write in the comments why this is nullable?

}
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's pretty simple, so might want to write this in the caller instead of making it a separate function.

#if FB_API_VER >= 40
/* set coercing a data type */
static void set_coercing_output_data_types(XSQLDA* sqlda)
Expand Down Expand Up @@ -681,6 +690,9 @@ static bool firebird_handle_preparer(pdo_dbh_t *dbh, zend_string *sql, /* {{{ */
if (isc_dsql_describe_bind(H->isc_status, &s, PDO_FB_SQLDA_VERSION, S->in_sqlda)) {
break;
}

// make all parameters nullable
set_nullable_input_params(S->in_sqlda);
}

stmt->driver_data = S;
Expand Down
78 changes: 78 additions & 0 deletions ext/pdo_firebird/tests/bug_15604.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
--TEST--
Bug #15604 It is not possible to pass a NULL value as an input parameter if the field is marked as NOT NULL.
--EXTENSIONS--
pdo_firebird
--SKIPIF--
<?php require('skipif.inc'); ?>
--XLEAK--
A bug in firebird causes a memory leak when calling `isc_attach_database()`.
See https://github.com/FirebirdSQL/firebird/issues/7849
--FILE--
<?php
require_once 'testdb.inc';

$dbh = getDbConnection();

$dbh->exec('
recreate table t_bug_15604 (
id bigint not null,
a int not null,
b int,
constraint pk_bug_15604 primary key(id)
)
');

$dbh->exec('recreate sequence g_bug_15604');

$dbh->exec(<<<'SQL'
create or alter trigger t_bug_15604_bi0 for t_bug_15604
active before insert position 0
as
begin
if (new.id is null) then
new.id = next value for g_bug_15604;
end
SQL
);

$stmt = $dbh->prepare('insert into t_bug_15604(id, a, b) values(?, ?, ?)');
$stmt->execute([null, 1, 2]);
$stmt->execute([2, 2, null]);
unset($stmt);

$stmt2 = $dbh->prepare('SELECT id, a, b FROM t_bug_15604 WHERE id = ?');

$stmt2->execute([null]);
$data = $stmt2->fetch(\PDO::FETCH_ASSOC);
$stmt2->closeCursor();
var_dump($data);

$stmt2->execute([2]);
$data = $stmt2->fetch(\PDO::FETCH_ASSOC);
$stmt2->closeCursor();
var_dump($data);

unset($stmt2);

echo "\nOK\n";
?>
--EXPECT--
bool(false)
array(3) {
["ID"]=>
int(2)
["A"]=>
int(2)
["B"]=>
NULL
}

OK
--CLEAN--
<?php
require_once 'testdb.inc';
$dbh = getDbConnection();
@$dbh->exec('drop table t_bug_15604');
@$dbh->exec('drop sequence g_bug_15604');
unset($dbh);
?>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can CLEAN be written before EXPECT? Other tests do that, so might miss it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Loading