Skip to content
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

Fix empty string bind param causes floating point exception #145

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion source/pdo_sqlsrv/core_stmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1960,7 +1960,7 @@ void default_sql_size_and_scale( sqlsrv_stmt* stmt, unsigned int paramno, zval*
column_size = SQL_SERVER_MAX_TYPE_SIZE;
}
else {
column_size = SQL_SERVER_MAX_FIELD_SIZE / char_size;
column_size = (char_size == 0) ? 1 : (SQL_SERVER_MAX_FIELD_SIZE / char_size);
Copy link
Author

Choose a reason for hiding this comment

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

char_size is computed differently on Windows:

Maybe not multiplying with the parameter string size would be a better fix?
Explaining the reason why the code changed in 4.0.3 would also help.

}
break;
}
Expand Down
30 changes: 30 additions & 0 deletions source/pdo_sqlsrv/tests/bug_141.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
--TEST--
Bug #141 (Assinging empty string as bind parameter produces floating point exception)
--SKIPIF--
<?php if(!extension_loaded("pdo_sqlsrv")) print "skip"; ?>
--INI--
--FILE--
<?php
require('config.inc');

$dbo = new PDO("sqlsrv:server=$serverName ; Database = $database", $username, $password);
print 'PDO sqlsrv connection successfull: '.($dbo ? 'yes' : 'no').PHP_EOL;

$sth = $dbo->prepare('select ?');
print 'PDO sqlsrv prepare successfull: '.($sth !== false ? 'yes' : 'no').PHP_EOL;

$result = $sth->bindValue(1, '');
print 'PDO sqlsrv value binding successfull: '.($result !== false ? 'yes' : 'no').PHP_EOL;

$result = $sth->execute();
print 'PDO sqlsrv execute successfull: '.($result !== false ? 'yes' : 'no').PHP_EOL;

$row = $sth->fetch(PDO::FETCH_NUM);
print 'PDO sqlsrv fetch result 1st column is empty string: ' . ($row[0] === '' ? 'yes' : 'no') . PHP_EOL;
?>
--EXPECT--
PDO sqlsrv connection successfull: yes
PDO sqlsrv prepare successfull: yes
PDO sqlsrv value binding successfull: yes
PDO sqlsrv execute successfull: yes
PDO sqlsrv fetch result 1st column is empty string: yes
6 changes: 6 additions & 0 deletions source/pdo_sqlsrv/tests/config.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
$serverName = getenv('MSSQL_SERVERNAME') ?: 'localhost';
$database = getenv('MSSQL_DATABASE') ?: 'test_db';
$username = getenv('MSSQL_USERNAME') ?: 'test_user';
$password = getenv('MSSQL_PASSWORD') ?: 'test_pass';

2 changes: 1 addition & 1 deletion source/sqlsrv/core_stmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1960,7 +1960,7 @@ void default_sql_size_and_scale( sqlsrv_stmt* stmt, unsigned int paramno, zval*
column_size = SQL_SERVER_MAX_TYPE_SIZE;
}
else {
column_size = SQL_SERVER_MAX_FIELD_SIZE / char_size;
column_size = (char_size == 0) ? 1 : (SQL_SERVER_MAX_FIELD_SIZE / char_size);
}
break;
}
Expand Down
25 changes: 25 additions & 0 deletions source/sqlsrv/tests/bug_144.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
--TEST--
Bug #144 (PHP-7.0-Linux sqlsrv_query() produces floating point exception when bind parameter is a empty string)
--SKIPIF--
<?php if(!extension_loaded("sqlsrv")) print "skip"; ?>
--INI--
--FILE--
<?php
require('config.inc');

$conn = sqlsrv_connect($serverName, ['Database' => $database, 'Uid' => $username, 'PWD' => $password]);
print 'sqlsrv connection successfull: '.($conn !== false ? 'yes' : 'no').PHP_EOL;

$result = sqlsrv_query($conn, 'SELECT ?', ['']);
print 'sqlsrv parametrized query successfull: '.($result !== false ? 'yes' : 'no').PHP_EOL;

$row = sqlsrv_fetch_array($result, SQLSRV_FETCH_NUMERIC);
print 'sqlsrv parametrized query result 1st colum is empty string: ' . ($row[0] === '' ? 'yes' : 'no') . PHP_EOL;

sqlsrv_free_stmt($result);

?>
--EXPECT--
sqlsrv connection successfull: yes
sqlsrv parametrized query successfull: yes
sqlsrv parametrized query result 1st colum is empty string: yes
6 changes: 6 additions & 0 deletions source/sqlsrv/tests/config.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
$serverName = getenv('MSSQL_SERVERNAME') ?: 'localhost';
$database = getenv('MSSQL_DATABASE') ?: 'test_db';
$username = getenv('MSSQL_USERNAME') ?: 'test_user';
$password = getenv('MSSQL_PASSWORD') ?: 'test_pass';