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

Initialize output param buffer when allocating extra space #907

Merged
merged 4 commits into from
Dec 17, 2018
Merged
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
11 changes: 5 additions & 6 deletions source/shared/core_stmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2770,10 +2770,10 @@ void resize_output_buffer_if_necessary( _Inout_ sqlsrv_stmt* stmt, _Inout_ zval*
{
SQLSRV_ASSERT( column_size != SQLSRV_UNKNOWN_SIZE, "column size should be set to a known value." );
buffer_len = Z_STRLEN_P( param_z );
SQLLEN original_len = buffer_len;
SQLLEN expected_len;
SQLLEN buffer_null_extra;
SQLLEN elem_size;
SQLLEN without_null_len;

// calculate the size of each 'element' represented by column_size. WCHAR is of course 2,
// as is a n(var)char/ntext field being returned as a binary field.
Expand Down Expand Up @@ -2801,9 +2801,6 @@ void resize_output_buffer_if_necessary( _Inout_ sqlsrv_stmt* stmt, _Inout_ zval*
// binary fields aren't null terminated, so we need to account for that in our buffer length calcuations
buffer_null_extra = (c_type == SQL_C_BINARY) ? elem_size : 0;

// this is the size of the string for Zend and for the StrLen parameter to SQLBindParameter
without_null_len = field_size * elem_size;

// increment to include the null terminator since the Zend length doesn't include the null terminator
buffer_len += elem_size;

Expand All @@ -2821,8 +2818,10 @@ void resize_output_buffer_if_necessary( _Inout_ sqlsrv_stmt* stmt, _Inout_ zval*
// A zval string len doesn't include the null. This calculates the length it should be
// regardless of whether the ODBC type contains the NULL or not.

// null terminate the string to avoid a warning in debug PHP builds
ZSTR_VAL(param_z_string)[without_null_len] = '\0';
// initialize the newly allocated space
char *p = ZSTR_VAL(param_z_string);
p = p + original_len;
memset(p, '\0', expected_len - original_len);
ZVAL_NEW_STR(param_z, param_z_string);

// buffer_len is the length passed to SQLBindParameter. It must contain the space for NULL in the
Expand Down
85 changes: 85 additions & 0 deletions test/functional/pdo_sqlsrv/pdo_900_output_param_memory_data.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
--TEST--
GitHub issue 900 - output parameter displays data from memory when not finalized
--DESCRIPTION--
This test verifies that when there is an active resultset and output parameter not finalized, it should not show any data from client memory. This test does not work with AlwaysEncrypted because
the output param is not assigned in the stored procedure.
--ENV--
PHPT_EXEC=true
--SKIPIF--
<?php require('skipif_mid-refactor.inc'); ?>
--FILE--
<?php
require_once("MsSetup.inc");
require_once("MsCommon_mid-refactor.inc");

$size = 30;

function getOutputParam($conn, $storedProcName, $dataType, $inout)
{
global $size;

try {
$output = null;

$stmt = $conn->prepare("$storedProcName @OUTPUT = :output");
// $stmt->bindParam('output', $output, PDO::PARAM_STR, $size);
if ($inout) {
$paramType = PDO::PARAM_STR | PDO::PARAM_INPUT_OUTPUT;
} else {
$paramType = PDO::PARAM_STR;
}
$stmt->bindParam('output', $output, $paramType, $size);

$stmt->execute();

// the output param should be doubled in size for wide characters
// however, it should not contain any data so after trimming it
// should be merely an empty string
$len = strlen($output);
$result = trim($output);

if ($len != ($size * 2) || $result !== "" ) {
echo "Unexpected output param for $dataType: ";
var_dump($output);
}

$stmt->closeCursor();
if (!is_null($output)) {
echo "Output param should be null when finalized!";
}
unset($stmt);
} catch (PdoException $e) {
echo $e->getMessage() . PHP_EOL;
}
}

try {
// This helper method sets PDO::ATTR_ERRMODE to PDO::ERRMODE_EXCEPTION
// $conn = connect();
$conn = new PDO( "sqlsrv:server=$server; Database = $databaseName", $uid, $pwd);
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

$dataTypes = array("VARCHAR(512)", "VARCHAR(max)", "NVARCHAR(512)", "NVARCHAR(max)");
for ($i = 0, $p = 3; $i < count($dataTypes); $i++, $p++) {
// Create the stored procedure first
$storedProcName = "spNullOutputParam" . $i;
$procArgs = "@OUTPUT $dataTypes[$i] OUTPUT";
$procCode = "SELECT 1, 2, 3";

createProc($conn, $storedProcName, $procArgs, $procCode);
getOutputParam($conn, $storedProcName, $dataTypes[$i], false);
getOutputParam($conn, $storedProcName, $dataTypes[$i], true);

// Drop the stored procedure
dropProc($conn, $storedProcName);
}

echo "Done\n";

unset($conn);
} catch (PdoException $e) {
echo $e->getMessage() . PHP_EOL;
}
?>
--EXPECT--
Done
81 changes: 81 additions & 0 deletions test/functional/sqlsrv/sqlsrv_900_output_param_memory_data.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
--TEST--
GitHub issue 900 - output parameter displays data from memory when not finalized
--DESCRIPTION--
This test verifies that when there is an active resultset and output parameter not finalized, it should not show any data from client memory. This test does not work with AlwaysEncrypted because
the output param is not assigned in the stored procedure.
--ENV--
PHPT_EXEC=true
--SKIPIF--
<?php require('skipif_versions_old.inc'); ?>
--FILE--
<?php
require_once('MsCommon.inc');

$size = 30;

function getOutputParam($conn, $storedProcName, $inout, $isVarchar, $isMax)
{
global $size;

$output = null;
$dir = ($inout)? SQLSRV_PARAM_INOUT : SQLSRV_PARAM_OUT;
$dataType = ($isVarchar)? "SQLSRV_SQLTYPE_VARCHAR" : "SQLSRV_SQLTYPE_NVARCHAR";
$sqlType = ($isMax)? call_user_func($dataType, 'max') : call_user_func($dataType, $size);

$stmt = sqlsrv_prepare($conn, "$storedProcName @OUTPUT = ?", array(array(&$output, $dir, null, $sqlType)));
if (!$stmt) {
fatalError("getOutputParam: failed when preparing to call $storedProcName");
}
if (!sqlsrv_execute($stmt)) {
fatalError("getOutputParam: failed to execute procedure $storedProcName");
}

// The output param should be doubled in size for wide characters;
// for max fields it should be the maximum anticipated size 8000.
// However, it should not contain any data so after trimming it
// should be merely an empty string
$len = strlen($output);
$expectedLen = ($isMax)? 8000 : ($size * 2);
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is there no comparison to the max size of 8000 in the pdo test?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

good point! I should redesign the sqlsrv test to fix the size like the pdo test

$result = trim($output);

if ($len != $expectedLen || $result !== "" ) {
echo "Unexpected output param for $dataType, $isMax: ";
var_dump($output);
}

sqlsrv_next_result($stmt);
if (!is_null($output)) {
echo "Output param should be null when finalized!";
}
}

set_time_limit(0);
sqlsrv_configure('WarningsReturnAsErrors', 1);

$conn = connect(array("CharacterSet" => "UTF-8"));
if (!$conn) {
fatalError("Could not connect.\n");
}

$dataTypes = array("VARCHAR(512)", "VARCHAR(max)", "NVARCHAR(512)", "NVARCHAR(max)");
for ($i = 0, $p = 3; $i < count($dataTypes); $i++, $p++) {
// Create the stored procedure first
$storedProcName = "spNullOutputParam" . $i;
$procArgs = "@OUTPUT $dataTypes[$i] OUTPUT";
$procCode = "SELECT 1, 2, 3";

createProc($conn, $storedProcName, $procArgs, $procCode);
getOutputParam($conn, $storedProcName, false, ($i < 2), ($i % 2 != 0));
getOutputParam($conn, $storedProcName, true, ($i < 2), ($i % 2 != 0));

// Drop the stored procedure
dropProc($conn, $storedProcName);
}

echo "Done\n";

sqlsrv_close($conn);

?>
--EXPECT--
Done