Skip to content

Commit 4df4264

Browse files
mvorisekramsey
authored andcommitted
Fix PDO OCI Bug #60994 (Reading a multibyte CLOB caps at 8192 chars)
1 parent 131b862 commit 4df4264

File tree

3 files changed

+52
-19
lines changed

3 files changed

+52
-19
lines changed

ext/pdo_oci/config.m4

+8
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,14 @@ if test "$PHP_PDO_OCI" != "no"; then
182182
-L$PDO_OCI_LIB_DIR $PDO_OCI_SHARED_LIBADD
183183
])
184184

185+
dnl Can handle bytes vs. characters?
186+
PHP_CHECK_LIBRARY(clntsh, OCILobRead2,
187+
[
188+
AC_DEFINE(HAVE_OCILOBREAD2,1,[ ])
189+
], [], [
190+
-L$PDO_OCI_LIB_DIR $PDO_OCI_SHARED_LIBADD
191+
])
192+
185193
PHP_CHECK_PDO_INCLUDES
186194

187195
PHP_NEW_EXTENSION(pdo_oci, pdo_oci.c oci_driver.c oci_statement.c, $ext_shared,,-I$pdo_cv_inc_path)

ext/pdo_oci/oci_statement.c

+23-9
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,7 @@ struct oci_lob_self {
616616
OCILobLocator *lob;
617617
oci_lob_env *E;
618618
ub4 offset;
619+
ub1 csfrm;
619620
};
620621

