Skip to content

pdo_odbc: allocate sufficient space for retrieving unicode data #15008

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

Open
wants to merge 2 commits into
base: PHP-8.2
Choose a base branch
from
Open
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
13 changes: 13 additions & 0 deletions ext/pdo_odbc/odbc_stmt.c
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,19 @@ static int odbc_stmt_describe(pdo_stmt_t *stmt, int colno)
}
}
colsize = displaysize;
if ( S->cols[colno].coltype == SQL_WCHAR
#ifdef SQL_WVARCHAR
|| S->cols[colno].coltype == SQL_WVARCHAR
#endif
#ifdef SQL_WLONGVARCHAR
|| S->cols[colno].coltype == SQL_WLONGVARCHAR
#endif
) {
/* displaysize is counted by characters;
for unicode, each could take up to 4 bytes in UTF-8;
see https://www.rfc-editor.org/rfc/rfc3629 */
colsize = displaysize * 4;
}

col->maxlen = S->cols[colno].datalen = colsize;
col->name = zend_string_init(S->cols[colno].colname, colnamelen, 0);
Expand Down
87 changes: 87 additions & 0 deletions ext/pdo_odbc/tests/gh9498_1.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
--TEST--
Bug GH-9498 (unicode string corruption during SELECT)
--EXTENSIONS--
pdo_odbc
--SKIPIF--
<?php
require 'ext/pdo/tests/pdo_test.inc';
PDOTest::skip();
?>
--FILE--
<?php
require 'ext/pdo/tests/pdo_test.inc';
$db = PDOTest::test_factory(dirname(__FILE__) . '/common.phpt');
$db->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);

$tests = [
[
"name" => "beamed_eighth_notes_binary_encoded_utf16le",
"expr" => "CAST(0x6b26 AS nvarchar(1))",
"expected_answer" => "♫",
],
[
"name" => "beamed_eighth_notes_nchar",
"expr" => "NCHAR(0x266b)",
"expected_answer" => "♫",
],
[
"name" => "beamed_eighth_notes_u+266b_constant",
"expr" => "N'♫'",
"expected_answer" => "♫",
],
[
"name" => "ligature_fi_nchar",
"expr" => "NCHAR(0xfb01)",
"expected_answer" => "fi",
],
[
"name" => "php_ascii_string_constant",
"expr" => "'php'",
"expected_answer" => "php",
],
[
"name" => "php_unicode_string_constant",
"expr" => "N'php'",
"expected_answer" => "php",
],
[
"name" => "pink_in_czech_binary_encoded_utf16le",
"expr" => "CAST(0x72006f017e016f007600fd00 AS nvarchar(6))",
"expected_answer" => "růžový",
],
[
"name" => "pink_in_czech_unicode_string_constant",
"expr" => "N'růžový'",
"expected_answer" => "růžový",
],
[
"name" => "segno_u+1d10b_constant",
"expr" => "N'𝄋'",
"expected_answer" => "𝄋",
],
];

foreach ($tests as $t) {
print $t["name"] . ": ";
$sql = "SELECT " . $t["expr"] . ' AS "' . $t["name"] . '";';
$results = $db->query($sql, \PDO::FETCH_NUM)->fetch();
if ( $results[0] == $t["expected_answer"] ) {
print "PASS";
} else {
print "FAIL!";
print "\tresult: '" . $results[0] . "'";
print "\texpected: '" . $t["expected_answer"] . "'";
}
print "\n";
}
?>
--EXPECT--
beamed_eighth_notes_binary_encoded_utf16le: PASS
beamed_eighth_notes_nchar: PASS
beamed_eighth_notes_u+266b_constant: PASS
ligature_fi_nchar: PASS
php_ascii_string_constant: PASS
php_unicode_string_constant: PASS
pink_in_czech_binary_encoded_utf16le: PASS
pink_in_czech_unicode_string_constant: PASS
segno_u+1d10b_constant: PASS
Loading