From 38254f8916b34a642f2a2f1fbb56f72b8d24a6a6 Mon Sep 17 00:00:00 2001 From: yitam Date: Fri, 19 Nov 2021 15:47:23 -0800 Subject: [PATCH 1/7] Reset sql types and column size for input params --- source/shared/core_stmt.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/source/shared/core_stmt.cpp b/source/shared/core_stmt.cpp index 6cf50d498..2dab23f40 100644 --- a/source/shared/core_stmt.cpp +++ b/source/shared/core_stmt.cpp @@ -395,6 +395,11 @@ void core_sqlsrv_bind_param( _Inout_ sqlsrv_stmt* stmt, _In_ SQLUSMALLINT param_ stmt->params_container.insert_param(param_num, new_param); param_ptr = new_param; new_param.transferred(); + } else if (direction == SQL_PARAM_INPUT && param_ptr->sql_data_type != SQL_SS_TABLE) { + // reset the followings for regular input parameters + param_ptr->sql_data_type = sql_type; + param_ptr->column_size = column_size; + param_ptr->decimal_digits = decimal_digits; } SQLSRV_ASSERT(param_ptr != NULL, "core_sqlsrv_bind_param: param_ptr is null. Something went wrong."); From 220d895e9d2f86aac5a4d4c0b06c67ae6545b59b Mon Sep 17 00:00:00 2001 From: yitam Date: Fri, 19 Nov 2021 17:53:34 -0800 Subject: [PATCH 2/7] Added new tests --- source/shared/core_stmt.cpp | 8 +- source/sqlsrv/stmt.cpp | 4 +- .../pdo_1329_string_truncation.phpt | 101 ++++++++++++++++++ .../sqlsrv/srv_1329_string_truncation.phpt | 94 ++++++++++++++++ 4 files changed, 203 insertions(+), 4 deletions(-) create mode 100644 test/functional/pdo_sqlsrv/pdo_1329_string_truncation.phpt create mode 100644 test/functional/sqlsrv/srv_1329_string_truncation.phpt diff --git a/source/shared/core_stmt.cpp b/source/shared/core_stmt.cpp index 2dab23f40..29511a822 100644 --- a/source/shared/core_stmt.cpp +++ b/source/shared/core_stmt.cpp @@ -395,11 +395,13 @@ void core_sqlsrv_bind_param( _Inout_ sqlsrv_stmt* stmt, _In_ SQLUSMALLINT param_ stmt->params_container.insert_param(param_num, new_param); param_ptr = new_param; new_param.transferred(); - } else if (direction == SQL_PARAM_INPUT && param_ptr->sql_data_type != SQL_SS_TABLE) { - // reset the followings for regular input parameters + } else if (direction == SQL_PARAM_INPUT + && param_ptr->sql_data_type != SQL_SS_TABLE + && param_ptr->strlen_or_indptr == SQL_NULL_DATA) { + // reset the followings for regular input parameters if it was bound as a null param before param_ptr->sql_data_type = sql_type; param_ptr->column_size = column_size; - param_ptr->decimal_digits = decimal_digits; + param_ptr->strlen_or_indptr = 0; } SQLSRV_ASSERT(param_ptr != NULL, "core_sqlsrv_bind_param: param_ptr is null. Something went wrong."); diff --git a/source/sqlsrv/stmt.cpp b/source/sqlsrv/stmt.cpp index 609209c32..c4f174cfb 100644 --- a/source/sqlsrv/stmt.cpp +++ b/source/sqlsrv/stmt.cpp @@ -1187,7 +1187,7 @@ void bind_params( _Inout_ ss_sqlsrv_stmt* stmt ) try { - stmt->free_param_data(); + // stmt->free_param_data(); stmt->executed = false; @@ -1265,6 +1265,8 @@ void bind_params( _Inout_ ss_sqlsrv_stmt* stmt ) } ZEND_HASH_FOREACH_END(); } catch( core::CoreException& ) { + stmt->free_param_data(); + SQLFreeStmt( stmt->handle(), SQL_RESET_PARAMS ); zval_ptr_dtor( stmt->params_z ); sqlsrv_free( stmt->params_z ); diff --git a/test/functional/pdo_sqlsrv/pdo_1329_string_truncation.phpt b/test/functional/pdo_sqlsrv/pdo_1329_string_truncation.phpt new file mode 100644 index 000000000..bb520cd0a --- /dev/null +++ b/test/functional/pdo_sqlsrv/pdo_1329_string_truncation.phpt @@ -0,0 +1,101 @@ +--TEST-- +GitHub issue 1329 - string truncation error when binding some parameters as non-nulls the second time +--DESCRIPTION-- +The test shows the same parameters, though bound as nulls in the first insertion, can be bound as non-nulls in the subsequent insertions. +--ENV-- +PHPT_EXEC=true +--SKIPIF-- + +--FILE-- +exec($drop); +} + +try { + $conn = new PDO("sqlsrv:server=$server; Database = $databaseName;", $uid, $pwd); + $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + + dropTable($conn, 'domains'); + +$tsql = <<exec($tsql); + +$tsql = <<prepare($tsql); + $authority = 'foo.com'; + $base = null; + $notFound = null; + $invalid = null; + $stmt->bindParam(1, $authority); + $stmt->bindParam(2, $base); + $stmt->bindParam(3, $notFound); + $stmt->bindParam(4, $invalid); + $stmt->execute(); + + $authority = 'detached-with-redirects.com'; + $base = 'foo.com'; + $notFound = 'bar.com'; + $invalid = null; + $stmt->bindParam(1, $authority); + $stmt->bindParam(2, $base); + $stmt->bindParam(3, $notFound); + $stmt->bindParam(4, $invalid); + $stmt->execute(); + + // fetch the data + $stmt = $conn->prepare("SELECT * FROM domains"); + $stmt->execute(); + $row = $stmt->fetchAll(PDO::FETCH_NUM); + print_r($row); + + dropTable($conn, 'domains'); + + echo "Done\n"; +} catch (PdoException $e) { + echo $e->getMessage(); +} + +?> +--EXPECT-- +Array +( + [0] => Array + ( + [0] => 1 + [1] => foo.com + [2] => + [3] => + [4] => + ) + + [1] => Array + ( + [0] => 2 + [1] => detached-with-redirects.com + [2] => foo.com + [3] => bar.com + [4] => + ) + +) +Done + + diff --git a/test/functional/sqlsrv/srv_1329_string_truncation.phpt b/test/functional/sqlsrv/srv_1329_string_truncation.phpt new file mode 100644 index 000000000..899416c03 --- /dev/null +++ b/test/functional/sqlsrv/srv_1329_string_truncation.phpt @@ -0,0 +1,94 @@ +--TEST-- +GitHub issue 1329 - string truncation error when binding some parameters as non-nulls the second time +--DESCRIPTION-- +The test shows the same parameters, though bound as nulls in the first insertion, can be bound as non-nulls in the subsequent insertions. +--ENV-- +PHPT_EXEC=true +--SKIPIF-- + +--FILE-- + +--EXPECT-- +Array +( + [0] => 1 + [1] => foo.com + [2] => + [3] => + [4] => +) +Array +( + [0] => 2 + [1] => detached-with-redirects.com + [2] => foo.com + [3] => bar.com + [4] => +) +Done \ No newline at end of file From e2244cec428c1da9ff99536e78bc5f629af504dd Mon Sep 17 00:00:00 2001 From: yitam Date: Mon, 22 Nov 2021 10:07:38 -0800 Subject: [PATCH 3/7] Extended the tests --- .../pdo_1329_string_truncation.phpt | 19 +++++++++++++++++++ .../sqlsrv/srv_1329_string_truncation.phpt | 17 +++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/test/functional/pdo_sqlsrv/pdo_1329_string_truncation.phpt b/test/functional/pdo_sqlsrv/pdo_1329_string_truncation.phpt index bb520cd0a..66b14697b 100644 --- a/test/functional/pdo_sqlsrv/pdo_1329_string_truncation.phpt +++ b/test/functional/pdo_sqlsrv/pdo_1329_string_truncation.phpt @@ -60,6 +60,16 @@ INSERTSQL; $stmt->bindParam(4, $invalid); $stmt->execute(); + $authority = 'other-redirects.com'; + $base = 'foobar.com'; + $notFound = null; + $invalid = 'none'; + $stmt->bindParam(1, $authority); + $stmt->bindParam(2, $base); + $stmt->bindParam(3, $notFound); + $stmt->bindParam(4, $invalid); + $stmt->execute(); + // fetch the data $stmt = $conn->prepare("SELECT * FROM domains"); $stmt->execute(); @@ -95,6 +105,15 @@ Array [4] => ) + [2] => Array + ( + [0] => 3 + [1] => other-redirects.com + [2] => foobar.com + [3] => + [4] => none + ) + ) Done diff --git a/test/functional/sqlsrv/srv_1329_string_truncation.phpt b/test/functional/sqlsrv/srv_1329_string_truncation.phpt index 899416c03..18ac1c652 100644 --- a/test/functional/sqlsrv/srv_1329_string_truncation.phpt +++ b/test/functional/sqlsrv/srv_1329_string_truncation.phpt @@ -56,6 +56,15 @@ if (!$result) { fatalError("failed to execute the insert statement (2)"); } +$authority = 'other-redirects.com'; +$base = 'foobar.com'; +$notFound = null; +$invalid = 'none'; +$result = sqlsrv_execute($stmt); +if (!$result) { + fatalError("failed to execute the insert statement (3)"); +} + // fetch the data $tsql = "SELECT * FROM srv_domains"; $stmt = sqlsrv_query($conn, $tsql); @@ -91,4 +100,12 @@ Array [3] => bar.com [4] => ) +Array +( + [0] => 3 + [1] => other-redirects.com + [2] => foobar.com + [3] => + [4] => none +) Done \ No newline at end of file From 261979c82b039a22f0c169902f56920ae9849f23 Mon Sep 17 00:00:00 2001 From: yitam Date: Mon, 22 Nov 2021 10:14:21 -0800 Subject: [PATCH 4/7] Cleaned up --- source/sqlsrv/stmt.cpp | 2 -- .../pdo_sqlsrv/pdo_1329_string_truncation.phpt | 2 -- test/functional/sqlsrv/srv_1329_string_truncation.phpt | 10 +--------- 3 files changed, 1 insertion(+), 13 deletions(-) diff --git a/source/sqlsrv/stmt.cpp b/source/sqlsrv/stmt.cpp index c4f174cfb..100273fa3 100644 --- a/source/sqlsrv/stmt.cpp +++ b/source/sqlsrv/stmt.cpp @@ -1187,8 +1187,6 @@ void bind_params( _Inout_ ss_sqlsrv_stmt* stmt ) try { - // stmt->free_param_data(); - stmt->executed = false; zval* params_z = stmt->params_z; diff --git a/test/functional/pdo_sqlsrv/pdo_1329_string_truncation.phpt b/test/functional/pdo_sqlsrv/pdo_1329_string_truncation.phpt index 66b14697b..c71f2a1cb 100644 --- a/test/functional/pdo_sqlsrv/pdo_1329_string_truncation.phpt +++ b/test/functional/pdo_sqlsrv/pdo_1329_string_truncation.phpt @@ -116,5 +116,3 @@ Array ) Done - - diff --git a/test/functional/sqlsrv/srv_1329_string_truncation.phpt b/test/functional/sqlsrv/srv_1329_string_truncation.phpt index 18ac1c652..a26a55edf 100644 --- a/test/functional/sqlsrv/srv_1329_string_truncation.phpt +++ b/test/functional/sqlsrv/srv_1329_string_truncation.phpt @@ -100,12 +100,4 @@ Array [3] => bar.com [4] => ) -Array -( - [0] => 3 - [1] => other-redirects.com - [2] => foobar.com - [3] => - [4] => none -) -Done \ No newline at end of file +Done From 7b95b8c693400dc0acadec29cb0039a1ff6eeae7 Mon Sep 17 00:00:00 2001 From: yitam Date: Mon, 22 Nov 2021 10:31:59 -0800 Subject: [PATCH 5/7] Updated srv test --- test/functional/sqlsrv/srv_1329_string_truncation.phpt | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/functional/sqlsrv/srv_1329_string_truncation.phpt b/test/functional/sqlsrv/srv_1329_string_truncation.phpt index a26a55edf..1dfc32029 100644 --- a/test/functional/sqlsrv/srv_1329_string_truncation.phpt +++ b/test/functional/sqlsrv/srv_1329_string_truncation.phpt @@ -100,4 +100,12 @@ Array [3] => bar.com [4] => ) +Array +( + [0] => 3 + [1] => other-redirects.com + [2] => foobar.com + [3] => + [4] => none +) Done From 1bf3a5521fe52f1faf10fc6ae1f91106d2f07f41 Mon Sep 17 00:00:00 2001 From: yitam Date: Mon, 22 Nov 2021 12:35:25 -0800 Subject: [PATCH 6/7] Updated tests to use different characters --- .../pdo_1329_string_truncation.phpt | 24 +++++++++---------- .../sqlsrv/srv_1329_string_truncation.phpt | 24 +++++++++---------- 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/test/functional/pdo_sqlsrv/pdo_1329_string_truncation.phpt b/test/functional/pdo_sqlsrv/pdo_1329_string_truncation.phpt index c71f2a1cb..7b1faf291 100644 --- a/test/functional/pdo_sqlsrv/pdo_1329_string_truncation.phpt +++ b/test/functional/pdo_sqlsrv/pdo_1329_string_truncation.phpt @@ -50,9 +50,9 @@ INSERTSQL; $stmt->bindParam(4, $invalid); $stmt->execute(); - $authority = 'detached-with-redirects.com'; - $base = 'foo.com'; - $notFound = 'bar.com'; + $authority = 'detached-with-ředirects.com'; + $base = 'fŏő.com'; + $notFound = 'baŗ.com'; $invalid = null; $stmt->bindParam(1, $authority); $stmt->bindParam(2, $base); @@ -60,10 +60,10 @@ INSERTSQL; $stmt->bindParam(4, $invalid); $stmt->execute(); - $authority = 'other-redirects.com'; - $base = 'foobar.com'; + $authority = 'Őther-redirects.com'; + $base = 'fooš.com'; $notFound = null; - $invalid = 'none'; + $invalid = 'ŷëå'; $stmt->bindParam(1, $authority); $stmt->bindParam(2, $base); $stmt->bindParam(3, $notFound); @@ -99,19 +99,19 @@ Array [1] => Array ( [0] => 2 - [1] => detached-with-redirects.com - [2] => foo.com - [3] => bar.com + [1] => detached-with-ředirects.com + [2] => fŏő.com + [3] => baŗ.com [4] => ) [2] => Array ( [0] => 3 - [1] => other-redirects.com - [2] => foobar.com + [1] => Őther-redirects.com + [2] => fooš.com [3] => - [4] => none + [4] => ŷëå ) ) diff --git a/test/functional/sqlsrv/srv_1329_string_truncation.phpt b/test/functional/sqlsrv/srv_1329_string_truncation.phpt index 1dfc32029..9ab8cc585 100644 --- a/test/functional/sqlsrv/srv_1329_string_truncation.phpt +++ b/test/functional/sqlsrv/srv_1329_string_truncation.phpt @@ -47,19 +47,19 @@ if (!$result) { fatalError("failed to execute the insert statement (1)"); } -$authority = 'detached-with-redirects.com'; -$base = 'foo.com'; -$notFound = 'bar.com'; +$authority = 'detached-with-ředirects.com'; +$base = 'fŏő.com'; +$notFound = 'baŗ.com'; $invalid = null; $result = sqlsrv_execute($stmt); if (!$result) { fatalError("failed to execute the insert statement (2)"); } -$authority = 'other-redirects.com'; -$base = 'foobar.com'; +$authority = 'Őther-redirects.com'; +$base = 'fooš.com'; $notFound = null; -$invalid = 'none'; +$invalid = 'ŷëå'; $result = sqlsrv_execute($stmt); if (!$result) { fatalError("failed to execute the insert statement (3)"); @@ -95,17 +95,17 @@ Array Array ( [0] => 2 - [1] => detached-with-redirects.com - [2] => foo.com - [3] => bar.com + [1] => detached-with-ředirects.com + [2] => fŏő.com + [3] => baŗ.com [4] => ) Array ( [0] => 3 - [1] => other-redirects.com - [2] => foobar.com + [1] => Őther-redirects.com + [2] => fooš.com [3] => - [4] => none + [4] => ŷëå ) Done From 0a92180df813569f1badfd7d56ebb624332f6988 Mon Sep 17 00:00:00 2001 From: yitam Date: Mon, 22 Nov 2021 13:19:30 -0800 Subject: [PATCH 7/7] Specified utf8 for the srv test --- test/functional/sqlsrv/srv_1329_string_truncation.phpt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/functional/sqlsrv/srv_1329_string_truncation.phpt b/test/functional/sqlsrv/srv_1329_string_truncation.phpt index 9ab8cc585..1395c92f8 100644 --- a/test/functional/sqlsrv/srv_1329_string_truncation.phpt +++ b/test/functional/sqlsrv/srv_1329_string_truncation.phpt @@ -10,7 +10,8 @@ PHPT_EXEC=true 'UTF-8'); +$conn = AE\connect($connectionInfo); dropTable($conn, 'srv_domains');