621622
static ssize_t oci_blob_write(php_stream *stream, const char *buf, size_t count)
@@ -641,23 +642,34 @@ static ssize_t oci_blob_write(php_stream *stream, const char *buf, size_t count)
641642
static ssize_t oci_blob_read(php_stream *stream, char *buf, size_t count)
642643
{
643644
struct oci_lob_self *self = (struct oci_lob_self*)stream->abstract;
644-
ub4 amt;
645-
sword r;
645+
#if HAVE_OCILOBREAD2
646+
oraub8 byte_amt = (oraub8) count;
647+
oraub8 char_amt = 0;
646648

647-
amt = (ub4) count;
648-
r = OCILobRead(self->E->svc, self->E->err, self->lob,
649-
&amt, self->offset, buf, (ub4) count,
649+
sword r = OCILobRead2(self->E->svc, self->E->err, self->lob,
650+
&byte_amt, &char_amt, (oraub8) self->offset, buf, (oraub8) count,
651+
OCI_ONE_PIECE, NULL, NULL, 0, self->csfrm);
652+
#else
653+
ub4 byte_amt = (ub4) count;
654+
655+
sword r = OCILobRead(self->E->svc, self->E->err, self->lob,
656+
&byte_amt, self->offset, buf, (ub4) count,
650657
NULL, NULL, 0, SQLCS_IMPLICIT);
658+
#endif
651659

652660
if (r != OCI_SUCCESS && r != OCI_NEED_DATA) {
653-
return (size_t)-1;
661+
return (ssize_t)-1;
654662
}
655663

656-
self->offset += amt;
657-
if (amt < count) {
664+
#if HAVE_OCILOBREAD2
665+
self->offset += self->csfrm == 0 ? byte_amt : char_amt;
666+
#else
667+
self->offset += byte_amt;
668+
#endif
669+
if (byte_amt < count) {
658670
stream->eof = 1;
659671
}
660-
return amt;
672+
return byte_amt;
661673
}
662674

663675
static int oci_blob_close(php_stream *stream, int close_handle)
@@ -724,6 +736,8 @@ static php_stream *oci_create_lob_stream(zval *dbh, pdo_stmt_t *stmt, OCILobLoca
724736
self->E->svc = self->S->H->svc;
725737
self->E->err = self->S->err;
726738

739+
OCILobCharSetForm(self->S->H->env, self->S->err, self->lob, &self->csfrm);
740+
727741
stm = php_stream_alloc(&oci_blob_stream_ops, self, 0, "r+b");
728742

729743
if (stm) {

ext/pdo_oci/tests/bug60994.phpt

+21-10
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,19 @@ $dbh->setAttribute(PDO::ATTR_CASE, PDO::CASE_NATURAL);
2121
$dbh->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, false);
2222

2323
@$dbh->exec('DROP TABLE pdo_oci_bug60994');
24-
$dbh->exec('CREATE TABLE pdo_oci_bug60994 (id NUMBER, data CLOB)');
24+
$dbh->exec('CREATE TABLE pdo_oci_bug60994 (id NUMBER, data CLOB, data2 NCLOB)');
2525

2626
$id = null;
27-
$insert = $dbh->prepare('INSERT INTO pdo_oci_bug60994 (id, data) VALUES (:id, :data)');
27+
$insert = $dbh->prepare('INSERT INTO pdo_oci_bug60994 (id, data, data2) VALUES (:id, :data, :data2)');
2828
$insert->bindParam(':id', $id, \PDO::PARAM_STR);
29-
$select = $dbh->prepare("SELECT data FROM pdo_oci_bug60994 WHERE id = :id");
29+
$select = $dbh->prepare("SELECT data, data2 FROM pdo_oci_bug60994 WHERE id = :id");
3030

3131

3232
echo PHP_EOL, 'Test 1: j', PHP_EOL;
3333
$string1 = 'abc' . str_repeat('j', 8187) . 'xyz'; // 8193 chars total works fine here (even 1 million works fine, subject to memory_limit)
3434
$id = 1;
3535
$insert->bindParam(':data', $string1, \PDO::PARAM_STR, strlen($string1)); // length in bytes
36+
$insert->bindParam(':data2', $string1, \PDO::PARAM_STR, strlen($string1));
3637
$insert->execute();
3738
$select->bindParam(':id', $id, \PDO::PARAM_STR);
3839
$select->execute();
@@ -44,12 +45,15 @@ echo 'size of string1 is ', strlen($string1), ' bytes, ', mb_strlen($string1), '
4445
echo 'size of stream1 is ', strlen($stream1), ' bytes, ', mb_strlen($stream1), ' chars.', PHP_EOL;
4546
echo 'beg of stream1 is ', $start1, PHP_EOL;
4647
echo 'end of stream1 is ', $ending1, PHP_EOL;
47-
48+
if ($string1 != $stream1 || $stream1 != stream_get_contents($row['DATA2'])) {
49+
echo 'Expected nclob value to match clob value for stream1', PHP_EOL;
50+
}
4851

4952
echo PHP_EOL, 'Test 2: £', PHP_EOL;
5053
$string2 = 'abc' . str_repeat('£', 8187) . 'xyz'; // 8193 chars total is when it breaks
5154
$id = 2;
5255
$insert->bindParam(':data', $string2, \PDO::PARAM_STR, strlen($string2)); // length in bytes
56+
$insert->bindParam(':data2', $string2, \PDO::PARAM_STR, strlen($string2));
5357
$insert->execute();
5458
$select->bindParam(':id', $id, \PDO::PARAM_STR);
5559
$select->execute();
@@ -61,12 +65,15 @@ echo 'size of string2 is ', strlen($string2), ' bytes, ', mb_strlen($string2), '
6165
echo 'size of stream2 is ', strlen($stream2), ' bytes, ', mb_strlen($stream2), ' chars.', PHP_EOL;
6266
echo 'beg of stream2 is ', $start2, PHP_EOL;
6367
echo 'end of stream2 is ', $ending2, PHP_EOL;
64-
68+
if ($string2 != $stream2 || $stream2 != stream_get_contents($row['DATA2'])) {
69+
echo 'Expected nclob value to match clob value for stream2', PHP_EOL;
70+
}
6571

6672
echo PHP_EOL, 'Test 3: Җ', PHP_EOL;
6773
$string3 = 'abc' . str_repeat('Җ', 8187) . 'xyz'; // 8193 chars total is when it breaks
6874
$id = 3;
6975
$insert->bindParam(':data', $string3, \PDO::PARAM_STR, strlen($string3)); // length in bytes
76+
$insert->bindParam(':data2', $string3, \PDO::PARAM_STR, strlen($string3));
7077
$insert->execute();
7178
$select->bindParam(':id', $id, \PDO::PARAM_STR);
7279
$select->execute();
@@ -78,12 +85,15 @@ echo 'size of string3 is ', strlen($string3), ' bytes, ', mb_strlen($string3), '
7885
echo 'size of stream3 is ', strlen($stream3), ' bytes, ', mb_strlen($stream3), ' chars.', PHP_EOL;
7986
echo 'beg of stream3 is ', $start3, PHP_EOL;
8087
echo 'end of stream3 is ', $ending3, PHP_EOL;
81-
88+
if ($string3 != $stream3 || $stream3 != stream_get_contents($row['DATA2'])) {
89+
echo 'Expected nclob value to match clob value for stream3', PHP_EOL;
90+
}
8291

8392
echo PHP_EOL, 'Test 4: の', PHP_EOL;
8493
$string4 = 'abc' . str_repeat('', 8187) . 'xyz'; // 8193 chars total is when it breaks
8594
$id = 4;
8695
$insert->bindParam(':data', $string4, \PDO::PARAM_STR, strlen($string4)); // length in bytes
96+
$insert->bindParam(':data2', $string4, \PDO::PARAM_STR, strlen($string4));
8797
$insert->execute();
8898
$select->bindParam(':id', $id, \PDO::PARAM_STR);
8999
$select->execute();
@@ -95,13 +105,14 @@ echo 'size of string4 is ', strlen($string4), ' bytes, ', mb_strlen($string4), '
95105
echo 'size of stream4 is ', strlen($stream4), ' bytes, ', mb_strlen($stream4), ' chars.', PHP_EOL;
96106
echo 'beg of stream4 is ', $start4, PHP_EOL;
97107
echo 'end of stream4 is ', $ending4, PHP_EOL;
108+
if ($string4 != $stream4 || $stream4 != stream_get_contents($row['DATA2'])) {
109+
echo 'Expected nclob value to match clob value for stream4', PHP_EOL;
110+
}
98111
?>
99-
--XFAIL--
100-
Fails due to Bug 60994
101112
--EXPECT--
102113
Test 1: j
103-
size of string1 is 1000006 bytes, 1000006 chars.
104-
size of stream1 is 1000006 bytes, 1000006 chars.
114+
size of string1 is 8193 bytes, 8193 chars.
115+
size of stream1 is 8193 bytes, 8193 chars.
105116
beg of stream1 is abcjjjjjjj
106117
end of stream1 is jjjjjjjxyz
107118

0 commit comments

Comments
 (0)